Ivan Jacob Agaloos Pesigan 2024-10-22
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.76266 Min. :-3.21655 Min. :-3.04206
#> 1st Qu.:-0.74750 1st Qu.:-0.66996 1st Qu.:-0.62765
#> Median :-0.06346 Median :-0.05311 Median : 0.01924
#> Mean :-0.05342 Mean :-0.03643 Mean : 0.02026
#> 3rd Qu.: 0.60882 3rd Qu.: 0.66178 3rd Qu.: 0.67857
#> Max. : 3.06685 Max. : 3.09347 Max. : 2.84476
#> 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.2833 0.0309 20000 0.2228 0.3444
#> b 0.4577 0.0298 20000 0.3983 0.5157
#> a 0.4891 0.0306 20000 0.4293 0.5494
#> X~~X 0.9396 0.0437 20000 0.8532 1.0259
#> Y~~Y 0.5328 0.0260 20000 0.4812 0.5829
#> M~~M 0.7392 0.0357 20000 0.6702 0.8099
#> Y~1 0.0507 0.0247 20000 0.0023 0.0991
#> M~1 -0.0166 0.0285 20000 -0.0729 0.0390
#> X~1 -0.0484 0.0317 20000 -0.1112 0.0133
#> indirect 0.2238 0.0201 20000 0.1860 0.2641
#> direct 0.2833 0.0309 20000 0.2228 0.3444
#> total 0.5072 0.0294 20000 0.4501 0.5655
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.2810 0.0317 20000 0.2189 0.3432
#> b 0.4576 0.0304 20000 0.3983 0.5172
#> a 0.4903 0.0307 20000 0.4299 0.5508
#> X~~X 0.9413 0.0443 20000 0.8540 1.0276
#> Y~~Y 0.5316 0.0262 20000 0.4805 0.5832
#> M~~M 0.7403 0.0354 20000 0.6699 0.8094
#> indirect 0.2244 0.0207 20000 0.1848 0.2655
#> direct 0.2810 0.0317 20000 0.2189 0.3432
#> total 0.5054 0.0293 20000 0.4482 0.5629
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.2849 0.0304 20000 0.2253 0.3446
#> b 0.4661 0.0282 20000 0.4095 0.5211
#> a 0.4829 0.0265 20000 0.4300 0.5342
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5733 0.0260 20000 0.5213 0.6236
#> M~~M 0.7668 0.0255 20000 0.7146 0.8151
#> indirect 0.0526 0.0185 20000 0.1893 0.2621
#> direct -0.0169 0.0304 20000 0.2253 0.3446
#> total -0.0499 0.0256 20000 0.4588 0.5589
MCStd(mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2793 0.0315 20000 0.2209 0.3444
#> b 0.4706 0.0291 20000 0.4097 0.5235
#> a 0.4593 0.0268 20000 0.4300 0.5349
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5798 0.0259 20000 0.5226 0.6240
#> M~~M 0.7891 0.0258 20000 0.7139 0.8151
#> indirect 0.2161 0.0191 20000 0.1888 0.2638
#> direct 0.2793 0.0315 20000 0.2209 0.3444
#> total 0.4954 0.0257 20000 0.4567 0.5581
Documentation
See GitHub Pages for package documentation.