[R] Where are the PCA outputs?

David L Carlson dcarlson at tamu.edu
Mon Sep 12 16:28:48 CEST 2016


At the risk of being redundant, the command prcomp(pcl, scale.=T) is the same as the command print(prcomp(pcl, scale.=T)). This passes the results of prcomp() to print() which prints some of them (whatever the function print.prcomp() is programmed to display) and then throws them away. To save the results, you need to assign them to an object, e.g.

> pcl.pca <- prcomp(pcl, scale.=T)

Or any other name you choose. Now pcl.pca is a list of 5 elements:

> str(pcl.pca)
List of 5
 $ sdev    : num [1:3] 1.405 0.845 0.559
 $ rotation: num [1:3, 1:3] 0.56 0.523 0.643 -0.644 0.762 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:3] "resmat.3...2." "resmat.3...3." "resmat.3...4."
  .. ..$ : chr [1:3] "PC1" "PC2" "PC3"
 $ center  : Named num [1:3] 0.1248 0.0488 0.2545
  ..- attr(*, "names")= chr [1:3] "resmat.3...2." "resmat.3...3." "resmat.3...4."
 $ scale   : Named num [1:3] 0.051 0.0405 0.1023
  ..- attr(*, "names")= chr [1:3] "resmat.3...2." "resmat.3...3." "resmat.3...4."
 $ x       : num [1:19, 1:3] -0.808 -0.887 -0.346 2.341 2.857 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:19] "1" "2" "3" "4" ...
  .. ..$ : chr [1:3] "PC1" "PC2" "PC3"
 - attr(*, "class")= chr "prcomp"

To plot the principal component scores, try

> plot(pcl.pca$x[, 1:2])

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Charles Determan
Sent: Monday, September 12, 2016 8:01 AM
To: WRAY NICHOLAS
Cc: r-help
Subject: Re: [R] Where are the PCA outputs?

Hi Nick,

"prcomp" returns an object of class "prcomp" so when you simply 'print' the
object it gets passed to the "print.prcomp" function.  If you want to see
all the objects you should assign the results to an object.

Regards,
Charles

On Mon, Sep 12, 2016 at 7:56 AM, WRAY NICHOLAS <nicholas.wray at ntlworld.com>
wrote:

> Hi R Folk  I have been kicking some data around and one thing has been to
> try a
> PC analysis  on it, but whereas in the online examples I've looked at the
> prcomp
> function gives a set of five outputs when I use the prcomp function it only
> gives me a set of standard deviations and the rotation matrix
>
> My data (pcl) is this:
>
> resmat.3...2. resmat.3...3. resmat.3...4.
> 1     0.08749276   0.015706470         0.259
> 2     0.08749276   0.039266176         0.198
> 3     0.10630841   0.047119411         0.235
> 4     0.25307047   0.062825881         0.374
> 5     0.14393971   0.117798527         0.534
> 6     0.23049169   0.023559705         0.355
> 7     0.15052518   0.007853235         0.179
> 8     0.09784137   0.031412940         0.219
> 9     0.09878215   0.039266176         0.301
> 10    0.14111736   0.157064702         0.285
> 11    0.03951286   0.015706470         0.036
> 12    0.16181457   0.125651762         0.324
> 13    0.13359110   0.031412940         0.304
> 14    0.08278885   0.031412940         0.221
> 15    0.08561120   0.023559705         0.207
> 16    0.12042015   0.039266176         0.194
> 17    0.13359110   0.047119411         0.164
> 18    0.08937433   0.047119411         0.216
> 19    0.12700562   0.023559705         0.230
>
> the output is then
> > prcomp(pcl,scale.=T)
> Standard deviations:
> [1] 1.4049397 0.8447366 0.5590747
>
> Rotation:
> PC1         PC2        PC3
> resmat.3...2. 0.5599782 -0.64434772 -0.5208075
> resmat.3...3. 0.5229417  0.76245515 -0.3810434
> resmat.3...4. 0.6426168 -0.05897597  0.7639146
>
> Does anyone know why the other things are not appearing?
>
> Thanks, Nick
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list