使用 prcomp 手动计算第一主成分时出现冲突的结果

Conflicting Results When Manually Calculating First Principal Component using prcomp

我计算 iris 数据集的 PCA 如下:

data(iris)
ir.pca <- prcomp(iris[, 1:4], center = TRUE, scale. = TRUE)

这是鸢尾花数据集的第一行:

head(iris, 1)
#Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1          5.1         3.5          1.4         0.2  setosa

对于第一行,我可以看到第一个主成分的值为-2.257141:

head(ir.pca$x, 1)
#           PC1        PC2       PC3        PC4
#[1,] -2.257141 -0.4784238 0.1272796 0.02408751

但是当我尝试提取载荷时:

ir.pca$rotation[, 1]
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
0.5210659   -0.2693474    0.5804131    0.5648565 

并自己计算第一个主成分:

0.5210659 * 5.1  + -0.2693474 * 3.5  + 0.5804131 * 1.4 + 0.5648565 * 0.2

我得到了不同的结果 2.64027。

这是为什么?

缩放是个问题。

prcomp() 调用中降低缩放比例

data(iris)
ir.pca <- prcomp(iris[, 1:4], center = FALSE, scale. = FALSE)

head(ir.pca$x, 1)
           # PC1      PC2         PC3         PC4
# [1,] -5.912747 2.302033 0.007401536 0.003087706

ir.pca$rotation[, 1] %*% t(iris[1, 1:4])
             # 1
# [1,] -5.912747

或在手动应用载荷之前缩放 iris

ir.pca <- prcomp(iris[, 1:4], center = TRUE, scale. = TRUE)

head(ir.pca$x, 1)
           # PC1        PC2       PC3        PC4
# [1,] -2.257141 -0.4784238 0.1272796 0.02408751

ir.pca$rotation[, 1] %*% scale(iris[, 1:4])[1,]
          # [,1]
# [1,] -2.257141