Des Grieux
Des Grieux

Reputation: 570

retrieving observation scores for each Principal Component in R

I performed Principal Component Analysis on a data frame to reduce the number of predictors in my linear regression analysis.

pr.out = prcomp(df, scale=TRUE)
pr.var = pr.out$sdev^2
pve = pr.var/sum(pr.var)

Having done that, I would like to substitute the old variables in my data frame with the first few Principal Components that explain the greatest proportion of variance. But, for the life of me, I cannot find the vector of observations for each Principal Component (i.e. the PC1 score for each data point). Do you know how I can access them? I tried pr.out$PC1 for example, but that returns NULL.

Upvotes: 5

Views: 8315

Answers (1)

Des Grieux
Des Grieux

Reputation: 570

Found it! You can access the scores of all Principal components with the following command:

pr.out$x

This will give you the scores for all the components. To access the scores of a single component you need to select the respective column with all rows. For example, to access the scores for the first Principal Component you use:

pr.out$x[,1]

Upvotes: 7

Related Questions