Structural Equation Modelling and Path Anaysis in R lavaan

Structural equation models (SEM) are a powerful tool for analyzing complex relationships between observed and latent variables. The R package lavaan (latent variable analysis) provides a user-friendly yet powerful environment for SEM in R. In this post, I’ll show you how to get started with lavaan – from installation to model interpretation.

Why lavaan?

Compared with commercial tools such as AMOS or Mplus, lavaan is:

  • Free and open source: Ideal for research and teaching.
  • Intuitive: The syntax is reminiscent of R formulas or Mplus.
  • Flexible: Supports CFA, SEM, growth models, mediation analyses, and more.
  • Reproducible: Perfectly integrates into R workflows, including tidyverse and ggplot2.

Installation

Make sure you have a current version of R (≥ 4.0.0) installed. You can then install lavaan directly from the CRAN repository:

install.packages("lavaan")
library(lavaan)

A first test with an example model:

example(cfa)

The lavaan syntax: explained quickly

Models are defined as character strings in lavaan. Specifically:

  • =~ is used for measurement models (e.g., latent variables),
  • ~ is used for regression relationships,
  • ~~ is used for covariances.

A simple example of a confirmatory factor analysis (CFA):

HS.model <- '
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
'
fit <- cfa(HS.model, data = HolzingerSwineford1939)
summary(fit, fit.measures = TRUE)

This model is based on the well-known Holzinger-Swineford dataset. The cfa() function fits the model to the data, while summary() provides a comprehensive output with estimates and fit indices.

Understanding Fit Indices

lavaan provides various fit measures for assessing the quality of model fit:

  • Chi-square test: Tests the null hypothesis that the model fits perfectly.
  • CFI (Comparative Fit Index): Values > 0.95 are considered a good fit.
  • RMSEA (Root Mean Square Error of Approximation): Values < 0.06 indicate a good fit.
  • SRMR (Standardized Root Mean Square Residual): Values < 0.08 are acceptable.

These indices help you critically assess how well your model fits the data.

Advanced Modeling Options

In addition to CFA, lavaan supports:

  • Structural equation models (SEM): Combining measurement and structural models.
  • Growth models: Analyzing changes over time.
  • Mediation analyses: Investigating indirect effects.
  • Multiple-group comparisons: Testing invariance across groups.
  • Handling missing data: e.g., using FIML (Full Information Maximum Likelihood).

An example of a mediation analysis:

model <- '
M ~ a*X
Y ~ b*M + c*X
ab := a*b
total := c + (a*b)
'
fit <- sem(model, data = myData)
summary(fit, standardized = TRUE)

This calculates the indirect effect ab and the total effect total.

Visualizing models

For the graphical representation of SEMs, you can use the semPlot package:

rCopyEditinstall.packages("semPlot")
library(semPlot)
semPaths(fit, "std", layout = "tree", whatLabels = "std")

This creates a clear path diagram of your model with standardized coefficients.

Conclusion

lavaan is a powerful yet user-friendly package for structural equation modeling in R. With its clear syntax and integration into the R ecosystem, it is excellent for research and teaching. Whether you want to analyze simple CFA models or complex SEMs with mediation and moderation effects, lavaan provides you with the tools you need.

You can find more information and the official tutorial here:

???? lavaan Tutorial