Skip to contents

In this example, the Monte Carlo method is used to generate confidence intervals for the indirect effects in a serial mediation model with two mediators where X is the predictor, M1 is the first mediator, M2 is the second mediator, and Y is the dependent variable.

The Serial Mediation Model
The Serial Mediation Model

Data

summary(df)
#>        X                  M1                 M2                 Y           
#>  Min.   :-3.37174   Min.   :-3.22690   Min.   :-4.33590   Min.   :-4.29019  
#>  1st Qu.:-0.67546   1st Qu.:-0.73709   1st Qu.:-0.82188   1st Qu.:-0.86035  
#>  Median :-0.01313   Median :-0.01651   Median :-0.03903   Median :-0.02704  
#>  Mean   :-0.02582   Mean   :-0.01823   Mean   :-0.01620   Mean   :-0.03338  
#>  3rd Qu.: 0.66400   3rd Qu.: 0.72825   3rd Qu.: 0.80016   3rd Qu.: 0.81721  
#>  Max.   : 3.49530   Max.   : 3.69001   Max.   : 3.65147   Max.   : 4.05239
colMeans(df)
#>           X          M1          M2           Y 
#> -0.02582443 -0.01823021 -0.01619576 -0.03337865
var(df)
#>            X        M1        M2         Y
#> X  1.0050488 0.5123920 0.3848638 0.3333458
#> M1 0.5123920 1.2334461 0.6645408 0.5108946
#> M2 0.3848638 0.6645408 1.4321822 0.8012638
#> Y  0.3333458 0.5108946 0.8012638 1.4504417

Model Specification

We can define several indirect effects in this example:

  • \(X \rightarrow M1 \rightarrow M2 \rightarrow Y\)
  • \(X \rightarrow M1 \rightarrow M2\)
  • \(X \rightarrow M1 \rightarrow Y\)
  • \(M1 \rightarrow M2 \rightarrow Y\)

These indirect effects are defined using the := operator in the lavaan model syntax.

model <- "
  Y ~ cp * X + b1 * M1 + b2 * M2
  M2 ~ a2 * X + k * M1
  M1 ~ a1 * X
  # X -> M1 -> M2 -> Y
  a1kb2 := a1 * k * b2
  # X -> M1 -> M2
  a1k := a1 * k
  # X -> M1 -> Y
  a2b2 := a2 * b2
  # M1 -> M2 -> Y
  kb2 := k * b2
"

Model Fitting

fit <- sem(data = df, model = model)

Monte Carlo Confidence Intervals

The fit lavaan object can then be passed to the MC() function from semmcci to generate Monte Carlo confidence intervals.

MC(fit, R = 20000L, alpha = 0.05)
#> Monte Carlo Confidence Intervals
#>           est     se     R   2.5%  97.5%
#> cp     0.0868 0.0355 20000 0.0169 0.1554
#> b1     0.1190 0.0350 20000 0.0513 0.1880
#> b2     0.4809 0.0305 20000 0.4215 0.5404
#> a2     0.1373 0.0364 20000 0.0660 0.2092
#> k      0.4817 0.0331 20000 0.4166 0.5469
#> a1     0.5098 0.0310 20000 0.4483 0.5700
#> Y~~Y   0.9744 0.0439 20000 0.8885 1.0603
#> M2~~M2 1.0581 0.0477 20000 0.9653 1.1522
#> M1~~M1 0.9712 0.0431 20000 0.8872 1.0563
#> X~~X   1.0040 0.0000 20000 1.0040 1.0040
#> a1kb2  0.1181 0.0132 20000 0.0931 0.1452
#> a1k    0.2456 0.0226 20000 0.2022 0.2908
#> a2b2   0.0660 0.0180 20000 0.0315 0.1022
#> kb2    0.2317 0.0217 20000 0.1902 0.2751

Standardized Monte Carlo Confidence Intervals

Standardized Monte Carlo Confidence intervals can be generated by passing the result of the MC() function to the MCStd() function.

Note: We recommend setting fixed.x = FALSE when generating standardized estimates and confidence intervals to model the variances and covariances of the exogenous observed variables if they are assumed to be random. If fixed.x = TRUE, which is the default setting in lavaan, MC() will fix the variances and the covariances of the exogenous observed variables to the sample values.

fit <- sem(data = df, model = model, fixed.x = FALSE)
unstd <- MC(fit, R = 20000L, alpha = 0.05)
MCStd(unstd, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#>           est     se     R   2.5%  97.5%
#> cp     0.0723 0.0290 20000 0.0159 0.1291
#> b1     0.1098 0.0316 20000 0.0472 0.1710
#> b2     0.4779 0.0276 20000 0.4235 0.5314
#> a2     0.1151 0.0306 20000 0.0545 0.1738
#> k      0.4470 0.0282 20000 0.3909 0.5015
#> a1     0.4602 0.0249 20000 0.4098 0.5080
#> Y~~Y   0.6725 0.0243 20000 0.6230 0.7181
#> M2~~M2 0.7396 0.0238 20000 0.6912 0.7845
#> M1~~M1 0.7882 0.0228 20000 0.7419 0.8320
#> X~~X   1.0000 0.0000 20000 1.0000 1.0000
#> a1kb2  0.0983 0.0104 20000 0.0786 0.1196
#> a1k    0.2057 0.0177 20000 0.1716 0.2414
#> a2b2   0.0550 0.0150 20000 0.0259 0.0846
#> kb2    0.2136 0.0188 20000 0.1777 0.2514

References

Pesigan, I. J. A., & Cheung, S. F. (2023). Monte Carlo confidence intervals for the indirect effect with missing data. Behavior Research Methods. https://doi.org/10.3758/s13428-023-02114-4