我希望在R中的pca之后检索解释方差的累积比例。summary(pca)
在最后一行返回此结果,但是如何提取该行?
summary(prcomp(USArrests, scale = TRUE))
Importance of components:
PC1 PC2 PC3 PC4
Standard deviation 1.5749 0.9949 0.59713 0.41645
Proportion of Variance 0.6201 0.2474 0.08914 0.04336
Cumulative Proportion 0.6201 0.8675 0.95664 1.00000
我尝试了s <- summary(prcomp(USArrests, scale = TRUE))
和s [3]等,但它没有返回最后一行。
答案 0 :(得分:10)
你可以尝试
pr <- prcomp(USArrests, scale = TRUE)
vars <- apply(pr$x, 2, var)
props <- vars / sum(vars)
cumsum(props)
答案 1 :(得分:9)
您也可以直接从特征值中提取此信息(即标准开发:pr$sdev
):
pr <- prcomp(USArrests, scale = TRUE)
cumsum(pr$sdev^2 / sum(pr$sdev^2))
答案 2 :(得分:2)
在问题的评论中扩展用户20650的回答,因为我认为它最直接地回答了问题(即通过对象本身,而不是重新计算)。 TL; DR:s$importance[3, ]
。
(s <- summary(prcomp(USArrests, scale = TRUE)))
# Importance of components:
# PC1 PC2 PC3 PC4
# Standard deviation 1.5749 0.9949 0.59713 0.41645
# Proportion of Variance 0.6201 0.2474 0.08914 0.04336
# Cumulative Proportion 0.6201 0.8675 0.95664 1.00000
str(s)
# List of 6
# $ sdev : num [1:4] 1.575 0.995 0.597 0.416
# $ rotation : num [1:4, 1:4] -0.536 -0.583 -0.278 -0.543 0.418 ...
# ..- attr(*, "dimnames")=List of 2
# .. ..$ : chr [1:4] "Murder" "Assault" "UrbanPop" "Rape"
# .. ..$ : chr [1:4] "PC1" "PC2" "PC3" "PC4"
# $ center : Named num [1:4] 7.79 170.76 65.54 21.23
# ..- attr(*, "names")= chr [1:4] "Murder" "Assault" "UrbanPop" "Rape"
# $ scale : Named num [1:4] 4.36 83.34 14.47 9.37
# ..- attr(*, "names")= chr [1:4] "Murder" "Assault" "UrbanPop" "Rape"
# $ x : num [1:50, 1:4] -0.976 -1.931 -1.745 0.14 -2.499 ...
# ..- attr(*, "dimnames")=List of 2
# .. ..$ : chr [1:50] "Alabama" "Alaska" "Arizona" "Arkansas" ...
# .. ..$ : chr [1:4] "PC1" "PC2" "PC3" "PC4"
# $ importance: num [1:3, 1:4] 1.575 0.62 0.62 0.995 0.247 ...
# ..- attr(*, "dimnames")=List of 2
# .. ..$ : chr [1:3] "Standard deviation" "Proportion of Variance" "Cumulative Proportion"
# .. ..$ : chr [1:4] "PC1" "PC2" "PC3" "PC4"
# - attr(*, "class")= chr "summary.prcomp"
# We see importance is the relevant feature
s$importance
# PC1 PC2 PC3 PC4
# Standard deviation 1.574878 0.9948694 0.5971291 0.4164494
# Proportion of Variance 0.620060 0.2474400 0.0891400 0.0433600
# Cumulative Proportion 0.620060 0.8675000 0.9566400 1.0000000
# Cool, same as displayed the table. Grab the third row and voila.
s$importance[3, ] # Numeric vector
# PC1 PC2 PC3 PC4
# 0.62006 0.86750 0.95664 1.00000
答案 3 :(得分:0)
这个怎么样?
R » s <- as.data.frame( summary(prcomp(USArrests, scale=TRUE))$importance )
R » s[3, 1:2]
PC1 PC2
Cumulative Proportion 0.6201 0.8675
R » message("PC1: ", s[3,1])
PC1: 0.62006