如何获得 R 中解释的主成分百分比方差? prcomp() 和 preProcess() 比较
How to obtain principal component % variance explained in R? prcomp() and preProcess() comparison
我知道可以使用 base R 中的 prcomp()
函数或 caret
包中的 preProcess()
函数等进行 PCA。
首先,如果我们只对 prcomp(<SOME_MATRIX>)
或 preProcess(<SOME_MATRIX>, method = "pca")
类型的操作使用默认设置,那么我们结果的唯一区别是 prcomp()
在进行 PCA 之前不会居中和缩放数据,而 preProcess() 会吗?所以,prcomp(scale(<SOME_MATRIX>))
和preProcess(<SOME_MATRIX>, method = "pca")
输出的是一样的吗?
其次,更重要的是,我们如何从 prcomp()
或 preProcess()
的输出中获得每个 PC 解释的百分比方差?从这两个输出中,我可以看到平均值、标准偏差或旋转等内容,但我认为这些仅指 'old' 变量。关于 'new' 个 PC 的信息在哪里以及它们占多少方差?
如果我使用 preProcess(<SOME_MATRIX>, method = "pca", thresh = 0.8)
和这 returns 6 台电脑,这可能会有用,但我发现前 5 台电脑总共解释了 79.5% 的方差.那么我可能倾向于不包括所有 6 台电脑。
由于您的第一个问题已经得到解答,这里是 prcomp
的第二个问题的答案。我们可以通过调用 summary
:
来获得每个 PC 解释的百分比方差
df <- iris[1:4]
pca_res <- prcomp(df, scale. = TRUE)
summ <- summary(pca_res)
summ
#Importance of components:
# PC1 PC2 PC3 PC4
#Standard deviation 1.7084 0.9560 0.38309 0.14393
#Proportion of Variance 0.7296 0.2285 0.03669 0.00518
#Cumulative Proportion 0.7296 0.9581 0.99482 1.00000
summ$importance[2,]
# PC1 PC2 PC3 PC4
#0.72962 0.22851 0.03669 0.00518
据我所知,使用 caret
包时此信息不可用(请参阅讨论的问题 here):
mod <- train(Species ~ ., data = iris, method = "knn",
preProc = c("center", "scale", "pca"))
str(mod$preProcess)
List of 22
$ dim : int [1:2] 150 4
$ bc : NULL
$ yj : NULL
$ et : NULL
$ invHyperbolicSine: NULL
$ mean : Named num [1:4] 5.84 3.06 3.76 1.2
..- attr(*, "names")= chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
$ std : Named num [1:4] 0.828 0.436 1.765 0.762
..- attr(*, "names")= chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
$ ranges : NULL
$ rotation : num [1:4, 1:2] 0.521 -0.269 0.58 0.565 -0.377 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
.. ..$ : chr [1:2] "PC1" "PC2"
$ method :List of 4
..$ center: chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
..$ scale : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
..$ pca : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
..$ ignore: chr(0)
$ thresh : num 0.95
$ pcaComp : NULL
$ numComp : num 2
$ ica : NULL
$ wildcards :List of 2
..$ PCA: chr(0)
..$ ICA: chr(0)
$ k : num 5
$ knnSummary :function (x, ...)
$ bagImp : NULL
$ median : NULL
$ data : NULL
$ rangeBounds : num [1:2] 0 1
$ call : chr "scrubed"
- attr(*, "class")= chr "preProcess"
我知道可以使用 base R 中的 prcomp()
函数或 caret
包中的 preProcess()
函数等进行 PCA。
首先,如果我们只对 prcomp(<SOME_MATRIX>)
或 preProcess(<SOME_MATRIX>, method = "pca")
类型的操作使用默认设置,那么我们结果的唯一区别是 prcomp()
在进行 PCA 之前不会居中和缩放数据,而 preProcess() 会吗?所以,prcomp(scale(<SOME_MATRIX>))
和preProcess(<SOME_MATRIX>, method = "pca")
输出的是一样的吗?
其次,更重要的是,我们如何从 prcomp()
或 preProcess()
的输出中获得每个 PC 解释的百分比方差?从这两个输出中,我可以看到平均值、标准偏差或旋转等内容,但我认为这些仅指 'old' 变量。关于 'new' 个 PC 的信息在哪里以及它们占多少方差?
如果我使用 preProcess(<SOME_MATRIX>, method = "pca", thresh = 0.8)
和这 returns 6 台电脑,这可能会有用,但我发现前 5 台电脑总共解释了 79.5% 的方差.那么我可能倾向于不包括所有 6 台电脑。
由于您的第一个问题已经得到解答,这里是 prcomp
的第二个问题的答案。我们可以通过调用 summary
:
df <- iris[1:4]
pca_res <- prcomp(df, scale. = TRUE)
summ <- summary(pca_res)
summ
#Importance of components:
# PC1 PC2 PC3 PC4
#Standard deviation 1.7084 0.9560 0.38309 0.14393
#Proportion of Variance 0.7296 0.2285 0.03669 0.00518
#Cumulative Proportion 0.7296 0.9581 0.99482 1.00000
summ$importance[2,]
# PC1 PC2 PC3 PC4
#0.72962 0.22851 0.03669 0.00518
据我所知,使用 caret
包时此信息不可用(请参阅讨论的问题 here):
mod <- train(Species ~ ., data = iris, method = "knn",
preProc = c("center", "scale", "pca"))
str(mod$preProcess)
List of 22
$ dim : int [1:2] 150 4
$ bc : NULL
$ yj : NULL
$ et : NULL
$ invHyperbolicSine: NULL
$ mean : Named num [1:4] 5.84 3.06 3.76 1.2
..- attr(*, "names")= chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
$ std : Named num [1:4] 0.828 0.436 1.765 0.762
..- attr(*, "names")= chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
$ ranges : NULL
$ rotation : num [1:4, 1:2] 0.521 -0.269 0.58 0.565 -0.377 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
.. ..$ : chr [1:2] "PC1" "PC2"
$ method :List of 4
..$ center: chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
..$ scale : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
..$ pca : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
..$ ignore: chr(0)
$ thresh : num 0.95
$ pcaComp : NULL
$ numComp : num 2
$ ica : NULL
$ wildcards :List of 2
..$ PCA: chr(0)
..$ ICA: chr(0)
$ k : num 5
$ knnSummary :function (x, ...)
$ bagImp : NULL
$ median : NULL
$ data : NULL
$ rangeBounds : num [1:2] 0 1
$ call : chr "scrubed"
- attr(*, "class")= chr "preProcess"