Skip to content

Instantly share code, notes, and snippets.

@drsimonj
Created April 17, 2018 09:01
Show Gist options
  • Select an option

  • Save drsimonj/33fbe72e998fd6d5a4bd0aad58cebabe to your computer and use it in GitHub Desktop.

Select an option

Save drsimonj/33fbe72e998fd6d5a4bd0aad58cebabe to your computer and use it in GitHub Desktop.

Revisions

  1. drsimonj created this gist Apr 17, 2018.
    27 changes: 27 additions & 0 deletions efa.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    library(tidyverse)
    library(lavaan)
    library(semTools)

    # Function to fit unrotated EFA with specific number of factors
    fit_unrotated_efa <- function(n_factors) {
    data %>% efaUnrotate(nf = n_factors, estimator = "mlr")
    }

    # Fit EFAs (unrotated) with a range of factors
    efa_results <- tibble(n_factors = seq(2, 6)) %>%
    mutate(unrotated_fit = map(n_factors, fit_unrotated_efa))

    # Compute fit indices
    fit_indices <- efa_results %>%
    mutate(indices = map(unrotated_fit, fitmeasures)) %>%
    mutate(indices = map(indices, broom::tidy)) %>%
    unnest(indices) %>%
    spread(names, x)

    # Extract specific fit indices per factor number
    fit_indices %>% select(n_factors, rmsea, cfi, tli)

    # Get rotated model results for specific fit
    rotated_fit <- oblqRotate(efa_results$unrotated_fit[[1]], method = "geomin")
    rotated_fit
    summary(rotated_fit, "std")