prcomp函数在R旋转值中的符号差异

时间:2013-11-15 05:11:03

标签: r

我正在尝试在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代码?请...

1 个答案:

答案 0 :(得分: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