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:

  • XM1M2YX \rightarrow M1 \rightarrow M2 \rightarrow Y
  • XM1M2X \rightarrow M1 \rightarrow M2
  • XM1YX \rightarrow M1 \rightarrow Y
  • M1M2YM1 \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.0367 20000 0.0649 0.2090
#> k      0.4817 0.0328 20000 0.4172 0.5460
#> 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.0938 0.1451
#> a1k    0.2456 0.0226 20000 0.2028 0.2913
#> a2b2   0.0660 0.0181 20000 0.0308 0.1020
#> kb2    0.2317 0.0215 20000 0.1909 0.2748

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.0291 20000 0.0148 0.1291
#> b1     0.1098 0.0317 20000 0.0485 0.1722
#> b2     0.4779 0.0273 20000 0.4232 0.5309
#> 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.6231 0.7179
#> 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.1198
#> a1k    0.2057 0.0177 20000 0.1716 0.2414
#> a2b2   0.0550 0.0150 20000 0.0259 0.0843
#> kb2    0.2136 0.0187 20000 0.1777 0.2509

References

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