semmcci: Methods (MC)
Ivan Jacob Agaloos Pesigan
2025-01-13
Source:vignettes/methods-mc.Rmd
methods-mc.Rmd
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
model <- "
Y ~ cp * X + b * M
M ~ a * X
indirect := a * b
direct := cp
total := cp + (a * b)
"
Model Fitting
fit <- sem(data = df, model = model)
Monte Carlo Confidence Intervals
unstd <- MC(fit, R = 20000L, alpha = 0.05)
Methods
print(unstd)
#> Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2333 0.0264 20000 0.1819 0.2849
#> b 0.5082 0.0272 20000 0.4553 0.5613
#> a 0.4820 0.0264 20000 0.4302 0.5340
#> 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.0187 20000 0.2094 0.2831
#> direct 0.2333 0.0264 20000 0.1819 0.2849
#> total 0.4782 0.0265 20000 0.4261 0.5306
summary
summary(unstd)
#> Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2333 0.0264 20000 0.1819 0.2849
#> b 0.5082 0.0272 20000 0.4553 0.5613
#> a 0.4820 0.0264 20000 0.4302 0.5340
#> 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.0187 20000 0.2094 0.2831
#> direct 0.2333 0.0264 20000 0.1819 0.2849
#> total 0.4782 0.0265 20000 0.4261 0.5306
coef
coef(unstd)
#> cp b a Y~~Y M~~M X~~X indirect direct
#> 0.2333230 0.5081833 0.4819530 0.5461589 0.7526879 1.0589562 0.2449205 0.2333230
#> total
#> 0.4782435
vcov
vcov(unstd)
#> cp b a Y~~Y M~~M
#> cp 6.975724e-04 -3.570594e-04 1.739434e-06 2.065846e-06 -4.029921e-06
#> b -3.570594e-04 7.379603e-04 -7.450590e-06 1.744389e-06 6.618803e-06
#> a 1.739434e-06 -7.450590e-06 6.982971e-04 8.322624e-06 -5.021764e-06
#> Y~~Y 2.065846e-06 1.744389e-06 8.322624e-06 5.962836e-04 4.504744e-06
#> M~~M -4.029921e-06 6.618803e-06 -5.021764e-06 4.504744e-06 1.148370e-03
#> X~~X 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
#> indirect -1.708943e-04 3.516821e-04 3.515320e-04 4.920373e-06 3.808621e-07
#> direct 6.975724e-04 -3.570594e-04 1.739434e-06 2.065846e-06 -4.029921e-06
#> total 5.266782e-04 -5.377290e-06 3.532714e-04 6.986219e-06 -3.649059e-06
#> X~~X indirect direct total
#> cp 0 -1.708943e-04 6.975724e-04 5.266782e-04
#> b 0 3.516821e-04 -3.570594e-04 -5.377290e-06
#> a 0 3.515320e-04 1.739434e-06 3.532714e-04
#> Y~~Y 0 4.920373e-06 2.065846e-06 6.986219e-06
#> M~~M 0 3.808621e-07 -4.029921e-06 -3.649059e-06
#> X~~X 0 0.000000e+00 0.000000e+00 0.000000e+00
#> indirect 0 3.486997e-04 -1.708943e-04 1.778055e-04
#> direct 0 -1.708943e-04 6.975724e-04 5.266782e-04
#> total 0 1.778055e-04 5.266782e-04 7.044836e-04
confint
confint(unstd, level = 0.95)
#> 2.5 % 97.5 %
#> cp 0.1819362 0.2849104
#> b 0.4552649 0.5613296
#> a 0.4301663 0.5339651
#> Y~~Y 0.4979155 0.5944224
#> M~~M 0.6857891 0.8187138
#> X~~X 1.0589562 1.0589562
#> indirect 0.2093636 0.2830510
#> direct 0.1819362 0.2849104
#> total 0.4261341 0.5305578
Standardized Monte Carlo Confidence Intervals
std <- MCStd(unstd, alpha = 0.05)
Methods
print(std)
#> Standardized Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2422 0.0266 20000 0.1897 0.2941
#> b 0.5123 0.0247 20000 0.4636 0.5605
#> a 0.4963 0.0240 20000 0.4476 0.5416
#> Y~~Y 0.5558 0.0236 20000 0.5089 0.6015
#> 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.2199 0.2891
#> direct 0.2422 0.0266 20000 0.1897 0.2941
#> total 0.4964 0.0239 20000 0.4477 0.5417
summary
summary(std)
#> Standardized Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2422 0.0266 20000 0.1897 0.2941
#> b 0.5123 0.0247 20000 0.4636 0.5605
#> a 0.4963 0.0240 20000 0.4476 0.5416
#> Y~~Y 0.5558 0.0236 20000 0.5089 0.6015
#> 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.2199 0.2891
#> direct 0.2422 0.0266 20000 0.1897 0.2941
#> total 0.4964 0.0239 20000 0.4477 0.5417
coef
coef(std)
#> cp b a Y~~Y M~~M X~~X indirect direct
#> 0.2422015 0.5122827 0.4962890 0.5557501 0.7536972 1.0000000 0.2542403 0.2422015
#> total
#> 0.4964418
vcov
vcov(std)
#> cp b a Y~~Y M~~M
#> cp 7.091899e-04 -4.651025e-04 8.245583e-06 -1.168207e-04 -8.275421e-06
#> b -4.651025e-04 6.083075e-04 2.339914e-05 -3.134309e-04 -2.305571e-05
#> a 8.245583e-06 2.339914e-05 5.769659e-04 -1.801830e-04 -5.706892e-04
#> Y~~Y -1.168207e-04 -3.134309e-04 -1.801830e-04 5.569665e-04 1.782647e-04
#> M~~M -8.275421e-06 -2.305571e-05 -5.706892e-04 1.782647e-04 5.651409e-04
#> X~~X 3.525000e-20 -4.147947e-20 -9.499398e-21 1.914120e-20 8.545081e-21
#> indirect -2.265255e-04 3.136098e-04 3.069966e-04 -2.474505e-04 -3.036457e-04
#> direct 7.091899e-04 -4.651025e-04 8.245583e-06 -1.168207e-04 -8.275421e-06
#> total 4.826644e-04 -1.514927e-04 3.152422e-04 -3.642712e-04 -3.119212e-04
#> X~~X indirect direct total
#> cp 3.525000e-20 -2.265255e-04 7.091899e-04 4.826644e-04
#> b -4.147947e-20 3.136098e-04 -4.651025e-04 -1.514927e-04
#> a -9.499398e-21 3.069966e-04 8.245583e-06 3.152422e-04
#> Y~~Y 1.914120e-20 -2.474505e-04 -1.168207e-04 -3.642712e-04
#> M~~M 8.545081e-21 -3.036457e-04 -8.275421e-06 -3.119212e-04
#> X~~X 1.182796e-32 -2.533127e-20 3.525000e-20 9.918728e-21
#> indirect -2.533127e-20 3.130369e-04 -2.265255e-04 8.651139e-05
#> direct 3.525000e-20 -2.265255e-04 7.091899e-04 4.826644e-04
#> total 9.918728e-21 8.651139e-05 4.826644e-04 5.691757e-04
confint
confint(std, level = 0.95)
#> 2.5 % 97.5 %
#> cp 0.1896616 0.2941122
#> b 0.4635708 0.5604860
#> a 0.4476424 0.5416327
#> Y~~Y 0.5089400 0.6015440
#> M~~M 0.7066340 0.7996163
#> X~~X 1.0000000 1.0000000
#> indirect 0.2198992 0.2890635
#> direct 0.1896616 0.2941122
#> total 0.4477209 0.5416757
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