-1

I'm doing principal component analysis to reduce number of variables for my regression model with low number of data sets & high number of independent variables (around 40 independent variables). I'm using the function princomp to generate the principal component as I have correlations between independent variables.But I don't know how to use princomp output based on the number of PCA.I'm interested in using a subset of the Principal Components for prediction

Can you please help me?

Thanks in advance

Python123
  • 41
  • 1
  • 2
  • 8

1 Answers1

0
my_pca <- prcomp(data)
summary(my_pca)

Standard deviations in summary are the square roots of the eigenvalues. You can use Kaiser criterion: retain only factors/components with eigenvalues > 1.

pc1 <- my_pca$x[,1] # 1st component
pc2 <- my_pca$x[,2] # 2nd component
...
damian
  • 41
  • 3
  • I'm using princomp,not prcomp as I have correlation matrix.I think $x won't work with princomp.Before posting in this forum,I tried prin_comp <- princomp(covmat=z$correlations) prin_comp$x[,1] but returns NULL – Python123 Dec 07 '16 at 14:39
  • I would try prcomp: http://stats.stackexchange.com/questions/20101/what-is-the-difference-between-r-functions-prcomp-and-princomp – damian Dec 07 '16 at 16:46
  • I don't have option to try prcomp.I have combination of binary & numeric variables so I'm using hetcor to generate correlation & I'm using that correlation in princomp – Python123 Dec 07 '16 at 16:56
  • `prin_comp$scores[,1]` – damian Dec 07 '16 at 17:07
  • Thank you. My summary(prin_comp) is returning Standard deviation,Proportion of Variance,Cumulative Proportion for PCs but don't know why prin_comp$scores[,1] is returning NULL. Can you please help me? – Python123 Dec 07 '16 at 18:19
  • Sry, I don't know. Your principal components are not calculated, because their calculation is normally based on data x, you use only covariance matrix in princomp function. So you have just loadings. If you use loadings and multiplied them by standardized data x, you will get principal components. But I don't know if it makes sense in your case. Anyway, you should try to better describe your problem and ask another question, because this one was marked as duplicate and probably no one is following our conversation. – damian Dec 07 '16 at 23:25