Thanks to visit codestin.com
Credit goes to github.com

Skip to content

jeksterslab/semmcci

Repository files navigation

semmcci

Ivan Jacob Agaloos Pesigan 2025-10-19

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

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("remotes")) install.packages("remotes")
remotes::install_github("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.

library(semmcci)
library(lavaan)

Data

summary(df)
#>        X                  M                  Y            
#>  Min.   :-3.18271   Min.   :-3.35323   Min.   :-2.671413  
#>  1st Qu.:-0.70696   1st Qu.:-0.62308   1st Qu.:-0.661543  
#>  Median :-0.03601   Median : 0.06377   Median :-0.005280  
#>  Mean   :-0.02181   Mean   : 0.01728   Mean   : 0.006416  
#>  3rd Qu.: 0.68023   3rd Qu.: 0.65785   3rd Qu.: 0.660697  
#>  Max.   : 2.95727   Max.   : 3.09694   Max.   : 3.664821  
#>  NA's   :100        NA's   :100        NA's   :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 <- 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.2238 0.0303 20000  0.1644 0.2840
#> b         0.5500 0.0299 20000  0.4907 0.6084
#> a         0.4982 0.0290 20000  0.4412 0.5549
#> X~~X      1.0535 0.0492 20000  0.9578 1.1500
#> Y~~Y      0.5592 0.0274 20000  0.5060 0.6134
#> M~~M      0.7649 0.0365 20000  0.6939 0.8358
#> Y~1       0.0111 0.0255 20000 -0.0388 0.0614
#> M~1       0.0187 0.0293 20000 -0.0382 0.0763
#> X~1      -0.0182 0.0339 20000 -0.0842 0.0478
#> indirect  0.2740 0.0218 20000  0.2321 0.3171
#> direct    0.2238 0.0303 20000  0.1644 0.2840
#> total     0.4978 0.0293 20000  0.4400 0.5549

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(
  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.2231 0.0302 20000 0.1632 0.2820
#> b        0.5493 0.0297 20000 0.4914 0.6083
#> a        0.4967 0.0289 20000 0.4400 0.5538
#> X~~X     1.0548 0.0493 20000 0.9585 1.1518
#> Y~~Y     0.5585 0.0276 20000 0.5036 0.6120
#> M~~M     0.7659 0.0380 20000 0.6921 0.8406
#> indirect 0.2728 0.0214 20000 0.2319 0.3159
#> direct   0.2231 0.0302 20000 0.1632 0.2820
#> total    0.4959 0.0298 20000 0.4371 0.5542

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, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#>              est     se     R   2.5%  97.5%
#> cp        0.2240 0.0300 20000 0.1649 0.2832
#> b         0.5434 0.0265 20000 0.4898 0.5948
#> a         0.5047 0.0256 20000 0.4537 0.5542
#> X~~X      1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y      0.5317 0.0250 20000 0.4834 0.5805
#> M~~M      0.7453 0.0258 20000 0.6928 0.7941
#> indirect  0.0109 0.0197 20000 0.2358 0.3131
#> direct    0.0185 0.0300 20000 0.1649 0.2832
#> total    -0.0177 0.0255 20000 0.4471 0.5474
MCStd(mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#>             est     se     R   2.5%  97.5%
#> cp       0.2243 0.0299 20000 0.1641 0.2813
#> b        0.5565 0.0262 20000 0.4912 0.5946
#> a        0.5048 0.0260 20000 0.4519 0.5540
#> X~~X     1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y     0.5139 0.0250 20000 0.4831 0.5809
#> M~~M     0.7452 0.0262 20000 0.6930 0.7958
#> indirect 0.2809 0.0193 20000 0.2362 0.3117
#> direct   0.2243 0.0299 20000 0.1641 0.2813
#> total    0.5052 0.0261 20000 0.4439 0.5467

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

About

semmcci: Monte Carlo Confidence Intervals in Structural Equation Modeling (Pesigan & Cheung, 2024: https://doi.org/10.3758/s13428-023-02114-4).

Topics

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages