Reputation: 3089
When I try to compute the principal components (PC) scores (i.e., the dataset projected in the PC space), I obtain a different result from what prcomp()
reports in the $x
attribute of its result. I center and scale the data.
I run prcomp()
as
pca.result = prcomp(USArrests, scale=TRUE)
Then I can access the the wanted PC scores as
pca.result$x # 1
From the doc., I understand the above centers and scales the data before multiplying them by the rotation matrix (a.k.a. variable loadings, a.k.a. eigenvectors). I am trying to reproduce the result above by projecting (rotating) the dataset myself, as in:
((as.matrix(USArrests)-pca.result$center)/pca.result$scale) %*% pca.result$rotation # 2
But the result I get from # 2
is different from the result I get from # 1
.
How should I correct # 2
so that it gives the same result as # 1
?
Upvotes: 0
Views: 514
Reputation: 11056
This is a bit simpler. Just use the score()
function to compute the standardized data:
pcscores <- scale(USArrests) %*% pca.result$rotation
If you want to use the statistics in pca.result
:
means <- pca.result$center
sdevs <- pca.result$scale
center <- sweep(USArrests, 2, means)
zscores <- sweep(center, 2, sdevs, "/")
pcscores2 <- as.matrix(zscores) %*% pca.result$rotation
Your method subtracts means down each column instead of across columns.
Upvotes: 1