Ivan Jacob Agaloos Pesigan 2024-10-01
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.83590 Min. :-3.39632 Min. :-3.10445
#> 1st Qu.:-0.67337 1st Qu.:-0.71509 1st Qu.:-0.72742
#> Median :-0.00771 Median :-0.10422 Median :-0.02411
#> Mean :-0.02052 Mean :-0.05667 Mean :-0.02289
#> 3rd Qu.: 0.64278 3rd Qu.: 0.59442 3rd Qu.: 0.68659
#> Max. : 3.02822 Max. : 3.55209 Max. : 3.51176
#> 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.2437 0.0317 20000 0.1818 0.3064
#> b 0.5041 0.0304 20000 0.4440 0.5626
#> a 0.5330 0.0299 20000 0.4742 0.5911
#> X~~X 0.9810 0.0460 20000 0.8918 1.0720
#> Y~~Y 0.5520 0.0271 20000 0.4985 0.6061
#> M~~M 0.7483 0.0360 20000 0.6777 0.8191
#> Y~1 0.0059 0.0253 20000 -0.0444 0.0558
#> M~1 -0.0339 0.0290 20000 -0.0915 0.0232
#> X~1 -0.0221 0.0325 20000 -0.0853 0.0418
#> indirect 0.2687 0.0222 20000 0.2263 0.3127
#> direct 0.2437 0.0317 20000 0.1818 0.3064
#> total 0.5124 0.0296 20000 0.4544 0.5699
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.2436 0.0322 20000 0.1806 0.3065
#> b 0.5029 0.0305 20000 0.4431 0.5621
#> a 0.5335 0.0303 20000 0.4736 0.5928
#> X~~X 0.9826 0.0459 20000 0.8931 1.0724
#> Y~~Y 0.5515 0.0273 20000 0.4986 0.6049
#> M~~M 0.7489 0.0355 20000 0.6787 0.8181
#> indirect 0.2683 0.0220 20000 0.2264 0.3128
#> direct 0.2436 0.0322 20000 0.1806 0.3065
#> total 0.5118 0.0299 20000 0.4533 0.5700
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.2414 0.0309 20000 0.1806 0.3019
#> b 0.5109 0.0284 20000 0.4535 0.5644
#> a 0.5209 0.0250 20000 0.4706 0.5684
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5522 0.0255 20000 0.5019 0.6020
#> M~~M 0.7286 0.0260 20000 0.6769 0.7785
#> indirect 0.0059 0.0200 20000 0.2270 0.3053
#> direct -0.0335 0.0309 20000 0.1806 0.3019
#> total -0.0223 0.0253 20000 0.4557 0.5555
MCStd(mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2426 0.0316 20000 0.1794 0.3029
#> b 0.5228 0.0283 20000 0.4543 0.5646
#> a 0.5172 0.0250 20000 0.4709 0.5697
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5366 0.0256 20000 0.5018 0.6025
#> M~~M 0.7325 0.0261 20000 0.6755 0.7782
#> indirect 0.2704 0.0198 20000 0.2279 0.3056
#> direct 0.2426 0.0316 20000 0.1794 0.3029
#> total 0.5130 0.0260 20000 0.4550 0.5563
Documentation
See GitHub Pages for package documentation.