Search code examples
rpca

PCA in R: how to determine contribution of each variable to a PC score


I'm performing a PCA in R, like below.

# Load data
data(mtcars)

# Run PCA
car.pca <- prcomp(mtcars, scale = TRUE, center = TRUE)

I get the PC scores for each car by using car.pca$x. So, for example, I know that for Mazda RX4 the PC1 value is -0.6468627420. What I'd like to know is, how do I calculate the contribution of each variable to achieving this value? I know that car.pca$rotation will give me the variable loadings. So, I'd expect something like mtcars[1,] * car.pca$rotation[, 1] would work (i.e., the loadings for PC1 multiplied by the data for the Mazda RX4), however, I don't think this would account for the fact that the data were centred and scaled by the prcomp function. How would I do the calculation while accounting for centring and scaling?


Solution

  • car.pca$rotation[, 1] * (mtcars[1,] - summary(car.pca)$center) / summary(car.pca)$scale