Example 2: The Serial Mediation Model
Ivan Jacob Agaloos Pesigan
2026-03-01
Source:vignettes/example-2-serial.Rmd
example-2-serial.RmdIn 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.

Data
summary(df)
#> X M1 M2 Y
#> Min. :-3.37174 Min. :-3.22690 Min. :-4.33590 Min. :-4.29020
#> 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.66401 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.4504417Model Specification
We can define several indirect effects in this example:
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.0657 0.2098
#> k 0.4817 0.0328 20000 0.4175 0.5463
#> 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.9641 1.1510
#> M1~~M1 0.9712 0.0431 20000 0.8862 1.0553
#> X~~X 1.0040 0.0000 20000 1.0040 1.0040
#> a1kb2 0.1181 0.0132 20000 0.0936 0.1451
#> a1k 0.2456 0.0224 20000 0.2034 0.2909
#> a2b2 0.0660 0.0181 20000 0.0310 0.1027
#> kb2 0.2317 0.0217 20000 0.1909 0.2754Standardized 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 = FALSEwhen generating standardized estimates and confidence intervals to model the variances and covariances of the exogenous observed variables if they are assumed to be random. Iffixed.x = TRUE, which is the default setting inlavaan,MC()will fix the variances and the covariances of the exogenous observed variables to the sample values.
MCStd(unstd, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.0723 0.0291 20000 0.0147 0.1291
#> b1 0.1098 0.0317 20000 0.0485 0.1723
#> b2 0.4779 0.0273 20000 0.4234 0.5309
#> a2 0.1151 0.0307 20000 0.0546 0.1736
#> k 0.4470 0.0281 20000 0.3911 0.5016
#> a1 0.4602 0.0249 20000 0.4108 0.5084
#> Y~~Y 0.6725 0.0242 20000 0.6230 0.7175
#> M2~~M2 0.7396 0.0237 20000 0.6920 0.7846
#> M1~~M1 0.7882 0.0229 20000 0.7415 0.8312
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> a1kb2 0.0983 0.0104 20000 0.0787 0.1194
#> a1k 0.2057 0.0175 20000 0.1717 0.2405
#> a2b2 0.0550 0.0150 20000 0.0259 0.0845
#> kb2 0.2136 0.0187 20000 0.1777 0.2509