0

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:

  1. 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").

  2. [SOLVED] The final table includes an ugly first column with the label "RMSEA" which is completely useless but I cannot get rid of it.

  3. 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(knitr)
    
     efas <- list()
    
     for (i in 1:10) {
         fitn <- fa(bfi, 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)
    
         }
    
      d <- do.call("rbind", efas) %>%
           kable()%>%
           sub("^\\|[^|]+(\\|.*)", "\\1", .)
    
      d
    

Thanks for any help!

Michael Matta
  • 394
  • 2
  • 16
  • 1
    Could you work on making your example reproducible, that is, essentially, we should be able to copy-paste your code in a fresh R session, and run it? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Thanks :) (hints: `dput(db)`, definition or package of `fa`, other necessary `library()` calls ...) – Aurèle Jul 20 '18 at 17:17
  • 1
    Thanks, Aurele for your comment! I picked one random database from R and now the function can be run. Looking forward to reading your feedback! – Michael Matta Jul 20 '18 at 20:39
  • 1
    Thanks! What is `big5`? – Aurèle Jul 23 '18 at 16:33
  • big5 is a dataset contained in qgraph package. It's just a random dataset on which you can run an EFA. – Michael Matta Jul 23 '18 at 19:08
  • I edited the original question because the data set could not be identified for some reasons. It should work now. I also added another line that solved my second question. – Michael Matta Jul 24 '18 at 17:39
  • 1
    @Aurèle I don't know why the big5 data set didn't work. I should have solved that issue thanks to the bfi data set which contains 25 Personality items representing 5 factors. – Michael Matta Jul 27 '18 at 21:03

0 Answers0