Skip to contents

In this example, the Monte Carlo method is used to generate confidence intervals for the indirect effect in a simple mediation model where variable X has an effect on variable Y, through a mediating variable M. This mediating or indirect effect is a product of path coefficients from the fitted model.

The Simple Mediation Model
The Simple Mediation Model

Data

summary(df)
#>        X                   M                   Y           
#>  Min.   :-3.199558   Min.   :-3.371276   Min.   :-3.61432  
#>  1st Qu.:-0.636035   1st Qu.:-0.692640   1st Qu.:-0.66146  
#>  Median : 0.011377   Median : 0.007125   Median :-0.04726  
#>  Mean   :-0.003207   Mean   :-0.023968   Mean   :-0.01677  
#>  3rd Qu.: 0.651951   3rd Qu.: 0.647363   3rd Qu.: 0.62640  
#>  Max.   : 3.470910   Max.   : 2.963216   Max.   : 3.09950
colMeans(df)
#>            X            M            Y 
#> -0.003206987 -0.023968103 -0.016774294
var(df)
#>           X         M         Y
#> X 1.0600162 0.5108780 0.5069458
#> M 0.5108780 0.9996606 0.6272104
#> Y 0.5069458 0.6272104 0.9837255

Model Specification

The indirect effect is defined by the product of the slopes of paths X to M labeled as a and M to Y labeled as b. In this example, we are interested in the confidence intervals of indirect defined as the product of a and b using the := operator in the lavaan model syntax.

model <- "
  Y ~ cp * X + b * M
  M ~ a * X
  indirect := a * b
  direct := cp
  total := cp + (a * b)
"

Model Fitting

We can now fit the model using the sem() function from lavaan.

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

Monte Carlo Confidence Intervals

The fit lavaan object can then be passed to the MC() function 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.2333 0.0264 20000 0.1817 0.2846
#> b        0.5082 0.0272 20000 0.4551 0.5611
#> a        0.4820 0.0264 20000 0.4299 0.5337
#> Y~~Y     0.5462 0.0244 20000 0.4979 0.5944
#> M~~M     0.7527 0.0339 20000 0.6858 0.8187
#> X~~X     1.0590 0.0000 20000 1.0590 1.0590
#> indirect 0.2449 0.0188 20000 0.2094 0.2831
#> direct   0.2333 0.0264 20000 0.1817 0.2846
#> total    0.4782 0.0264 20000 0.4262 0.5295

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.2422 0.0268 20000 0.1895 0.2942
#> b        0.5123 0.0246 20000 0.4640 0.5609
#> a        0.4963 0.0240 20000 0.4476 0.5416
#> Y~~Y     0.5558 0.0235 20000 0.5094 0.6017
#> M~~M     0.7537 0.0238 20000 0.7066 0.7996
#> X~~X     1.0000 0.0000 20000 1.0000 1.0000
#> indirect 0.2542 0.0177 20000 0.2200 0.2889
#> direct   0.2422 0.0268 20000 0.1895 0.2942
#> total    0.4964 0.0240 20000 0.4483 0.5422

Standardized Monte Carlo Confidence Intervals - An Alternative Approach

In this example, confidence intervals for the standardized indirect effect are generated by specifying the standardized indirect effect as a derived parameter using the := operator. The standardized indirect effect in a simple mediation model involves paths aa and bb, and the standard deviations of XX and YY. It is given by

absXsY a b \frac{s_X}{s_Y}

where

sX=sX2 s_X = \sqrt{ s_{X}^{2} }

and

sY=c2sX2+a2b2sX2+b2seM2+2cbasX2+seY2 s_Y = \sqrt{ c^{\prime 2} s_{X}^{2} + a^2 b^2 s_{X}^{2} + b^2 s_{e_{M}}^{2} + 2 c^{\prime} b a s_{X}^{2} + s_{e_{Y}}^{2} }

where

seY2s_{e_{Y}}^{2} and seM2s_{e_{M}}^{2} are the residual variances in the regression equations.

The standardized indirect effect can be defined using the := operator and the named parameters in the model.

model <- "
  Y ~ cp * X + b * M
  M ~ a * X
  X ~~ s2_X * X
  M ~~ s2_em * M
  Y ~~ s2_ey * Y
  indirect_std := a * b * (sqrt(s2_X) / sqrt((cp^2 * s2_X + a^2 * b^2 * s2_X) + (b^2 * s2_em) + (2 * cp * b * a * s2_X) + s2_ey))
"
fit <- sem(data = df, model = model, fixed.x = FALSE)

The row indirect_std corresponds to the confidence intervals for the standardized indirect effect.

MC(fit, R = 20000L, alpha = 0.05)
#> Monte Carlo Confidence Intervals
#>                 est     se     R   2.5%  97.5%
#> cp           0.2333 0.0262 20000 0.1818 0.2844
#> b            0.5082 0.0271 20000 0.4562 0.5618
#> a            0.4820 0.0264 20000 0.4309 0.5338
#> s2_X         1.0590 0.0472 20000 0.9675 1.1531
#> s2_em        0.7527 0.0339 20000 0.6865 0.8192
#> s2_ey        0.5462 0.0243 20000 0.4983 0.5936
#> indirect_std 0.2542 0.0174 20000 0.2208 0.2886

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