如何使用Prcomp提取PCA摘要作为R中的数据框?

时间:2018-07-12 08:58:59

标签: r dataframe pca

res.pca = prcomp(y, scale = TRUE)
summ=summary(res.pca)
summ

给我输出Desired Output

我想将此摘要更改为数据框,

我试图使用do.call(cbind,lapply(res.pca,summary)),但是它给了我Min / Max的摘要,而不是我想要的摘要。

请注意,我不想从列名中提取值,我在寻找可以使用的常规解决方案。

1 个答案:

答案 0 :(得分:3)

您要查找的是importance中的“元素” summary(res.pca)

示例取自Principal Components Analysis - how to get the contribution (%) of each parameter to a Prin.Comp.?

a <- rnorm(10, 50, 20)
b <- seq(10, 100, 10)
c <- seq(88, 10, -8)
d <- rep(seq(3, 16, 3), 2)
e <- rnorm(10, 61, 27)

my_table <- data.frame(a, b, c, d, e)
res.pca <- prcomp(my_table, scale = TRUE)

summary(res.pca)$importance 
#                          PC1    PC2    PC3     PC4       PC5
#Standard deviation     1.7882 0.9038 0.8417 0.52622 9.037e-17
#Proportion of Variance 0.6395 0.1634 0.1417 0.05538 0.000e+00
#Cumulative Proportion  0.6395 0.8029 0.9446 1.00000 1.000e+00

class(summary(res.pca)$importance)
#[1] "matrix"

N.B。:
当您要“研究”一个对象时,可以方便地在其上使用str。在这里,您可以进行str(summary(pca)来查看信息在哪里,从而在哪里可以得到想要的东西:

str(summary(res.pca))

List of 6
 $ sdev      : num [1:5] 1.79 9.04e-01 8.42e-01 5.26e-01 9.04e-17
 $ rotation  : num [1:5, 1:5] 0.278 0.512 -0.512 0.414 -0.476 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:5] "a" "b" "c" "d" ...
  .. ..$ : chr [1:5] "PC1" "PC2" "PC3" "PC4" ...
 $ center    : Named num [1:5] 34.9 55 52 9 77.8
  ..- attr(*, "names")= chr [1:5] "a" "b" "c" "d" ...
 $ scale     : Named num [1:5] 22.4 30.28 24.22 4.47 26.11
  ..- attr(*, "names")= chr [1:5] "a" "b" "c" "d" ...
 $ x         : num [1:10, 1:5] -2.962 -1.403 -1.653 -0.537 1.186 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:5] "PC1" "PC2" "PC3" "PC4" ...
 $ importance: num [1:3, 1:5] 1.788 0.64 0.64 0.904 0.163 ...
 ..- attr(*, "dimnames")=List of 2
 .. ..$ : chr [1:3] "Standard deviation" "Proportion of Variance" "Cumulative Proportion"
 .. ..$ : chr [1:5] "PC1" "PC2" "PC3" "PC4" ...
- attr(*, "class")= chr "summary.prcomp"