Skip to contents

This function generates the lavaan syntax for a moderated mediation model with optional moderators on the a (x -> m), b (m -> y), and c (x -> y) paths. Optionally, covariates can be added, with effects partialled out from endogeneous (m, y) and exogenous (x, moderators) variables.

Usage

get_moderated_mediation_model(
  x,
  m,
  y,
  mod_a = NULL,
  mod_b = NULL,
  mod_c = NULL,
  values_at = NULL,
  cov = NULL,
  adjust_exogenous = TRUE
)

Arguments

x, m, y

(character) variable name(s)

mod_a, mod_b, mod_c

(character) variable name(s) for moderators of a (x -> m), b (m -> y), and c (x -> y) paths (default NULL)

values_at

list of numeric vectors with values at which to evaluate the conditional effects; names correspond to mod_a, mod_b, and mod_c (default NULL)

cov

(character) variable name(s) of control variables (default NULL)

adjust_exogenous

(logical) adjust exogenous variables (x, mods) for the effect of covariates in cov? (default TRUE)

Value

Character value to be used with lavaan as model syntax

Details

This function is a more flexible alternative to the PROCESS macro (Hayes, 2022), which does not require manual specification of a model number. Also, because the model is estimated in the structural equation modeling framework, both normal-theory p-values and bootstrap intervals are available for indirect effects, and additional parameters can be computed using the := operator in lavaan.

If no moderators are specified, the model corresponds to the mediation model provided in get_mediation_model().

If only one of the a and b paths is moderated, then indexes of moderated mediation are computed (labelled with the i_ prefix in the output). If both the a and b paths are moderated, indexes of moderated mediation are not provided (similar to the PROCESS macro; see Hayes, 2022).

Currently, the number of x, m, y, and a-b-c moderator variables is limited to 9 each. This is not a statistical limitation, but instead due to how the function is written.

References

Hayes, A. F. (2022). Introduction to mediation, moderation, and conditional process analysis (3rd ed.). New York: Guilford.

Examples

if (FALSE) {
library(lavaan)
get_moderated_mediation_model(
     x=c("x1", "x2"),
     m=c("m1", "m2"),
     y=c("y1", "y2", "y3"),
     mod_a=c("Sex","Ethnicity"),
     mod_b=NULL,
     mod_c="Sex",
     values_at=list(Sex=c(0,1), Ethnicity=c(0,1)),
     cov=c("IQ","Age")
   ) |>
   sem(df) |>
   summary()
}