【问题标题】:How do I extract summary of PCA as a dataframe in R using Prcomp?如何使用 Prcomp 将 PCA 的摘要提取为 R 中的数据框?
【发布时间】:2018-07-12 08:58:58
【问题描述】:
res.pca = prcomp(y, scale = TRUE)
summ=summary(res.pca)
summ

给我输出Desired Output

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

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

请看我不想从列名中提取值,我寻求一个我可以使用的通用解决方案。

【问题讨论】:

  • 当我尝试 as.data.frame(summ) 时,它说无法将“summary.prcomp”类强制转换为 data.frame。
  • 当我尝试 tidy(summ) 时,它说:“No method for tidying an S3 object of class summary.prcomp , using as.data.frame”

标签: r dataframe pca


【解决方案1】:

你要找的是summary(res.pca)的“元素”importance

示例取自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"

注意:
当你想“研究”一个对象时,可以方便地在其上使用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"

【讨论】:

  • s
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-06
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多