Principal component analysis in R with prcomp and by myself: different results

…衆ロ難τιáo~ 提交于 2020-01-10 10:44:01

问题


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?

DOING IT BY MYSELF:

>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

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

range (scores-scores1) is not zero! Please help me!!! Gloria


回答1:


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.

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).



来源:https://stackoverflow.com/questions/14593603/principal-component-analysis-in-r-with-prcomp-and-by-myself-different-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!