Skip to contents

In this example, the Monte Carlo method is used to generate confidence intervals for the indirect effect in a simple mediation model with missing data where variable X has an effect on variable Y, through a mediating variable M with multiply imputed data sets.

Data

summary(df)
#>        X                   M                  Y           
#>  Min.   :-3.199558   Min.   :-3.37128   Min.   :-3.61432  
#>  1st Qu.:-0.632676   1st Qu.:-0.70516   1st Qu.:-0.66921  
#>  Median : 0.028235   Median : 0.02825   Median :-0.04834  
#>  Mean   : 0.002689   Mean   :-0.01992   Mean   :-0.01538  
#>  3rd Qu.: 0.657543   3rd Qu.: 0.65239   3rd Qu.: 0.65293  
#>  Max.   : 3.470910   Max.   : 2.93497   Max.   : 3.09950  
#>  NA's   :100         NA's   :100        NA's   :100

Multiple Imputation

Perform the appropriate multiple imputation approach to deal with missing values. In this example, we impute multivariate missing data under the normal model.

mi <- mice::mice(
  df,
  method = "norm",
  m = 100,
  print = FALSE,
  seed = 42
)

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

Model Fitting

We can now fit the model using the sem() function from lavaan. We do not need to deal with missing values in this stage.

fit <- sem(data = df, model = model)

Monte Carlo Confidence Intervals

The fit lavaan object and mi mids object can then be passed to the MCMI() function from semmcci to generate Monte Carlo confidence intervals using multiple imputation as described in Pesigan and Cheung (2023).

MCMI(fit, mi = mi, R = 20000L, alpha = 0.05, seed = 42)
#> Monte Carlo Confidence Intervals (Multiple Imputation Estimates)
#>             est     se     R   2.5%  97.5%
#> cp       0.2328 0.0299 20000 0.1742 0.2914
#> b        0.5113 0.0301 20000 0.4529 0.5700
#> a        0.4811 0.0289 20000 0.4245 0.5380
#> X~~X     1.0617 0.0495 20000 0.9651 1.1591
#> Y~~Y     0.5533 0.0270 20000 0.5000 0.6062
#> M~~M     0.7568 0.0361 20000 0.6856 0.8276
#> indirect 0.2460 0.0204 20000 0.2065 0.2865
#> direct   0.2328 0.0299 20000 0.1742 0.2914
#> total    0.4788 0.0286 20000 0.4236 0.5349

Standardized Monte Carlo Confidence Intervals

Standardized Monte Carlo Confidence intervals can be generated by passing the result of the MCMI() function to the MCStd() function.

fit <- sem(data = df, model = model)
unstd <- MCMI(fit, mi = mi, R = 20000L, alpha = 0.05, seed = 42)
MCStd(unstd, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#>             est     se     R   2.5%  97.5%
#> cp       0.2450 0.0307 20000 0.1806 0.2999
#> b        0.5189 0.0277 20000 0.4589 0.5662
#> a        0.5031 0.0261 20000 0.4435 0.5453
#> X~~X     1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y     0.5429 0.0251 20000 0.5063 0.6054
#> M~~M     0.7469 0.0258 20000 0.7026 0.8033
#> indirect 0.2610 0.0191 20000 0.2165 0.2921
#> direct   0.2450 0.0307 20000 0.1806 0.2999
#> total    0.5060 0.0262 20000 0.4421 0.5446

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