Ivan Jacob Agaloos Pesigan 2026-03-01
You can install the CRAN release of semmcci with:
install.packages("semmcci")You can install the development version of semmcci from
GitHub with:
if (!require("pak")) install.packages("pak")
pak::pkg_install("jeksterslab/semmcci")In the Monte Carlo method, a sampling distribution of parameter estimates is generated from the multivariate normal distribution using the parameter estimates and the sampling variance-covariance matrix. Confidence intervals for defined parameters are generated by obtaining percentiles corresponding to 100(1 - α)% from the generated sampling distribution, where α is the significance level.
Monte Carlo confidence intervals for free and defined parameters in
models fitted in the structural equation modeling package lavaan can
be generated using the semmcci package. The package has three main
functions, namely, MC(), MCMI(), and MCStd(). The output of
lavaan is passed as the first argument to the MC() function or the
MCMI() function to generate Monte Carlo confidence intervals. Monte
Carlo confidence intervals for the standardized estimates can also be
generated by passing the output of the MC() function or the MCMI()
function to the MCStd() function. A description of the package and
code examples are presented in Pesigan and Cheung (2023:
https://doi.org/10.3758/s13428-023-02114-4).
A common application of the Monte Carlo method is to generate confidence
intervals for the indirect effect. In the simple mediation model,
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.
library(semmcci)
library(lavaan)summary(df)
#> X M Y
#> Min. :-3.08783 Min. :-2.889916 Min. :-3.213309
#> 1st Qu.:-0.68015 1st Qu.:-0.670510 1st Qu.:-0.694303
#> Median : 0.05853 Median : 0.004282 Median : 0.020578
#> Mean : 0.01914 Mean : 0.008340 Mean :-0.004127
#> 3rd Qu.: 0.69842 3rd Qu.: 0.723521 3rd Qu.: 0.640903
#> Max. : 3.84263 Max. : 2.642671 Max. : 2.935465
#> NA's :100 NA's :100 NA's :100The 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)
"We can now fit the model using the sem() function from lavaan. We
use full-information maximum likelihood to deal with missing values.
fit <- sem(data = df, model = model, missing = "fiml")The fit lavaan object can then be passed to the MC() function to
generate Monte Carlo confidence intervals.
mc <- MC(fit, R = 20000L, alpha = 0.05)
mc
#> Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2239 0.0318 20000 0.1612 0.2854
#> b 0.4791 0.0314 20000 0.4184 0.5402
#> a 0.5025 0.0294 20000 0.4450 0.5596
#> X~~X 1.0017 0.0468 20000 0.9103 1.0930
#> Y~~Y 0.5628 0.0276 20000 0.5090 0.6170
#> M~~M 0.7191 0.0347 20000 0.6509 0.7868
#> Y~1 -0.0132 0.0253 20000 -0.0623 0.0366
#> M~1 -0.0037 0.0284 20000 -0.0592 0.0521
#> X~1 0.0219 0.0329 20000 -0.0436 0.0855
#> indirect 0.2407 0.0212 20000 0.2004 0.2834
#> direct 0.2239 0.0318 20000 0.1612 0.2854
#> total 0.4647 0.0293 20000 0.4064 0.5217The MCMI() function can be used to handle missing values using
multiple imputation. The MCMI() accepts the output of mice::mice(),
Amelia::amelia(), or a list of multiply imputed data sets. In this
example, we impute multivariate missing data under the normal model.
mi <- mice::mice(
df,
method = "norm",
m = 100,
print = FALSE,
seed = 42
)We fit the model using lavaan using the default listwise deletion.
fit <- sem(data = df, model = model)The fit lavaan object and mi object can then be passed to the
MCMI() function to generate Monte Carlo confidence intervals.
mcmi <- MCMI(fit, mi = mi, R = 20000L, alpha = 0.05, seed = 42)
mcmi
#> Monte Carlo Confidence Intervals (Multiple Imputation Estimates)
#> est se R 2.5% 97.5%
#> cp 0.2236 0.0317 20000 0.1616 0.2856
#> b 0.4795 0.0321 20000 0.4172 0.5418
#> a 0.5036 0.0287 20000 0.4467 0.5602
#> X~~X 1.0042 0.0474 20000 0.9117 1.0973
#> Y~~Y 0.5610 0.0274 20000 0.5071 0.6153
#> M~~M 0.7205 0.0354 20000 0.6516 0.7903
#> indirect 0.2415 0.0211 20000 0.2008 0.2835
#> direct 0.2236 0.0317 20000 0.1616 0.2856
#> total 0.4651 0.0286 20000 0.4095 0.5212Standardized Monte Carlo Confidence intervals can be generated by
passing the result of the MC() function or the MCMI() function to
MCStd().
MCStd(mc, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2307 0.0323 20000 0.1662 0.2929
#> b 0.4861 0.0296 20000 0.4275 0.5435
#> a 0.5101 0.0260 20000 0.4577 0.5593
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5961 0.0261 20000 0.5456 0.6472
#> M~~M 0.7398 0.0265 20000 0.6871 0.7905
#> indirect -0.0136 0.0200 20000 0.2092 0.2880
#> direct -0.0037 0.0323 20000 0.1662 0.2929
#> total 0.0219 0.0267 20000 0.4242 0.5292MCStd(mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2428 0.0323 20000 0.1672 0.2932
#> b 0.4869 0.0304 20000 0.4273 0.5460
#> a 0.5228 0.0253 20000 0.4603 0.5593
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5804 0.0255 20000 0.5437 0.6443
#> M~~M 0.7267 0.0258 20000 0.6872 0.7881
#> indirect 0.2545 0.0201 20000 0.2096 0.2887
#> direct 0.2428 0.0323 20000 0.1672 0.2932
#> total 0.4974 0.0260 20000 0.4270 0.5287See GitHub Pages for package documentation.
To cite semmcci in publications, please cite Pesigan & Cheung (2024).
Pesigan, I. J. A., & Cheung, S. F. (2024). 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
R Core Team. (2025). R: A language and environment for statistical computing. R Foundation for Statistical Computing. https://www.R-project.org/