semmcci: Methods (MC)
Ivan Jacob Agaloos Pesigan
2024-10-22
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.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
summary
summary(unstd)
#> 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
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.957832e-04 -3.569537e-04 -7.335055e-06 -1.863172e-06 -6.369651e-06
#> b -3.569537e-04 7.397495e-04 2.175751e-06 -1.959388e-06 4.413204e-06
#> a -7.335055e-06 2.175751e-06 6.982971e-04 -8.322624e-06 5.021764e-06
#> Y~~Y -1.863172e-06 -1.959388e-06 -8.322624e-06 5.962836e-04 4.504744e-06
#> M~~M -6.369651e-06 4.413204e-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.760619e-04 3.579324e-04 3.559548e-04 -5.050361e-06 4.668854e-06
#> direct 6.957832e-04 -3.569537e-04 -7.335055e-06 -1.863172e-06 -6.369651e-06
#> total 5.197213e-04 9.786539e-07 3.486198e-04 -6.913533e-06 -1.700796e-06
#> X~~X indirect direct total
#> cp 0 -1.760619e-04 6.957832e-04 5.197213e-04
#> b 0 3.579324e-04 -3.569537e-04 9.786539e-07
#> a 0 3.559548e-04 -7.335055e-06 3.486198e-04
#> Y~~Y 0 -5.050361e-06 -1.863172e-06 -6.913533e-06
#> M~~M 0 4.668854e-06 -6.369651e-06 -1.700796e-06
#> X~~X 0 0.000000e+00 0.000000e+00 0.000000e+00
#> indirect 0 3.540809e-04 -1.760619e-04 1.780190e-04
#> direct 0 -1.760619e-04 6.957832e-04 5.197213e-04
#> total 0 1.780190e-04 5.197213e-04 6.977403e-04
confint
confint(unstd, level = 0.95)
#> 2.5 % 97.5 %
#> cp 0.1816910 0.2845601
#> b 0.4551497 0.5611403
#> a 0.4299410 0.5337397
#> Y~~Y 0.4979155 0.5944224
#> M~~M 0.6857891 0.8187138
#> X~~X 1.0589562 1.0589562
#> indirect 0.2094303 0.2831318
#> direct 0.1816910 0.2845601
#> total 0.4262403 0.5294621
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.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
summary
summary(std)
#> 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
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.191848e-04 -4.667519e-04 6.123858e-06 -1.242838e-04 -6.255887e-06
#> b -4.667519e-04 6.043972e-04 2.231523e-05 -3.063863e-04 -2.203165e-05
#> a 6.123858e-06 2.231523e-05 5.769659e-04 -1.767496e-04 -5.706892e-04
#> Y~~Y -1.242838e-04 -3.063863e-04 -1.767496e-04 5.545463e-04 1.749864e-04
#> M~~M -6.255887e-06 -2.203165e-05 -5.706892e-04 1.749864e-04 5.651409e-04
#> X~~X 3.417802e-20 -4.198085e-20 -1.927269e-20 2.384949e-20 1.827301e-20
#> indirect -2.284289e-04 3.111062e-04 3.064863e-04 -2.421852e-04 -3.031584e-04
#> direct 7.191848e-04 -4.667519e-04 6.123858e-06 -1.242838e-04 -6.255887e-06
#> total 4.907559e-04 -1.556457e-04 3.126101e-04 -3.664690e-04 -3.094143e-04
#> X~~X indirect direct total
#> cp 3.417802e-20 -2.284289e-04 7.191848e-04 4.907559e-04
#> b -4.198085e-20 3.111062e-04 -4.667519e-04 -1.556457e-04
#> a -1.927269e-20 3.064863e-04 6.123858e-06 3.126101e-04
#> Y~~Y 2.384949e-20 -2.421852e-04 -1.242838e-04 -3.664690e-04
#> M~~M 1.827301e-20 -3.031584e-04 -6.255887e-06 -3.094143e-04
#> X~~X 1.183659e-32 -3.060614e-20 3.417802e-20 3.571876e-21
#> indirect -3.060614e-20 3.115388e-04 -2.284289e-04 8.310990e-05
#> direct 3.417802e-20 -2.284289e-04 7.191848e-04 4.907559e-04
#> total 3.571876e-21 8.310990e-05 4.907559e-04 5.738658e-04
confint
confint(std, level = 0.95)
#> 2.5 % 97.5 %
#> cp 0.1894519 0.2941807
#> b 0.4640480 0.5609003
#> a 0.4476424 0.5416327
#> Y~~Y 0.5094295 0.6016893
#> M~~M 0.7066340 0.7996163
#> X~~X 1.0000000 1.0000000
#> indirect 0.2199734 0.2889152
#> direct 0.1894519 0.2941807
#> total 0.4482508 0.5421569
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