[R] RStudio Exploratory Factor Analysis: write a function that extracts an increasing number of factors
michael matta
mich@el@m@tt@ @ending from unimib@it
Tue Jul 24 05:21:38 CEST 2018
I have been trying to write a function in Rstudio that extracts an
increasing number of latent factors for the EFA and reports fit measures
for each solution in a final table. Below, I pasted what I was able to come
up with.
Unfortunately, it has some critical limitations:
The for loop requires to set the interval of factors that will be
extracted. That is fine but it would be nicer if the function stopped when
it reaches an error message (such as "maximum iteration exceeded",
"convergence not obtained in GPFoblq").
The final table includes an ugly first column with the label "RMSEA" which
is completely useless but I cannot get rid of it.
In general, the for loop might not be the most elegant way to reach the
goal.
library(psych)
library(GPArotation)
library(dplyr)
library(plyr)
library(qgraph)
efas <- list()
for (i in 1:10) {
fitn <- fa(big5, nfactors = i, fm = "pa", rotate = "oblimin",
scores = "regression")
efas[[i]] <- data.frame(fitn$TLI, fitn$RMSEA[1], fitn$rms, fitn$BIC)
%>%
mutate(Factors = i) %>%
dplyr::rename(TLI = fitn.TLI,
RMSEA = fitn.RMSEA.1.,
SRMR = fitn.rms,
BIC = fitn.BIC) %>%
dplyr::select(Factors, TLI, RMSEA, SRMR, BIC)
}
do.call("rbind", efas) %>%
kable()
Thanks for any help!
--
*Michael Matta, **Ph.D.*
Postdoctoral Research Associate, Department of Applied Psychology
408 International Village
360 Huntington Ave
Northeastern University
Boston, MA 02115
[[alternative HTML version deleted]]
More information about the R-help
mailing list