Skip to contents

DOI CRAN Status R-Universe Status Make Project R-CMD-check-standard R-CMD-check-oldrel R Package Test Coverage Lint R Package Package Website (GitHub Pages) Compile LaTeX Shell Check pages-build-deployment codecov

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.   :-2.82180   Min.   :-3.130071   Min.   :-2.912288  
#>  1st Qu.:-0.64989   1st Qu.:-0.691253   1st Qu.:-0.687495  
#>  Median : 0.03282   Median :-0.008392   Median : 0.005957  
#>  Mean   : 0.02303   Mean   : 0.004328   Mean   : 0.003421  
#>  3rd Qu.: 0.71382   3rd Qu.: 0.680563   3rd Qu.: 0.719463  
#>  Max.   : 3.09723   Max.   : 2.863601   Max.   : 2.977578  
#>  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.2166 0.0307 20000  0.1573 0.2773
#> b         0.5553 0.0293 20000  0.4974 0.6124
#> a         0.5489 0.0294 20000  0.4916 0.6062
#> X~~X      0.9866 0.0459 20000  0.8967 1.0768
#> Y~~Y      0.5214 0.0255 20000  0.4712 0.5717
#> M~~M      0.7446 0.0360 20000  0.6741 0.8144
#> Y~1      -0.0123 0.0247 20000 -0.0608 0.0356
#> M~1       0.0001 0.0289 20000 -0.0567 0.0573
#> X~1       0.0237 0.0326 20000 -0.0402 0.0873
#> indirect  0.3048 0.0228 20000  0.2617 0.3507
#> direct    0.2166 0.0307 20000  0.1573 0.2773
#> total     0.5214 0.0291 20000  0.4649 0.5788

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.2167 0.0308 20000 0.1563 0.2772
#> b        0.5534 0.0307 20000 0.4934 0.6131
#> a        0.5484 0.0297 20000 0.4902 0.6065
#> X~~X     0.9868 0.0460 20000 0.8970 1.0774
#> Y~~Y     0.5220 0.0259 20000 0.4715 0.5729
#> M~~M     0.7443 0.0351 20000 0.6750 0.8125
#> indirect 0.3035 0.0234 20000 0.2589 0.3509
#> direct   0.2167 0.0308 20000 0.1563 0.2772
#> total    0.5202 0.0297 20000 0.4621 0.5781

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.2131 0.0300 20000 0.1547 0.2719
#> b         0.5614 0.0265 20000 0.5087 0.6121
#> a         0.5341 0.0244 20000 0.4848 0.5808
#> X~~X      1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y      0.5115 0.0242 20000 0.4642 0.5590
#> M~~M      0.7147 0.0260 20000 0.6626 0.7650
#> indirect -0.0122 0.0200 20000 0.2606 0.3390
#> direct    0.0001 0.0300 20000 0.1547 0.2719
#> total     0.0239 0.0250 20000 0.4627 0.5608
MCStd(mc = mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#>             est     se     R   2.5%  97.5%
#> cp       0.2016 0.0302 20000 0.1539 0.2723
#> b        0.5746 0.0278 20000 0.5046 0.6130
#> a        0.5385 0.0246 20000 0.4847 0.5814
#> X~~X     1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y     0.5045 0.0251 20000 0.4641 0.5622
#> M~~M     0.7100 0.0262 20000 0.6619 0.7651
#> indirect 0.3094 0.0206 20000 0.2589 0.3403
#> direct   0.2016 0.0302 20000 0.1539 0.2723
#> total    0.5110 0.0254 20000 0.4610 0.5597

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/