我哪里错了?我正在尝试通过 prcomp 和我自己执行 PCA,但我得到了不同的结果,你能帮帮我吗?
自己动手:
>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 进行操作:
>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) 不为零!请帮我!!!格洛丽亚