# PCs scores from Correlation and Covariance matrices through matrix computations and prcomp

I'm want to get PCs scores through matrix approach. My calculated PCs scores for correlation matrix matches with prcomp results but the PCs scores for covariance matrix do not match with the results of prcomp. Could you point out what am I missing? Thanks

PCA on Correlation matrix

# PCA on Correlation matrix
X <- USArrests
Cor <- cor(X)

EigenCor <- eigen(Cor)
ECor <- EigenCor$vectors head(t(t(ECor) %*% t(scale(X)))) [,1] [,2] [,3] [,4] Alabama -0.9756604 1.1220012 -0.43980366 0.154696581 Alaska -1.9305379 1.0624269 2.01950027 -0.434175454 Arizona -1.7454429 -0.7384595 0.05423025 -0.826264240 Arkansas 0.1399989 1.1085423 0.11342217 -0.180973554 California -2.4986128 -1.5274267 0.59254100 -0.338559240 Colorado -1.4993407 -0.9776297 1.08400162 0.001450164 PCACor <- prcomp(x = X, retx = TRUE, center = TRUE, scale. = TRUE) summary(PCACor) Importance of components: PC1 PC2 PC3 PC4 Standard deviation 1.5749 0.9949 0.59713 0.41645 Proportion of Variance 0.6201 0.2474 0.08914 0.04336 Cumulative Proportion 0.6201 0.8675 0.95664 1.00000 head(PCACor$x)
PC1        PC2         PC3          PC4
Alabama    -0.9756604  1.1220012 -0.43980366  0.154696581
Arizona    -1.7454429 -0.7384595  0.05423025 -0.826264240
Arkansas    0.1399989  1.1085423  0.11342217 -0.180973554
California -2.4986128 -1.5274267  0.59254100 -0.338559240


PCA on Covariance matrix

# PCA on Covariance matrix
Cov <- var(X)
EigenCov <- eigen(Cov)
ECov <- EigenCov$vectors head(t(t(ECov) %*% t(X))) [,1] [,2] [,3] [,4] Alabama -239.7035 -46.45394 -5.873077 5.7840485 Alaska -267.7288 -39.91901 16.748431 -0.7178995 Arizona -298.9695 -66.73235 -5.065592 -0.9775376 Arkansas -193.2414 -41.19804 -3.167955 2.8551540 California -282.3243 -80.42202 3.367729 0.5643217 Colorado -209.8773 -71.62154 8.901219 1.6546839 PCACov <- prcomp(x = X, retx = TRUE, center = TRUE, scale. = FALSE) summary(PCACov) Importance of components: PC1 PC2 PC3 PC4 Standard deviation 83.7324 14.21240 6.4894 2.48279 Proportion of Variance 0.9655 0.02782 0.0058 0.00085 Cumulative Proportion 0.9655 0.99335 0.9991 1.00000 head(PCACov$x)
PC1        PC2        PC3        PC4
Alabama     64.80216 -11.448007 -2.4949328 -2.4079009
Arizona    124.06822   8.830403 -1.6874484  4.3536852
Arkansas    18.34004 -16.703911  0.2101894  0.5209936
California 107.42295  22.520070  6.7458730  2.8118259


I don't know R but can see your mistake. When you do it with correlations, you correctly multiply standardized data by the eigenvectors (I guess it's scale(X) which standardizes) to get the PC scores.

When you analyse covariances, you must multiply centered data by the eigenvectors. But instead, you are multiplying raw data by the eigenvectors. Hence you get incorrect scores.

Translating the answer of @ttnphns into R.

PCA on Correlation matrix

# PCA on Correlation matrix
X <- USArrests
Cor <- cor(X)

EigenCor <- eigen(Cor)
ECor <- EigenCor$vectors head(t(t(ECor) %*% t(scale(X)))) [,1] [,2] [,3] [,4] Alabama -0.9756604 1.1220012 -0.43980366 0.154696581 Alaska -1.9305379 1.0624269 2.01950027 -0.434175454 Arizona -1.7454429 -0.7384595 0.05423025 -0.826264240 Arkansas 0.1399989 1.1085423 0.11342217 -0.180973554 California -2.4986128 -1.5274267 0.59254100 -0.338559240 Colorado -1.4993407 -0.9776297 1.08400162 0.001450164 PCACor <- prcomp(x = X, retx = TRUE, center = TRUE, scale. = TRUE) summary(PCACor) Importance of components: PC1 PC2 PC3 PC4 Standard deviation 1.5749 0.9949 0.59713 0.41645 Proportion of Variance 0.6201 0.2474 0.08914 0.04336 Cumulative Proportion 0.6201 0.8675 0.95664 1.00000 head(PCACor$x)
PC1        PC2         PC3          PC4
Alabama    -0.9756604  1.1220012 -0.43980366  0.154696581
Arizona    -1.7454429 -0.7384595  0.05423025 -0.826264240
Arkansas    0.1399989  1.1085423  0.11342217 -0.180973554
California -2.4986128 -1.5274267  0.59254100 -0.338559240


PCA on Covariance matrix

# PCA on Covariance matrix
Cov <- var(X)
EigenCov <- eigen(Cov)
ECov <- EigenCov$vectors head(t(t(ECov) %*% t(scale(x = X, center = TRUE, scale = FALSE)))) [,1] [,2] [,3] [,4] Alabama -64.80216 11.448007 -2.4949328 2.4079009 Alaska -92.82745 17.982943 20.1265749 -4.0940470 Arizona -124.06822 -8.830403 -1.6874484 -4.3536852 Arkansas -18.34004 16.703911 0.2101894 -0.5209936 California -107.42295 -22.520070 6.7458730 -2.8118259 Colorado -34.97599 -13.719584 12.2793628 -1.7214637 # Here is the change PCACov <- prcomp(x = X, retx = TRUE, center = TRUE, scale. = FALSE) summary(PCACov) Importance of components: PC1 PC2 PC3 PC4 Standard deviation 83.7324 14.21240 6.4894 2.48279 Proportion of Variance 0.9655 0.02782 0.0058 0.00085 Cumulative Proportion 0.9655 0.99335 0.9991 1.00000 head(PCACov$x)
PC1        PC2        PC3        PC4
Alabama     64.80216 -11.448007 -2.4949328 -2.4079009