Ivan Jacob Agaloos Pesigan 2024-12-29
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")
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).
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.
library(semmcci)
library(lavaan)
summary(df)
#> X M Y
#> Min. :-3.19971 Min. :-2.86033 Min. :-3.32444
#> 1st Qu.:-0.61958 1st Qu.:-0.61034 1st Qu.:-0.60828
#> Median : 0.06000 Median : 0.01902 Median : 0.01347
#> Mean : 0.05374 Mean : 0.02038 Mean : 0.02507
#> 3rd Qu.: 0.71782 3rd Qu.: 0.69918 3rd Qu.: 0.72165
#> Max. : 3.64549 Max. : 3.44731 Max. : 3.04963
#> NA's :100 NA's :100 NA's :100
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)
"
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.2458 0.0301 20000 0.1863 0.3045
#> b 0.5284 0.0301 20000 0.4697 0.5876
#> a 0.4630 0.0300 20000 0.4043 0.5223
#> X~~X 1.0000 0.0469 20000 0.9087 1.0906
#> Y~~Y 0.5603 0.0276 20000 0.5062 0.6142
#> M~~M 0.7812 0.0380 20000 0.7066 0.8552
#> Y~1 0.0058 0.0253 20000 -0.0438 0.0559
#> M~1 0.0019 0.0291 20000 -0.0557 0.0589
#> X~1 0.0522 0.0330 20000 -0.0130 0.1166
#> indirect 0.2446 0.0210 20000 0.2052 0.2873
#> direct 0.2458 0.0301 20000 0.1863 0.3045
#> total 0.4904 0.0298 20000 0.4319 0.5490
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.2441 0.0306 20000 0.1837 0.3041
#> b 0.5287 0.0296 20000 0.4705 0.5868
#> a 0.4632 0.0303 20000 0.4036 0.5230
#> X~~X 1.0025 0.0475 20000 0.9096 1.0959
#> Y~~Y 0.5585 0.0275 20000 0.5049 0.6124
#> M~~M 0.7828 0.0374 20000 0.7094 0.8564
#> indirect 0.2449 0.0211 20000 0.2046 0.2879
#> direct 0.2441 0.0306 20000 0.1837 0.3041
#> total 0.4890 0.0300 20000 0.4292 0.5473
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.2435 0.0296 20000 0.1853 0.3017
#> b 0.5223 0.0269 20000 0.4686 0.5740
#> a 0.4640 0.0270 20000 0.4102 0.5165
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5499 0.0254 20000 0.5000 0.5998
#> M~~M 0.7847 0.0250 20000 0.7332 0.8318
#> indirect 0.0057 0.0188 20000 0.2061 0.2801
#> direct 0.0019 0.0296 20000 0.1853 0.3017
#> total 0.0522 0.0263 20000 0.4326 0.5363
MCStd(mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2418 0.0299 20000 0.1829 0.3009
#> b 0.5159 0.0265 20000 0.4707 0.5746
#> a 0.4656 0.0270 20000 0.4109 0.5166
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5593 0.0251 20000 0.5003 0.5986
#> M~~M 0.7832 0.0251 20000 0.7332 0.8312
#> indirect 0.2402 0.0191 20000 0.2064 0.2813
#> direct 0.2418 0.0299 20000 0.1829 0.3009
#> total 0.4820 0.0261 20000 0.4320 0.5352
See GitHub Pages for package documentation.
To cite semmcci
in publications, please cite Pesigan & Cheung (2023).
MacKinnon, D. P., Lockwood, C. M., & Williams, J. (2004). Confidence limits for the indirect effect: Distribution of the product and resampling methods. Multivariate Behavioral Research, 39(1), 99–128. https://doi.org/10.1207/s15327906mbr3901_4
Pesigan, I. J. A., & Cheung, S. F. (2023). Monte Carlo confidence intervals for the indirect effect with missing data. Behavior Research Methods, 56(3), 1678–1696. https://doi.org/10.3758/s13428-023-02114-4
Preacher, K. J., & Selig, J. P. (2012). Advantages of Monte Carlo confidence intervals for indirect effects. Communication Methods and Measures, 6(2), 77–98. https://doi.org/10.1080/19312458.2012.679848
Tofighi, D., & Kelley, K. (2019). Indirect effects in sequential mediation models: Evaluating methods for hypothesis testing and confidence interval formation. Multivariate Behavioral Research, 55(2), 188–210. https://doi.org/10.1080/00273171.2019.1618545
Tofighi, D., & MacKinnon, D. P. (2015). Monte Carlo confidence intervals for complex functions of indirect effects. Structural Equation Modeling: A Multidisciplinary Journal, 23(2), 194–205. https://doi.org/10.1080/10705511.2015.1057284