Skip to contents

Ivan Jacob Agaloos Pesigan 2024-02-16

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.

Data

summary(df)
#>        X                  M                  Y           
#>  Min.   :-2.72466   Min.   :-3.45409   Min.   :-2.96994  
#>  1st Qu.:-0.66150   1st Qu.:-0.65344   1st Qu.:-0.65810  
#>  Median :-0.03369   Median : 0.01486   Median :-0.03471  
#>  Mean   :-0.00067   Mean   : 0.02098   Mean   :-0.00724  
#>  3rd Qu.: 0.66118   3rd Qu.: 0.72148   3rd Qu.: 0.63307  
#>  Max.   : 2.97891   Max.   : 3.18796   Max.   : 3.39427  
#>  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.2615 0.0317 20000  0.2000 0.3239
#> b         0.5300 0.0316 20000  0.4678 0.5925
#> a         0.5210 0.0282 20000  0.4659 0.5769
#> X~~X      0.9599 0.0454 20000  0.8699 1.0491
#> Y~~Y      0.5537 0.0272 20000  0.4996 0.6065
#> M~~M      0.6747 0.0328 20000  0.6108 0.7384
#> Y~1      -0.0112 0.0252 20000 -0.0616 0.0377
#> M~1       0.0161 0.0276 20000 -0.0375 0.0703
#> X~1      -0.0021 0.0321 20000 -0.0655 0.0596
#> indirect  0.2761 0.0221 20000  0.2346 0.3211
#> direct    0.2615 0.0317 20000  0.2000 0.3239
#> total     0.5376 0.0296 20000  0.4801 0.5961

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.2613 0.0318 20000 0.1993 0.3237
#> b        0.5297 0.0322 20000 0.4669 0.5921
#> a        0.5201 0.0288 20000 0.4634 0.5765
#> X~~X     0.9611 0.0451 20000 0.8731 1.0497
#> Y~~Y     0.5535 0.0275 20000 0.4999 0.6073
#> M~~M     0.6751 0.0325 20000 0.6121 0.7390
#> indirect 0.2755 0.0224 20000 0.2330 0.3208
#> direct   0.2613 0.0318 20000 0.1993 0.3237
#> total    0.5368 0.0299 20000 0.4781 0.5950

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.2536 0.0303 20000 0.1941 0.3122
#> b         0.5073 0.0279 20000 0.4518 0.5612
#> a         0.5279 0.0246 20000 0.4786 0.5753
#> X~~X      1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y      0.5425 0.0251 20000 0.4922 0.5903
#> M~~M      0.7214 0.0259 20000 0.6690 0.7709
#> indirect -0.0111 0.0194 20000 0.2303 0.3062
#> direct    0.0167 0.0303 20000 0.1941 0.3122
#> total    -0.0021 0.0249 20000 0.4710 0.5684
MCStd(mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#>             est     se     R   2.5%  97.5%
#> cp       0.2666 0.0303 20000 0.1938 0.3126
#> b        0.4964 0.0283 20000 0.4516 0.5617
#> a        0.5261 0.0252 20000 0.4761 0.5753
#> X~~X     1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y     0.5433 0.0257 20000 0.4926 0.5931
#> M~~M     0.7232 0.0265 20000 0.6690 0.7733
#> indirect 0.2612 0.0197 20000 0.2296 0.3067
#> direct   0.2666 0.0303 20000 0.1938 0.3126
#> total    0.5277 0.0247 20000 0.4712 0.5671

Documentation

See GitHub Pages for package documentation.

Citation

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

References

MacKinnon, D. P., Lockwood, C. M., & Williams, J. (2004). Confidence limits for the indirect effect: Distribution of the product and resampling methods. Multivariate Behavioral Research, 39(1), 99–128. https://doi.org/10.1207/s15327906mbr3901_4
Pesigan, I. J. A., & Cheung, S. F. (2023). Monte Carlo confidence intervals for the indirect effect with missing data. Behavior Research Methods. https://doi.org/10.3758/s13428-023-02114-4
Preacher, K. J., & Selig, J. P. (2012). Advantages of Monte Carlo confidence intervals for indirect effects. Communication Methods and Measures, 6(2), 77–98. https://doi.org/10.1080/19312458.2012.679848
Tofighi, D., & Kelley, K. (2019). Indirect effects in sequential mediation models: Evaluating methods for hypothesis testing and confidence interval formation. Multivariate Behavioral Research, 55(2), 188–210. https://doi.org/10.1080/00273171.2019.1618545
Tofighi, D., & MacKinnon, D. P. (2015). Monte Carlo confidence intervals for complex functions of indirect effects. Structural Equation Modeling: A Multidisciplinary Journal, 23(2), 194–205. https://doi.org/10.1080/10705511.2015.1057284