R EFA syntax

View previous topic View next topic Go down

R EFA syntax

Post by pvincentruz on Tue Sep 13, 2016 3:13 pm

This is the syntax I use to conduct EFA in R


Code:
## Pricipal Components Analysis
# entering raw data and extracting PCs 
# from the correlation matrix 
fit <- princomp(na.omit(x), cor=TRUE)
summary(fit) # print variance accounted for 
loadings(fit) # pc loadings 

# Determine Number of Factors to Extract
library(nFactors)
ev <- eigen(cor(na.omit(x))) # get eigenvalues
ap <- parallel(subject=nrow(x),var=ncol(x),
               rep=100,cent=.05)
#Get Screeplot
nS <- nScree(x=ev$values, aparallel=ap$eigen$qevpea)


plotnScree(nS)

#ONCE YOU KNOW HOW MANY FACTORS TO EXTRACT
# Maximum Likelihood Factor Analysis
# entering raw data and extracting n factors,
# with promax rotation
#EFA1
#substitue n for the number of factors to extract
fit2 <- factanal(na.omit(dataEFA), n, rotation="promax")


print(fit2, digits=2, sort=FALSE
avatar
pvincentruz

Posts : 2
Reputation : 0

View user profile

Back to top Go down

Re: R EFA syntax

Post by Jeffrey Girard on Wed Sep 14, 2016 12:17 am

Thanks for sharing! One quick note: your syntax includes both PCA and EFA. If all you want is EFA, the first part isn't necessary.

Code:
# Maximum Likelihood Factor Analysis
# entering raw data and extracting 3 factors,
# with varimax rotation
fit <- factanal(mydata, 3, rotation="varimax")
print(fit, digits=2, cutoff=.3, sort=TRUE)
# plot factor 1 by factor 2
load <- fit$loadings[,1:2]
plot(load,type="n") # set up plot
text(load,labels=names(mydata),cex=.7) # add variable names
# Determine Number of Factors to Extract
library(nFactors)
ev <- eigen(cor(mydata)) # get eigenvalues
ap <- parallel(subject=nrow(mydata),var=ncol(mydata),
  rep=100,cent=.05)
nS <- nScree(x=ev$values, aparallel=ap$eigen$qevpea)
plotnScree(nS)
avatar
Jeffrey Girard
Admin

Posts : 16
Reputation : 0
Location : 4325 Sennott Sq

View user profile http://www.jmgirard.com

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum