在R
编程语言和包pcaPP
中,我有以下代码:
# 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)
以下是PCAproj
函数输出值的文档:
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.
如何调用PCAproj
的其他输出,例如loadings
和sdev
,并在R-studio中报告这些输出?
答案 0 :(得分:4)
在您的示例中,它全部存储在pc
中。
如果您处于互动模式,只需输入pc$sdev
和pc$loading
即可查看其中包含的内容。
> 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
答案 1 :(得分:1)
为了补充一下Bottoms先生所说的内容,我发现以下一组功能在深入研究诸如pc
对象 - names()
,{{1}等输出时非常有用}和str()
。
summary
names()返回数据结构中每个元素顶级名称的向量。
# 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)
str()是结构的缩写。它输出一个易于阅读的R数据结构描述。我想把它当作一个目录。您会注意到它与您的名单相匹配。
> names(pc)
[1] "loadings" "sdev" "center" "scale" "n.obs" "scores" "call"
summary()最精心设计的函数允许您将新对象传递给摘要函数,并返回...让我们称之为“最明显和最有用”的输出摘要那个功能。
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"
然后RStudio和其他IDE具有很强大的功能,如标签自动完成,因此如果您键入> 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
然后点击pc$
键,它将列出上面列出的所有名称。然后,您可以使用箭头键选择要选择的元素。
tab