RStudio Exploratory Factor Analysis: write a function that extracts an increasing number of factors

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

RStudio Exploratory Factor Analysis: write a function that extracts an increasing number of factors

michael matta
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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: RStudio Exploratory Factor Analysis: write a function that extracts an increasing number of factors

Rui Barradas
Hello,

The output of kable() is a character vector, so you can solve your
problem with a regex.


res <- do.call("rbind", efas) %>%
   kable()

res2 <- sub("^\\|[^|]+(\\|.*)", "\\1", res)
head(res2)


Alternatively, a more tidyverse like way would be to pipe the output of
kable() through sub().


do.call("rbind", efas) %>%
   kable() %>%
   sub("^\\|[^|]+(\\|.*)", "\\1", .)


Hope this helps,

Rui Barradas

Às 04:21 de 24-07-2018, michael matta escreveu:

> 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!
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.