In R programming language and package pcaPP I have this code:
# multivariate data with outliers
library(mvtnorm)
library(pcaPP)
x <- rbind(rmvnorm(200, rep(0, 6), diag(c(5, rep(1,5)))),
rmvnorm( 15, c(0, rep(20, 5)), diag(rep(1, 6))))
# Here we calculate the principal components with PCAgrid
pc <- PCAproj(x)
Here is documentation for the output value of the PCAproj function:
The function returns a list of class '"princomp"', i.e. a list similar to the output of the function 'princomp'. sdev: the (robust) standard deviations of the principal components. loadings: the matrix of variable loadings (i.e., a matrix whose columns contain the eigen- vectors). This is of class "loadings": see loadings for its print method. center: the means that were subtracted. scale: the scalings applied to each variable. n.obs: the number of observations. scores: if 'scores = TRUE', the scores of the supplied data on the principal components. call: the matched call.
How can I call other outputs of PCAproj like loadings and sdev and report those in R-studio?
In your example, it is all stored in pc.
If you are in interactive mode, just type pc$sdev and pc$loading to see what they contain.
> pc$sdev
Comp.1 Comp.2
2.425413 1.346727
> pc$loadings
Loadings:
Comp.1 Comp.2
V1 0.972 0.153
V2 -0.201 0.447
V3 -0.130
V4 -0.211
V5 0.739
V6 -0.109 0.412
Comp.1 Comp.2
SS loadings 1.000 1.000
Proportion Var 0.167 0.167
Cumulative Var 0.167 0.333
Just to top up a bit of what Mr. Bottoms said, I have found the following set of functions super useful when digging deeper into outputs like your pc object -- names(), str(), and summary.
# Set Up
library(mvtnorm)
library(pcaPP)
x <- rbind(rmvnorm(200, rep(0, 6), diag(c(5, rep(1,5)))),
rmvnorm( 15, c(0, rep(20, 5)), diag(rep(1, 6))))
pc <- PCAproj(x)
names() returns a vector of names of the top level of each element in your data structure.
> names(pc)
[1] "loadings" "sdev" "center" "scale" "n.obs" "scores" "call"
str() is short for structure. It outputs an easy to read description of your R data structure. I like to think of it as a table of contents. You'll notice that it matches your names list.
str(pc)
List of 7
$ loadings: loadings [1:6, 1:2] 0.962 0.1011 0.048 0.2461 0.0152 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:6] "V1" "V2" "V3" "V4" ...
.. ..$ : chr [1:2] "Comp.1" "Comp.2"
$ sdev : Named num [1:2] 2.79 1.39
..- attr(*, "names")= chr [1:2] "Comp.1" "Comp.2"
$ center : num [1:6] 0.193 0.114 0.093 0.117 0.215 ...
$ scale : num [1:6(1d)] 1 1 1 1 1 1
$ n.obs : int 215
$ scores : num [1:215, 1:2] -0.413 1.707 0.835 2.164 0.495 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:215] "1" "2" "3" "4" ...
.. ..$ : chr [1:2] "Comp.1" "Comp.2"
$ call : language PCAproj(x = x)
- attr(*, "class")= chr [1:2] "pcaPP" "princomp"
summary() most well designed functions allow you to pass your new object into the summary function, and it returns ... let's call it the "most obvious and useful" summary of the output of that function.
> summary(pc)
Importance of components:
Comp.1 Comp.2
Standard deviation 2.7873357 1.3855889
Proportion of Variance 0.8018539 0.1981461
Cumulative Proportion 0.8018539 1.0000000
And then RStudio and other IDEs have cool features like tab auto-complete, so if you type pc$ and then hit the tab key, it'll list all those names listed above. And then you can use your arrow keys to select which element you want to select.
> str(pc$loadings)
loadings [1:6, 1:2] 0.962 0.1011 0.048 0.2461 0.0152 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:6] "V1" "V2" "V3" "V4" ...
..$ : chr [1:2] "Comp.1" "Comp.2"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With