Skip to contents

In this example, the Monte Carlo method is used to generate confidence intervals for the indirect effects in a simple mediation model with latent variables. X, M, and Y are latent variables with three indicators each where X is the predictor, M is the mediator, and Y is the dependent variable.

The Simple Mediation Model with Latent Variables

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 <- "
  X =~ x1 + x2 + x3
  M =~ x4 + x5 + x6
  Y =~ x7 + x8 + x9
  M ~ a * X
  Y ~ b * M
  indirect := a * b
"

Model Fitting

We can now fit the model using the sem() function from lavaan using the Holzinger and Swineford (1939) data set.

df <- lavaan::HolzingerSwineford1939
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 = c(0.001, 0.01, 0.05))
#> Monte Carlo Confidence Intervals
#>             est     se     R  0.05%   0.5%   2.5%  97.5%  99.5% 99.95%
#> X=~x2    0.5554 0.1043 20000 0.2050 0.2883 0.3541 0.7596 0.8187 0.8803
#> X=~x3    0.7045 0.1172 20000 0.3322 0.4086 0.4743 0.9337 1.0108 1.0803
#> M=~x5    1.1106 0.0652 20000 0.8961 0.9447 0.9836 1.2401 1.2794 1.3185
#> M=~x6    0.9268 0.0555 20000 0.7502 0.7858 0.8181 1.0367 1.0689 1.1036
#> Y=~x8    1.1482 0.1646 20000 0.6146 0.7206 0.8211 1.4671 1.5655 1.6692
#> Y=~x9    0.8854 0.1241 20000 0.4753 0.5562 0.6443 1.1294 1.1984 1.2803
#> a        0.5107 0.0955 20000 0.2049 0.2677 0.3257 0.6993 0.7575 0.8179
#> b        0.1884 0.0517 20000 0.0191 0.0557 0.0882 0.2895 0.3222 0.3584
#> x1~~x1   0.5320 0.1299 20000 0.1112 0.1988 0.2765 0.7885 0.8718 0.9663
#> x2~~x2   1.1269 0.1033 20000 0.7807 0.8602 0.9252 1.3283 1.3879 1.4538
#> x3~~x3   0.8647 0.0951 20000 0.5630 0.6204 0.6762 1.0503 1.1057 1.1621
#> x4~~x4   0.3714 0.0473 20000 0.2119 0.2488 0.2780 0.4634 0.4913 0.5273
#> x5~~x5   0.4519 0.0586 20000 0.2651 0.3025 0.3387 0.5680 0.6069 0.6477
#> x6~~x6   0.3551 0.0423 20000 0.2183 0.2470 0.2717 0.4376 0.4629 0.4954
#> x7~~x7   0.7309 0.0841 20000 0.4523 0.5114 0.5656 0.8946 0.9518 1.0066
#> x8~~x8   0.4257 0.0825 20000 0.1588 0.2143 0.2650 0.5876 0.6440 0.6955
#> x9~~x9   0.6605 0.0705 20000 0.4277 0.4742 0.5237 0.7984 0.8449 0.8888
#> X~~X     0.8264 0.1590 20000 0.3182 0.4210 0.5153 1.1387 1.2487 1.3546
#> M~~M     0.7638 0.0971 20000 0.4539 0.5112 0.5713 0.9541 1.0127 1.0691
#> Y~~Y     0.4175 0.0893 20000 0.1293 0.1903 0.2409 0.5899 0.6488 0.7319
#> indirect 0.0962 0.0319 20000 0.0097 0.0253 0.0397 0.1644 0.1928 0.2228

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)
unstd <- MC(fit, R = 20000L, alpha = c(0.001, 0.01, 0.05))
MCStd(unstd)
#> Standardized Monte Carlo Confidence Intervals
#>             est     se     R  0.05%   0.5%   2.5%  97.5%  99.5% 99.95%
#> X=~x2    0.4295 0.0613 20000 0.1977 0.2528 0.2967 0.5392 0.5698 0.6036
#> X=~x3    0.5672 0.0601 20000 0.3227 0.3831 0.4324 0.6702 0.6967 0.7254
#> M=~x5    0.8531 0.0228 20000 0.7729 0.7881 0.8037 0.8941 0.9061 0.9177
#> M=~x6    0.8385 0.0235 20000 0.7536 0.7722 0.7883 0.8802 0.8930 0.9052
#> Y=~x8    0.7639 0.0551 20000 0.5436 0.5957 0.6408 0.8580 0.8841 0.9172
#> Y=~x9    0.5910 0.0547 20000 0.3822 0.4356 0.4735 0.6879 0.7161 0.7504
#> a        0.4691 0.0646 20000 0.2228 0.2855 0.3301 0.5840 0.6184 0.6542
#> b        0.2772 0.0700 20000 0.0208 0.0895 0.1350 0.4110 0.4568 0.5178
#> x1~~x1   0.3917 0.0965 20000 0.0905 0.1546 0.2066 0.5878 0.6536 0.7349
#> x2~~x2   0.8155 0.0515 20000 0.6357 0.6753 0.7093 0.9120 0.9361 0.9609
#> x3~~x3   0.6783 0.0663 20000 0.4738 0.5145 0.5509 0.8130 0.8533 0.8959
#> x4~~x4   0.2750 0.0389 20000 0.1620 0.1835 0.2040 0.3569 0.3831 0.4178
#> x5~~x5   0.2722 0.0387 20000 0.1579 0.1789 0.2006 0.3541 0.3788 0.4026
#> x6~~x6   0.2969 0.0392 20000 0.1806 0.2025 0.2253 0.3786 0.4037 0.4320
#> x7~~x7   0.6177 0.0665 20000 0.4130 0.4490 0.4895 0.7518 0.7949 0.8544
#> x8~~x8   0.4165 0.0826 20000 0.1588 0.2183 0.2639 0.5894 0.6451 0.7045
#> x9~~x9   0.6507 0.0636 20000 0.4369 0.4872 0.5268 0.7758 0.8103 0.8539
#> X~~X     1.0000 0.0000 20000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
#> M~~M     0.7799 0.0593 20000 0.5720 0.6176 0.6590 0.8910 0.9185 0.9503
#> Y~~Y     0.9231 0.0390 20000 0.7319 0.7913 0.8311 0.9818 0.9920 0.9994
#> indirect 0.1301 0.0378 20000 0.0105 0.0379 0.0580 0.2076 0.2368 0.2762