I came across PCA analysis, and noticed the different values returned by different functions in R. The intention of this question is to disambiguate the output of each. I didn't find a satisfactory answer as to why these functions return different values. The functions compared are: stats::princomp()
, stats::prcomp()
, psych::principal()
, and FactoMineR::PCA()
. Data set was scaled and centered for sake of comparison and all set to return 4 components, however only the first two PCs are shown here for brevity.
Below is a code of a MWE
to set up the case. Please feel free to report any other function in R that you might see it helpful to compare its output here in one place, I hope.
princompPCA <- princomp(USArrests, cor = TRUE)
prcompPCA <- prcomp(USArrests,scale.=TRUE)
principalPCA <- principal(USArrests, nfactors=4 , scores=TRUE, rotate = "none",scale=TRUE)
library(FactoMineR)
fmrPCA <- PCA(USArrests, ncp=4, graph=FALSE) # vars scaled data
# now the first two PCs from each package into one data frame
dfComp <- cbind.data.frame(princompPCA$scores[,1:2],prcompPCA$x[,1:2],principalPCA$scores[,1:2],fmrPCA$ind$coord[,1:2])
names(dfComp) <- c("princompDim1","princompDim2","prcompDim1","prcompDim2","principalDim1","principalDim2","fmrDim1","fmrDim2")
head(dfComp)
Output:
princompDim1 princompDim2 prcompDim1 prcompDim2 principalDim1 principalDim2 fmrDim1 fmrDim2
Alabama -0.9855659 1.1333924 -0.9756604 1.1220012 0.61951483 -1.1277874 0.9855659 -1.1333924
Alaska -1.9501378 1.0732133 -1.9305379 1.0624269 1.22583308 -1.0679059 1.9501378 -1.0732133
Arizona -1.7631635 -0.7459568 -1.7454429 -0.7384595 1.10830334 0.7422678 1.7631635 0.7459568
Arkansas 0.1414203 1.1197968 0.1399989 1.1085423 -0.08889509 -1.1142591 -0.1414203 -1.1197968
California -2.5239801 -1.5429340 -2.4986128 -1.5274267 1.58654347 1.5353037 2.5239801 1.5429340
Colorado -1.5145629 -0.9875551 -1.4993407 -0.9776297 0.95203595 0.9826713 1.5145629 0.9875551
I noticed that output of stats::princomp()
is exactly the same as FactoMineR::PCA()
except for the inverted signs. Any idea why the signs are mirrored? Both outputs of these two functions are drawing near to the stats::prcomp()
but that may be due to floating point issues, a minor issue. But psych::principal()
is relatively different than others. Could it be due to rotation differences between the mentioned functions? So any explanation for these differences would be much appreciated.