Ivan Jacob Agaloos Pesigan 2023-05-29
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.19956 Min. :-3.37128 Min. :-3.61432
#> 1st Qu.:-0.63268 1st Qu.:-0.70516 1st Qu.:-0.66921
#> Median : 0.02823 Median : 0.02825 Median :-0.04833
#> Mean : 0.00269 Mean :-0.01992 Mean :-0.01538
#> 3rd Qu.: 0.65754 3rd Qu.: 0.65240 3rd Qu.: 0.65293
#> Max. : 3.47091 Max. : 2.93497 Max. : 3.09950
#> 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.2335 0.0291 20000 0.1765 0.2907
#> b 0.5113 0.0295 20000 0.4534 0.5698
#> a 0.4809 0.0286 20000 0.4253 0.5374
#> X~~X 1.0591 0.0491 20000 0.9634 1.1559
#> Y~~Y 0.5542 0.0269 20000 0.5016 0.6072
#> M~~M 0.7564 0.0362 20000 0.6853 0.8287
#> Y~1 -0.0127 0.0253 20000 -0.0625 0.0366
#> M~1 -0.0223 0.0292 20000 -0.0797 0.0344
#> X~1 0.0025 0.0339 20000 -0.0640 0.0691
#> indirect 0.2458 0.0202 20000 0.2080 0.2870
#> direct 0.2335 0.0291 20000 0.1765 0.2907
#> total 0.4794 0.0282 20000 0.4237 0.5344
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.2328 0.0299 20000 0.1741 0.2917
#> b 0.5113 0.0301 20000 0.4529 0.5701
#> a 0.4811 0.0287 20000 0.4241 0.5374
#> X~~X 1.0617 0.0495 20000 0.9650 1.1591
#> Y~~Y 0.5533 0.0272 20000 0.5005 0.6069
#> M~~M 0.7568 0.0361 20000 0.6856 0.8278
#> indirect 0.2460 0.0204 20000 0.2070 0.2864
#> direct 0.2328 0.0299 20000 0.1741 0.2917
#> total 0.4788 0.0285 20000 0.4236 0.5349
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.2409 0.0296 20000 0.1829 0.2983
#> b 0.5128 0.0269 20000 0.4596 0.5650
#> a 0.4946 0.0256 20000 0.4440 0.5437
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5568 0.0251 20000 0.5082 0.6056
#> M~~M 0.7554 0.0253 20000 0.7044 0.8029
#> indirect -0.0127 0.0188 20000 0.2174 0.2913
#> direct -0.0222 0.0296 20000 0.1829 0.2983
#> total 0.0024 0.0255 20000 0.4425 0.5427
MCStd(mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2450 0.0307 20000 0.1802 0.3000
#> b 0.5189 0.0277 20000 0.4587 0.5663
#> a 0.5031 0.0260 20000 0.4429 0.5454
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5429 0.0250 20000 0.5071 0.6053
#> M~~M 0.7469 0.0258 20000 0.7026 0.8038
#> indirect 0.2610 0.0190 20000 0.2168 0.2915
#> direct 0.2450 0.0307 20000 0.1802 0.3000
#> total 0.5060 0.0261 20000 0.4416 0.5444
Citation
To cite semmcci
in publications, please use:
Pesigan, I. J. A., & Cheung, S. F. (2023). Monte Carlo confidence intervals for the indirect effect with missing data. Behavior Research Methods. https://doi.org/10.3758/s13428-023-02114-4
Documentation
See GitHub Pages for package documentation.