我知道,PCA可以用以下方法进行 prcomp()
函数,或者用 preProcess()
中的功能 caret
包等。
首先,我是否可以这样说,如果我们只是使用默认的设置来操作类型为 prcomp(<SOME_MATRIX>)
或 preProcess(<SOME_MATRIX>, method = "pca")
那么,我们结果的唯一区别是 prcomp()
在进行PCA之前并没有对数据进行居中和缩放,而preProcess()却可以。因此,是否 prcomp(scale(<SOME_MATRIX>))
和 preProcess(<SOME_MATRIX>, method = "pca")
输出相同的东西?
其次,也是更重要的一点,我们如何从以下两种PC的输出中获得每个PC所解释的方差百分比?prcomp()
或 preProcess()
? 从这两个输出结果中,我可以看到诸如均值、标准差或旋转之类的东西,但我认为这些只是指 "旧 "变量。关于 "新 "PC的信息在哪里,它们占了多少方差?
例如,如果我在使用 preProcess(<SOME_MATRIX>, method = "pca", thresh = 0.8)
并返回6台PC,但我发现前5台PC共解释了79.5%的方差。那么我可能会倾向于不包含所有6个PC。
由于你的第一个问题已经回答了,这里回答你的第二个问题,为 prcomp
. 我们可以通过调用以下方法得到每台PC的方差解释率 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
包(见所讨论的问题 此处):
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"