Skip to contents

Ivan Jacob Agaloos Pesigan 2026-12-03 04:42:40

Description

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. semmcci 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. The functions MCGeneric(), MCFunc(), and Func() generate Monte Carlo confidence intervals for functions of parameters when the estimates and their sampling covariance matrix are supplied. A description of the package and code examples are presented in Pesigan and Cheung (2024, doi:10.3758/s13428-023-02114-4).

In the Monte Carlo method, a sampling distribution of parameter estimates 𝜽̂*\hat{\boldsymbol{\theta}}^{\ast} is generated from the multivariate normal distribution using the parameter estimates 𝜽̂\hat{\boldsymbol{\theta}} and the corresponding sampling variance-covariance matrix Var̂(𝜽̂)\widehat{\mathrm{Var}} \left( \hat{\boldsymbol{\theta}} \right),

𝜽̂*∼𝒩(𝜽̂,Var̂(𝜽̂)).\hat{\boldsymbol{\theta}}^{\ast} \sim \mathcal{N} \left( \hat{\boldsymbol{\theta}}, \widehat{\mathrm{Var}} \left( \hat{\boldsymbol{\theta}} \right) \right) .

Confidence intervals for defined parameters 𝒈(𝜽̂)\boldsymbol{g} \left( \hat{\boldsymbol{\theta}} \right) are generated by obtaining percentiles corresponding to 100(1−α)%100 \left( 1 - \alpha \right) \% from the generated sampling distribution, where α\alpha is the significance level.

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")

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.

library(package = semmcci)
library(package = lavaan)

Data

summary(object = df)
#>        X                  M                   Y            
#>  Min.   :-4.19042   Min.   :-3.074942   Min.   :-3.010075  
#>  1st Qu.:-0.67706   1st Qu.:-0.661117   1st Qu.:-0.680681  
#>  Median : 0.03236   Median : 0.019731   Median : 0.002433  
#>  Mean   : 0.01597   Mean   :-0.004718   Mean   :-0.012507  
#>  3rd Qu.: 0.68465   3rd Qu.: 0.621487   3rd Qu.: 0.617368  
#>  Max.   : 3.44872   Max.   : 3.017559   Max.   : 3.006131  
#>  NAs    :100        NAs    :100         NAs    :100

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 <- "
  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 <- lavaan::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(lav = fit, R = 20000L, alpha = 0.05)
mc
#> Monte Carlo Confidence Intervals
#>              est     se     R    2.5%  97.5%
#> cp        0.2318 0.0312 20000  0.1703 0.2923
#> b         0.4598 0.0310 20000  0.4002 0.5208
#> a         0.4894 0.0290 20000  0.4328 0.5461
#> X~~X      0.9874 0.0467 20000  0.8964 1.0794
#> Y~~Y      0.5512 0.0264 20000  0.4993 0.6031
#> M~~M      0.6870 0.0332 20000  0.6221 0.7516
#> Y~1      -0.0259 0.0251 20000 -0.0753 0.0229
#> M~1      -0.0034 0.0275 20000 -0.0578 0.0500
#> X~1       0.0091 0.0324 20000 -0.0544 0.0736
#> indirect  0.2250 0.0202 20000  0.1873 0.2655
#> direct    0.2318 0.0312 20000  0.1703 0.2923
#> total     0.4568 0.0289 20000  0.4003 0.5136

Monte 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(
  data = df,
  method = "norm",
  m = 100,
  print = FALSE,
  seed = 42
)

We fit the model using lavaan using the default listwise deletion.

fit <- lavaan::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(lav = 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.2312 0.0314 20000 0.1696 0.2924
#> b        0.4569 0.0314 20000 0.3957 0.5178
#> a        0.4899 0.0290 20000 0.4331 0.5472
#> X~~X     0.9902 0.0472 20000 0.8980 1.0831
#> Y~~Y     0.5514 0.0269 20000 0.4985 0.6036
#> M~~M     0.6860 0.0333 20000 0.6205 0.7505
#> indirect 0.2238 0.0204 20000 0.1849 0.2646
#> direct   0.2312 0.0314 20000 0.1696 0.2924
#> total    0.4550 0.0290 20000 0.3990 0.5122

Standardized 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 = mc, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#>              est     se     R   2.5%  97.5%
#> cp        0.2425 0.0320 20000 0.1789 0.3050
#> b         0.4651 0.0292 20000 0.4078 0.5221
#> a         0.5060 0.0260 20000 0.4546 0.5561
#> X~~X      1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y      0.6107 0.0259 20000 0.5593 0.6603
#> M~~M      0.7439 0.0263 20000 0.6908 0.7934
#> indirect -0.0273 0.0194 20000 0.1986 0.2736
#> direct   -0.0036 0.0320 20000 0.1789 0.3050
#> total     0.0091 0.0266 20000 0.4243 0.5277
MCStd(mc = mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#>             est     se     R   2.5%  97.5%
#> cp       0.2298 0.0325 20000 0.1784 0.3053
#> b        0.4699 0.0296 20000 0.4046 0.5197
#> a        0.4881 0.0266 20000 0.4548 0.5583
#> X~~X     1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y     0.6209 0.0261 20000 0.5605 0.6635
#> M~~M     0.7617 0.0270 20000 0.6883 0.7932
#> indirect 0.2294 0.0196 20000 0.1964 0.2738
#> direct   0.2298 0.0325 20000 0.1784 0.3053
#> total    0.4592 0.0270 20000 0.4228 0.5291

Other Features

The package provides the following functions.

Function Purpose
MC() Monte Carlo confidence intervals from a lavaan model
MCMI() Monte Carlo confidence intervals from a lavaan model fitted to multiply imputed data
MCStd() Monte Carlo confidence intervals for the standardized estimates
MCGeneric() Monte Carlo confidence intervals for defined parameters of any fitted model with coef() and vcov() methods
MCFunc() Monte Carlo confidence intervals for a function of supplied estimates and their sampling covariance matrix
Func() Confidence intervals for a function of a supplied sampling distribution

The coef(), vcov(), confint(), summary(), and print() methods are available for the returned objects. Examples for each function are in the package vignettes.

Documentation

See GitHub Pages for package documentation.

Citation

To cite semmcci in publications, please cite Pesigan & Cheung (2024).

References

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. (2026). R: A language and environment for statistical computing. R Foundation for Statistical Computing. https://www.R-project.org/