Home > Mobile >  PCA in R with prcomp
PCA in R with prcomp

Time:12-09

I am trying to compute an index with PCA. I am using 'mtcars' as my dataset. This is my code:

 pca_mt<- prcomp(mtcars[,c(1:7,10,11)], center = TRUE,scale. = TRUE)

How do I get the weights for each variable to sum them up to compute an index? Appreciate if someone can explain about what pca_mt$rotation , pca_mt$x are.

CodePudding user response:

Maybe this helps you to understand better what pca_mt$rotation, pca_mt$x are:

data <- mtcars[,c(1:7,10,11)]
pca_mt<- prcomp(data, center = F,scale. = F)
sum(data[1,] * pca_mt$rotation[,1]) == pca_mt$x[1,1]
[1] TRUE

Note that this only works because center=F and scale.=F.

For further reading, I recommend https://builtin.com/data-science/step-step-explanation-principal-component-analysis to get a first idea of what PCA is.

So, to get to know how much variance can be explained with the PCA components, you can use

summary(pca_mt)
Importance of components:
                            PC1      PC2      PC3     PC4     PC5     PC6    PC7    PC8    PC9
Standard deviation     310.1169 40.88309 15.83774 2.12821 0.99854 0.74271 0.4412 0.2913 0.2168
Proportion of Variance   0.9803  0.01704  0.00256 0.00005 0.00001 0.00001 0.0000 0.0000 0.0000
Cumulative Proportion    0.9803  0.99738  0.99993 0.99998 0.99999 1.00000 1.0000 1.0000 1.0000

The first component explaines 98,03% of variance in the data, the next one (98,03%-99,738%) and so on. So, using the first component you can explain almost all of the variance in your data.

  •  Tags:  
  • r
  • Related