Skip to contents

This function generates the lavaan syntax for a mediation model. The model can take an arbitrary number of predictor (x), mediator (m), and outcome (y) variables.

Usage

get_mediation_model(x, m, y, covariates = NULL)

Arguments

x

Vector of predictor (x) variable names

m

Vector of mediator (m) variable names

y

Vector of outcome (y) variable names

covariates

Vector of covariate (control) variable names (default NULL)

Value

Character value to be used with lavaan as model syntax

Details

This function implements the method used by JASP v0.16.4 to generate a mediation model, possibly adjusting for covariates. Adjustment for covariates is done by partialling out the effect of the control variables given in covariates from all predictor x, mediator m, and outcome y variables.

References

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

Examples

library(lavaan)
#> This is lavaan 0.6-12
#> lavaan is FREE software! Please report any bugs.
get_mediation_model("x1", "x2", "x3") |>
   sem(df) |>
   summary()
#> lavaan 0.6-12 ended normally after 1 iterations
#> 
#>   Estimator                                         ML
#>   Optimization method                           NLMINB
#>   Number of model parameters                         5
#> 
#>   Number of observations                           500
#> 
#> Model Test User Model:
#>                                                       
#>   Test statistic                                 0.000
#>   Degrees of freedom                                 0
#> 
#> Parameter Estimates:
#> 
#>   Standard errors                             Standard
#>   Information                                 Expected
#>   Information saturated (h1) model          Structured
#> 
#> Regressions:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>   x3 ~                                                
#>     x2       (b11)    0.990    0.049   20.397    0.000
#>     x1       (c11)    0.060    0.068    0.875    0.382
#>   x2 ~                                                
#>     x1       (a11)    0.984    0.045   22.000    0.000
#> 
#> Variances:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>    .x3                0.375    0.024   15.811    0.000
#>    .x2                0.318    0.020   15.811    0.000
#> 
#> Defined Parameters:
#>                    Estimate  Std.Err  z-value  P(>|z|)
#>     ind_x1_m1_y1      0.974    0.065   14.957    0.000
#>     ind_x1_y1         0.974    0.065   14.957    0.000
#>     tot_x1_y1         1.034    0.066   15.733    0.000
#>