如何获得 R 中解释的主成分方差百分比?prcomp() 和 preProcess() 比较

kma*_*nka 2 r pca r-caret

prcomp()我知道 PCA 可以使用基础 R 中的函数或包preProcess()中的函数caret等进行。

首先,我是否正确地说,如果我们只使用prcomp(<SOME_MATRIX>)或类型的操作的默认设置preProcess(<SOME_MATRIX>, method = "pca"),那么我们结果的唯一区别是prcomp()在进行 PCA 之前不会对数据进行居中和缩放,而 preProcess() 会这样做?因此,执行prcomp(scale(<SOME_MATRIX>))preProcess(<SOME_MATRIX>, method = "pca")输出相同的事情吗?

prcomp()其次,更重要的是,我们如何从或 的输出中获得每台 PC 解释的方差百分比preProcess()?从这两个输出中,我可以看到诸如平均值、标准差或旋转之类的信息,但我认为这些仅指“旧”变量。关于“新”电脑的信息在哪里?它们造成了多少差异?

例如,如果我正在使用preProcess(<SOME_MATRIX>, method = "pca", thresh = 0.8)并且返回 6 个 PC,则这可能会很有用,但我发现前 5 个 PC 总共解释了 79.5% 的方差。那么我可能倾向于不包括所有 6 台 PC。

Dom*_*ier 6

由于您的第一个问题已经得到解答,这里是您的第二个问题的答案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
Run Code Online (Sandbox Code Playgroud)

据我所知,使用该包时此信息不可用(请参阅此处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"
Run Code Online (Sandbox Code Playgroud)