问题描述
我在哪里错了?我正在尝试通过prcomp和我自己执行PCA,我得到不同的结果,您能帮我吗?
Where do I am wrong? I am trying to perform PCA through prcomp and by myself,and I get different results, can you please help me?
由我自己做:
>database <- read.csv("E:/R/database.csv", sep=";", dec=",") #it's a 105 rows x 8 columns, each column is a variable
>matrix.cor<-cor(database)
>standardize<-function(x) {(x-mean(x))/sd(x)}
>values.standard<-apply(database, MARGIN=2, FUN=standardize)
>my.eigen<-eigen(matrix.cor)
>loadings<-my.eigen$vectors
>scores<-values.standard %*% loadings
>head (scores, n=10) # I m just posting here the first row scores for the first 6 pc
[,1] [,2] [,3] [,4] [,5] [,6]
2.3342586 2.3426398 -0.9169527 0.80711713 1.1409138 -0.25832090
>sd <-sqrt (my.eigen$values)
>sd
[1] 1.5586078 1.1577093 1.1168477 0.9562853 0.8793033 0.8094500 0.6574788
0.4560247
通过PRCOMP做到这一点
DOING IT WITH PRCOMP:
>database.pca<-prcomp(database, retx=TRUE, center= TRUE, scale=TRUE)
>sd1<-database.pca$sdev
>loadings1<-database.pca$rotation
>rownames(loadings1)<-colnames(database)
>scores1<-database.pca$x
>head (scores1, n=10)
PC1 PC2 PC3 PC4 PC5 PC6
-2.3342586 2.3426398 0.9169527 0.80711713 1.1409138 0.25832090
范围(scores-scores1)不为零!请帮我!!!格洛里亚
range (scores-scores1) is not zero!Please help me!!!Gloria
推荐答案
看来您的主成分分数或多或少完全相同,只是符号不同.正如我在此处所了解的那样,主要成分的符号基本上是任意的.
It looks like your principal component scores have come out more or less exactly the same, just with different signs. As I learned here, the sign of a principal component is basically arbitrary.
如果用range(abs(scores) - abs(scores1))
之类的方法测试手动计算的分数,则应该得到接近0的值(由于可能的浮点精度影响,可能不完全为0).
If you test your manually calculated scores with something like range(abs(scores) - abs(scores1))
instead, you should get something pretty close to 0 (maybe not exactly 0, due to possible floating-point precision effects).
这篇关于用prcomp和我自己进行R中的主成分分析:不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!