Ivan Jacob Agaloos Pesigan 2026-06-14
Installation
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")Description
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).
Example
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.
Data
summary(df)
#> X M Y
#> Min. :-3.24635 Min. :-3.2758 Min. :-2.96764
#> 1st Qu.:-0.61936 1st Qu.:-0.7034 1st Qu.:-0.67348
#> Median : 0.01012 Median :-0.0570 Median :-0.03637
#> Mean : 0.01241 Mean :-0.0120 Mean :-0.01110
#> 3rd Qu.: 0.66866 3rd Qu.: 0.6921 3rd Qu.: 0.61656
#> Max. : 3.25659 Max. : 3.4264 Max. : 3.42824
#> NAs :100 NAs :100 NAs :100Model 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)
"Monte Carlo Confidence Intervals
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.2189 0.0304 20000 0.1584 0.2780
#> b 0.5131 0.0279 20000 0.4585 0.5684
#> a 0.5079 0.0320 20000 0.4451 0.5709
#> X~~X 0.9654 0.0452 20000 0.8769 1.0556
#> Y~~Y 0.5308 0.0260 20000 0.4801 0.5822
#> M~~M 0.8348 0.0403 20000 0.7562 0.9148
#> Y~1 -0.0216 0.0248 20000 -0.0692 0.0279
#> M~1 -0.0252 0.0305 20000 -0.0844 0.0344
#> X~1 0.0093 0.0324 20000 -0.0551 0.0719
#> indirect 0.2606 0.0217 20000 0.2194 0.3042
#> direct 0.2189 0.0304 20000 0.1584 0.2780
#> total 0.4795 0.0302 20000 0.4202 0.5386Monte Carlo Confidence Intervals - Multiple Imputation
The 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.2195 0.0308 20000 0.1579 0.2791
#> b 0.5124 0.0284 20000 0.4565 0.5684
#> a 0.5085 0.0320 20000 0.4450 0.5711
#> X~~X 0.9671 0.0454 20000 0.8780 1.0557
#> Y~~Y 0.5311 0.0269 20000 0.4784 0.5844
#> M~~M 0.8342 0.0410 20000 0.7545 0.9137
#> indirect 0.2606 0.0219 20000 0.2188 0.3047
#> direct 0.2195 0.0308 20000 0.1579 0.2791
#> total 0.4800 0.0301 20000 0.4204 0.5382Standardized Monte Carlo Confidence Intervals
Standardized 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.2181 0.0299 20000 0.1588 0.2757
#> b 0.5417 0.0266 20000 0.4886 0.5931
#> a 0.4793 0.0266 20000 0.4260 0.5302
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5458 0.0255 20000 0.4958 0.5956
#> M~~M 0.7702 0.0254 20000 0.7189 0.8186
#> indirect -0.0219 0.0195 20000 0.2214 0.2979
#> direct -0.0242 0.0299 20000 0.1588 0.2757
#> total 0.0094 0.0264 20000 0.4240 0.5280
MCStd(mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2253 0.0305 20000 0.1581 0.2781
#> b 0.5379 0.0269 20000 0.4870 0.5923
#> a 0.4787 0.0269 20000 0.4257 0.5312
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5439 0.0258 20000 0.4956 0.5963
#> M~~M 0.7709 0.0257 20000 0.7178 0.8188
#> indirect 0.2575 0.0196 20000 0.2213 0.2981
#> direct 0.2253 0.0305 20000 0.1581 0.2781
#> total 0.4828 0.0266 20000 0.4243 0.5287Documentation
See GitHub Pages for package documentation.