Search code examples
rpcar-caret

PCA in R using the caret package vs prcomp PCA


I have a dataframe data with more than 50 variables and I am trying to do a PCA in R using the caret package.

library(caret)
library(e1071)
trans <- preProcess(data,method=c("YeoJohnson", "center","scale", "pca"))

If I understand this code correctly, it applies a YeoJohnson transformation (because data has zeros in it), standardises data and than applies PCA (by default, the function keeps only the PCs that are necessary to explain at least 95% of the variability in the data)

However, when I use the prcomp command,

  model<-prcomp(data,scale=TRUE)

I can get more outputs like printing the summary or doing plot(data, type = "l") which I am not able to do in trans. Does anyone know if there are any functions in caret package producing the same outputs as in prcomp?


Solution

  • You can access the principal components themselves with the predict function.

    df <- predict(trans, data)
    summary(df)
    

    You won't have exactly the same output as with prcomp: while caret uses prcomp(), it discards the original prcomp class object and does not return it.