Skip to contents

Ivan Jacob Agaloos Pesigan 2024-06-24

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.9312   Min.   :-3.41986   Min.   :-3.22681  
#>  1st Qu.:-0.6617   1st Qu.:-0.61378   1st Qu.:-0.70590  
#>  Median : 0.0253   Median :-0.01727   Median :-0.04893  
#>  Mean   : 0.0049   Mean   :-0.01659   Mean   :-0.02131  
#>  3rd Qu.: 0.6682   3rd Qu.: 0.63030   3rd Qu.: 0.62212  
#>  Max.   : 2.9616   Max.   : 3.35225   Max.   : 3.10977  
#>  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.2628 0.0304 20000  0.2033 0.3224
#> b         0.4865 0.0301 20000  0.4273 0.5452
#> a         0.4781 0.0298 20000  0.4203 0.5368
#> X~~X      0.9882 0.0467 20000  0.8968 1.0817
#> Y~~Y      0.5480 0.0268 20000  0.4964 0.6012
#> M~~M      0.7613 0.0367 20000  0.6886 0.8339
#> Y~1      -0.0044 0.0249 20000 -0.0531 0.0446
#> M~1      -0.0123 0.0291 20000 -0.0694 0.0451
#> X~1      -0.0067 0.0328 20000 -0.0717 0.0568
#> indirect  0.2326 0.0203 20000  0.1944 0.2740
#> direct    0.2628 0.0304 20000  0.2033 0.3224
#> total     0.4954 0.0294 20000  0.4376 0.5530

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.2624 0.0311 20000 0.2018 0.3234
#> b        0.4863 0.0309 20000 0.4258 0.5475
#> a        0.4792 0.0301 20000 0.4200 0.5382
#> X~~X     0.9905 0.0468 20000 0.8985 1.0821
#> Y~~Y     0.5474 0.0271 20000 0.4950 0.6005
#> M~~M     0.7615 0.0369 20000 0.6885 0.8336
#> indirect 0.2330 0.0206 20000 0.1932 0.2748
#> direct   0.2624 0.0311 20000 0.2018 0.3234
#> total    0.4954 0.0295 20000 0.4373 0.5540

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.2651 0.0303 20000 0.2056 0.3241
#> b         0.4906 0.0279 20000 0.4349 0.5446
#> a         0.4784 0.0265 20000 0.4262 0.5293
#> X~~X      1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y      0.5645 0.0257 20000 0.5135 0.6148
#> M~~M      0.7712 0.0253 20000 0.7198 0.8184
#> indirect -0.0045 0.0187 20000 0.1990 0.2722
#> direct   -0.0123 0.0303 20000 0.2056 0.3241
#> total    -0.0068 0.0261 20000 0.4479 0.5491
MCStd(mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#>             est     se     R   2.5%  97.5%
#> cp       0.2626 0.0311 20000 0.2045 0.3254
#> b        0.4873 0.0287 20000 0.4340 0.5466
#> a        0.4798 0.0268 20000 0.4255 0.5306
#> X~~X     1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y     0.5708 0.0260 20000 0.5125 0.6146
#> M~~M     0.7698 0.0256 20000 0.7185 0.8189
#> indirect 0.2338 0.0190 20000 0.1982 0.2731
#> direct   0.2626 0.0311 20000 0.2045 0.3254
#> total    0.4964 0.0264 20000 0.4468 0.5506

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, 56(3), 1678–1696. 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