Ivan Jacob Agaloos Pesigan 2024-07-08
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. :-3.49425 Min. :-3.16745 Min. :-3.16624
#> 1st Qu.:-0.74812 1st Qu.:-0.64019 1st Qu.:-0.68746
#> Median :-0.01954 Median :-0.02472 Median :-0.04138
#> Mean :-0.05797 Mean : 0.00612 Mean :-0.03266
#> 3rd Qu.: 0.62931 3rd Qu.: 0.66834 3rd Qu.: 0.66710
#> Max. : 3.19801 Max. : 3.00636 Max. : 2.91971
#> 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.2076 0.0317 20000 0.1466 0.2711
#> b 0.5101 0.0303 20000 0.4506 0.5688
#> a 0.4754 0.0310 20000 0.4153 0.5361
#> X~~X 0.9599 0.0451 20000 0.8715 1.0489
#> Y~~Y 0.5737 0.0283 20000 0.5186 0.6286
#> M~~M 0.7813 0.0378 20000 0.7086 0.8567
#> Y~1 -0.0358 0.0257 20000 -0.0864 0.0139
#> M~1 0.0384 0.0294 20000 -0.0189 0.0964
#> X~1 -0.0580 0.0321 20000 -0.1220 0.0046
#> indirect 0.2425 0.0212 20000 0.2023 0.2856
#> direct 0.2076 0.0317 20000 0.1466 0.2711
#> total 0.4501 0.0311 20000 0.3903 0.5114
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.2076 0.0324 20000 0.1439 0.2712
#> b 0.5082 0.0311 20000 0.4473 0.5689
#> a 0.4751 0.0309 20000 0.4140 0.5357
#> X~~X 0.9622 0.0450 20000 0.8747 1.0509
#> Y~~Y 0.5741 0.0283 20000 0.5190 0.6296
#> M~~M 0.7799 0.0371 20000 0.7070 0.8523
#> indirect 0.2415 0.0216 20000 0.1998 0.2846
#> direct 0.2076 0.0324 20000 0.1439 0.2712
#> total 0.4490 0.0307 20000 0.3887 0.5094
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.2064 0.0311 20000 0.1464 0.2685
#> b 0.5171 0.0279 20000 0.4614 0.5698
#> a 0.4662 0.0270 20000 0.4125 0.5181
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5905 0.0264 20000 0.5378 0.6419
#> M~~M 0.7827 0.0252 20000 0.7316 0.8298
#> indirect -0.0363 0.0192 20000 0.2038 0.2794
#> direct 0.0384 0.0311 20000 0.1464 0.2685
#> total -0.0592 0.0277 20000 0.3922 0.5001
MCStd(mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2202 0.0320 20000 0.1434 0.2687
#> b 0.5160 0.0283 20000 0.4590 0.5696
#> a 0.4621 0.0272 20000 0.4127 0.5192
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5802 0.0257 20000 0.5418 0.6426
#> M~~M 0.7865 0.0254 20000 0.7304 0.8297
#> indirect 0.2385 0.0198 20000 0.2016 0.2794
#> direct 0.2202 0.0320 20000 0.1434 0.2687
#> total 0.4586 0.0277 20000 0.3918 0.5000
Documentation
See GitHub Pages for package documentation.