2013-11-15 30 views
0

我試圖做PCA R中的差異和使用prcomp功能: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

我只是要評論,沒有什麼可以解決在這裏;兩個輸出都是完美的和數學上正確的(忽略浮點運算的複雜性以及R用於打印到控制檯的精度)。 –

回答

0

僅僅通過-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某些列只有那些列工作乘以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 
+0

非常感謝您的快速回復。家庭作業是在午夜,我得到它! –