Ivan Jacob Agaloos Pesigan 2024-08-08
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.20904 Min. :-4.21945 Min. :-3.06119
#> 1st Qu.:-0.66485 1st Qu.:-0.66307 1st Qu.:-0.75574
#> Median :-0.05806 Median :-0.02508 Median :-0.02087
#> Mean :-0.03363 Mean : 0.00260 Mean :-0.02688
#> 3rd Qu.: 0.63165 3rd Qu.: 0.67435 3rd Qu.: 0.65341
#> Max. : 3.28294 Max. : 3.30787 Max. : 3.62184
#> 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.2481 0.0323 20000 0.1848 0.3124
#> b 0.4883 0.0305 20000 0.4284 0.5478
#> a 0.5230 0.0305 20000 0.4630 0.5825
#> X~~X 0.9876 0.0464 20000 0.8966 1.0780
#> Y~~Y 0.6052 0.0294 20000 0.5478 0.6631
#> M~~M 0.7930 0.0381 20000 0.7182 0.8670
#> Y~1 -0.0251 0.0262 20000 -0.0768 0.0265
#> M~1 0.0227 0.0298 20000 -0.0358 0.0807
#> X~1 -0.0362 0.0327 20000 -0.0994 0.0278
#> indirect 0.2554 0.0217 20000 0.2140 0.2989
#> direct 0.2481 0.0323 20000 0.1848 0.3124
#> total 0.5035 0.0306 20000 0.4434 0.5633
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.2485 0.0325 20000 0.1852 0.3125
#> b 0.4890 0.0311 20000 0.4286 0.5495
#> a 0.5235 0.0302 20000 0.4637 0.5825
#> X~~X 0.9895 0.0467 20000 0.8973 1.0807
#> Y~~Y 0.6049 0.0295 20000 0.5475 0.6630
#> M~~M 0.7931 0.0372 20000 0.7196 0.8655
#> indirect 0.2560 0.0220 20000 0.2136 0.2998
#> direct 0.2485 0.0325 20000 0.1852 0.3125
#> total 0.5045 0.0303 20000 0.4454 0.5640
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.2412 0.0310 20000 0.1805 0.3028
#> b 0.4926 0.0284 20000 0.4354 0.5468
#> a 0.5041 0.0255 20000 0.4522 0.5525
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5793 0.0255 20000 0.5294 0.6288
#> M~~M 0.7459 0.0257 20000 0.6947 0.7955
#> indirect -0.0246 0.0192 20000 0.2108 0.2861
#> direct 0.0220 0.0310 20000 0.1805 0.3028
#> total -0.0364 0.0261 20000 0.4364 0.5393
MCStd(mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2435 0.0313 20000 0.1799 0.3028
#> b 0.4993 0.0289 20000 0.4360 0.5485
#> a 0.5193 0.0255 20000 0.4535 0.5534
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5651 0.0256 20000 0.5279 0.6278
#> M~~M 0.7303 0.0256 20000 0.6937 0.7943
#> indirect 0.2593 0.0195 20000 0.2107 0.2873
#> direct 0.2435 0.0313 20000 0.1799 0.3028
#> total 0.5028 0.0262 20000 0.4375 0.5405
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