Skip to contents

This function generates the lavaan syntax for a two-level mediation model. The model takes one predictor (x), one mediator (m), and one outcome (y). The mediation model is either reproduced at both the within (level 1) and between (level 2) levels, or the level-2 model is empty (corresponding to random-intercept models in multilevel modeling). When the mediation is estimated at both levels, a test of the difference between the level-1 and level-2 indirect effects is provided.

Usage

get_mediation_model_2level(x, m, y, empty_level2 = FALSE)

Arguments

x, m, y

(character) variable names

empty_level2

(logical) should the level-2 model be left empty? (default FALSE; see Details)

Value

Character value to be used with lavaan as model syntax

Details

This model is for fully-nested designs and is described in Lachowicz, Sterba, & Preacher (2015). The empty model at level 2 is saturated with covariances between endogeneous variables, yielding a model similar to random-intercept models in multilevel modeling.

References

Lachowicz, M. J., Sterba, S. K., & Preacher, K. J. (2015). Investigating multilevel mediation with fully or partially nested data. Group Processes & Intergroup Relations, 18, 274–289. https://doi.org/10.1177/1368430214550343.

Examples

if (FALSE) {
library(lavaan)
get_mediation_model_2level("x1", "x2", "x3") |>
   sem(df, group="id") |>
   summary()
}