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

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Tue Jul 24 07:31:21 CEST 2018


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




More information about the R-help mailing list