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 with missing data where variable X has an effect on variable Y, through a mediating variable M.

Data

summary(df)
#>        X                  M                  Y           
#>  Min.   :-3.19956   Min.   :-3.37128   Min.   :-3.61432  
#>  1st Qu.:-0.63268   1st Qu.:-0.70516   1st Qu.:-0.66921  
#>  Median : 0.02823   Median : 0.02825   Median :-0.04833  
#>  Mean   : 0.00269   Mean   :-0.01992   Mean   :-0.01538  
#>  3rd Qu.: 0.65754   3rd Qu.: 0.65240   3rd Qu.: 0.65293  
#>  Max.   : 3.47091   Max.   : 2.93497   Max.   : 3.09950  
#>  NA's   :100        NA's   :100        NA's   :100

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
  X ~~ X
  indirect := a * b
  direct := cp
  total := cp + (a * b)
"

Model Fitting

We can now fit the model using the sem() function from lavaan. We are using missing = "fiml" to handle missing data in lavaan.

fit <- sem(data = df, model = model, missing = "fiml")

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.2335 0.0295 20000  0.1752 0.2913
#> b         0.5113 0.0295 20000  0.4532 0.5680
#> a         0.4809 0.0286 20000  0.4256 0.5368
#> X~~X      1.0591 0.0499 20000  0.9619 1.1574
#> Y~~Y      0.5542 0.0268 20000  0.5019 0.6074
#> M~~M      0.7564 0.0361 20000  0.6851 0.8267
#> Y~1      -0.0127 0.0253 20000 -0.0621 0.0375
#> M~1      -0.0223 0.0292 20000 -0.0804 0.0344
#> X~1       0.0025 0.0338 20000 -0.0645 0.0688
#> indirect  0.2458 0.0203 20000  0.2074 0.2871
#> direct    0.2335 0.0295 20000  0.1752 0.2913
#> total     0.4794 0.0288 20000  0.4217 0.5353

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.

fit <- sem(data = df, model = model, missing = "fiml")
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.2409 0.0298 20000 0.1827 0.2998
#> b         0.5128 0.0269 20000 0.4589 0.5646
#> a         0.4946 0.0254 20000 0.4442 0.5433
#> X~~X      1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y      0.5568 0.0249 20000 0.5078 0.6052
#> M~~M      0.7554 0.0251 20000 0.7048 0.8027
#> indirect -0.0128 0.0188 20000 0.2174 0.2909
#> direct   -0.0222 0.0298 20000 0.1827 0.2998
#> total     0.0024 0.0256 20000 0.4426 0.5435

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