0

我正在尝试在 R 中进行 PCA 并使用 prcomp 函数:

 pca = prcomp(Matrix)
 > pc
 Standard deviations:
 [1] 8393.8274 1011.6205  818.8312  698.5403

 Rotation:
            PC1         PC2         PC3        PC4
 V2 -0.02241626 -0.36009538 -0.92000949  0.1530077
 V3 -0.29054489  0.62959907 -0.12122144  0.7102774
 V4 -0.92701517 -0.01334916 -0.03425825 -0.3732172
 V5  0.23605944  0.68830090 -0.37109873 -0.5768913

我的 PCA - 集群在左边,+x 值打算 -x 值,集群在右边。什么时候应该输出:

 Rotation:
             PC1         PC2        PC3        PC4
  V2   0.02241626 -0.36009538 0.92000949 -0.1530077
  V3   0.29054489  0.62959907 0.12122144 -0.7102774
  V4   0.92701517 -0.01334916 0.03425825  0.3732172
  V5  -0.23605944  0.68830090 0.37109873  0.5768913

我从 stats.stackexchange.com/q/30348/5443 阅读。这是任意的,但谁能告诉我如何修复它的 R 代码?请...

4

1 回答 1

0

只需将rotation矩阵乘以-1

> pca <- prcomp(iris[, 1:4])
> pca$rotation
                     PC1         PC2         PC3        PC4
Sepal.Length  0.36138659 -0.65658877  0.58202985  0.3154872
Sepal.Width  -0.08452251 -0.73016143 -0.59791083 -0.3197231
Petal.Length  0.85667061  0.17337266 -0.07623608 -0.4798390
Petal.Width   0.35828920  0.07548102 -0.54583143  0.7536574
> pca$rotation * -1
                     PC1         PC2         PC3        PC4
Sepal.Length -0.36138659  0.65658877 -0.58202985 -0.3154872
Sepal.Width   0.08452251  0.73016143  0.59791083  0.3197231
Petal.Length -0.85667061 -0.17337266  0.07623608  0.4798390
Petal.Width  -0.35828920 -0.07548102  0.54583143 -0.7536574

如果您只需要翻转某些列或rotation仅使用这些列;在您的示例中,您似乎想要翻转第 1、3 和 4 列:

> pca
Standard deviations:
[1] 2.0562689 0.4926162 0.2796596 0.1543862

Rotation:
                     PC1         PC2         PC3        PC4
Sepal.Length  0.36138659 -0.65658877  0.58202985  0.3154872
Sepal.Width  -0.08452251 -0.73016143 -0.59791083 -0.3197231
Petal.Length  0.85667061  0.17337266 -0.07623608 -0.4798390
Petal.Width   0.35828920  0.07548102 -0.54583143  0.7536574
> rot <- pca$rotation
> rot[, c(1, 3, 4)] <- rot[, c(1, 3, 4)] * -1
> rot
                     PC1         PC2         PC3        PC4
Sepal.Length -0.36138659 -0.65658877 -0.58202985 -0.3154872
Sepal.Width   0.08452251 -0.73016143  0.59791083  0.3197231
Petal.Length -0.85667061  0.17337266  0.07623608  0.4798390
Petal.Width  -0.35828920  0.07548102  0.54583143 -0.7536574
于 2013-11-15T05:35:52.973 回答