Skip to contents

This function generates the lavaan syntax for a longitudinal mediation model with three waves. The model uses a predictor x at time 1 (x1), one mediator at times 2 and 3 (m2, m3), and one outcome at times 1, 2, and 3 (y1, y2, y3).

Usage

get_mediation_model_3wave(
  x1,
  m1,
  m2,
  y1,
  y2,
  y3,
  add_resid_correl = FALSE,
  covariates = NULL
)

Arguments

x1

(character) variable name for predictor at time 1

m1, m2

(character) variable names for mediator at times 1-2

y1, y2, y3

(character) variable names for outcome at times 1-3

add_resid_correl

(logical) whether to add residual correlations between variables assessed at the same time (default FALSE)

covariates

(character) covariate (control) variable names (default NULL)

Value

Character value to be used with lavaan as model syntax

Details

The indirect effect is computed as the effect from x1 to m2 to y3. The effect of x1 on m2 is estimated after removing the effects of m1 and y1 on m2, and the effect of m2 on y3 is estimated after removing the effects of y1, y2, x1, and m1 on y3. This corresponds to the model described in Hayes (2022), Figure 14.4(B), p. 570.

Optionally, residual correlations can be estimated between variables assessed at the same time (as is conventional in cross-lagged models). It is also possible to adjust for covariates, which is achieved by partialling out the effect of the control variables given in covariates from x1, m2, and y3 (the variables involved in the indirect effect). This is the method used by JASP v0.16.4 to adjust for covariates in mediation models.

References

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

JASP Team (2022). JASP (Version 0.16.4). Computer software: https://jasp-stats.org/

Examples

if (FALSE) {
library(lavaan)
get_mediation_model_3wave("x1", "m1", "m2", "y1", "y2", "y3") |>
   sem(df) |>
   summary()
}