How to run EFA and CFA analysis in R
Here are the basic steps for running EFA and CFA in R using the psych and lavaan packages, respectively.
Exploratory Factor Analysis (EFA) using the psych package:
Sample EFA Output
Firstly, make sure you have the necessary packages installed. You can install them by running:
install.packages("psych")
install.packages("lavaan")
EFA Example:
# Load the required library library(psych)
# Example dataset (replace with your own dataset) data <- read.csv("your_data.csv")
# Run EFA efa_result <- fa(data, nfactors = 3, rotate = "varimax")
# Change nfactors according to your analysis
# View the factor loadings print(efa_result$loadings)
Confirmatory Factor Analysis (CFA) using the lavaan package:
# Load the required library library(lavaan)
# Example dataset (replace with your own dataset) data <- read.csv("your_data.csv")
# Define the CFA model cfa_model <- ' # Define your model here using syntax from lavaan latent_variable =~ observed_item1 + observed_item2 + observed_item3 # Add more variables and relationships as needed '
# Fit the CFA model cfa_result <- lavaan::cfa(cfa_model, data = data, estimator = "ML")
# Change estimator as needed
# Summarize the results summary(cfa_result, fit.measures = TRUE)
Comments