Ivan Jacob Agaloos Pesigan 2025-01-13
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.82074 Min. :-2.77010 Min. :-2.83760
#> 1st Qu.:-0.63486 1st Qu.:-0.65804 1st Qu.:-0.69182
#> Median : 0.00954 Median :-0.00422 Median : 0.00394
#> Mean : 0.00962 Mean :-0.00942 Mean : 0.00601
#> 3rd Qu.: 0.68676 3rd Qu.: 0.61552 3rd Qu.: 0.66277
#> Max. : 2.52246 Max. : 3.82345 Max. : 3.27990
#> 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.3352 0.0304 20000 0.2755 0.3949
#> b 0.4713 0.0298 20000 0.4131 0.5304
#> a 0.4586 0.0302 20000 0.3996 0.5173
#> X~~X 0.9299 0.0435 20000 0.8460 1.0164
#> Y~~Y 0.5151 0.0250 20000 0.4668 0.5640
#> M~~M 0.7199 0.0344 20000 0.6523 0.7876
#> Y~1 -0.0031 0.0242 20000 -0.0504 0.0445
#> M~1 -0.0099 0.0284 20000 -0.0658 0.0458
#> X~1 0.0187 0.0318 20000 -0.0433 0.0807
#> indirect 0.2161 0.0194 20000 0.1795 0.2553
#> direct 0.3352 0.0304 20000 0.2755 0.3949
#> total 0.5513 0.0295 20000 0.4933 0.6088
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.3345 0.0307 20000 0.2745 0.3948
#> b 0.4696 0.0305 20000 0.4098 0.5294
#> a 0.4592 0.0301 20000 0.3997 0.5188
#> X~~X 0.9311 0.0435 20000 0.8454 1.0161
#> Y~~Y 0.5154 0.0254 20000 0.4661 0.5653
#> M~~M 0.7196 0.0338 20000 0.6525 0.7853
#> indirect 0.2156 0.0199 20000 0.1774 0.2551
#> direct 0.3345 0.0307 20000 0.2745 0.3948
#> total 0.5501 0.0294 20000 0.4929 0.6078
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.3303 0.0290 20000 0.2733 0.3858
#> b 0.4608 0.0272 20000 0.4073 0.5138
#> a 0.4622 0.0270 20000 0.4085 0.5139
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5379 0.0251 20000 0.4886 0.5866
#> M~~M 0.7864 0.0249 20000 0.7359 0.8331
#> indirect -0.0032 0.0175 20000 0.1798 0.2477
#> direct -0.0104 0.0290 20000 0.2733 0.3858
#> total 0.0193 0.0244 20000 0.4934 0.5894
MCStd(mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.3196 0.0295 20000 0.2717 0.3874
#> b 0.4723 0.0278 20000 0.4046 0.5132
#> a 0.4753 0.0269 20000 0.4088 0.5144
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5313 0.0256 20000 0.4891 0.5896
#> M~~M 0.7741 0.0249 20000 0.7354 0.8329
#> indirect 0.2245 0.0180 20000 0.1777 0.2479
#> direct 0.3196 0.0295 20000 0.2717 0.3874
#> total 0.5441 0.0246 20000 0.4928 0.5894
Documentation
See GitHub Pages for package documentation.