Skip to content

Instantly share code, notes, and snippets.

@drsimonj
Created April 17, 2018 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drsimonj/33fbe72e998fd6d5a4bd0aad58cebabe to your computer and use it in GitHub Desktop.
Save drsimonj/33fbe72e998fd6d5a4bd0aad58cebabe to your computer and use it in GitHub Desktop.
EFA in R
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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment