Ivan Jacob Agaloos Pesigan 2024-04-14
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.43030 Min. :-3.81719 Min. :-3.17158
#> 1st Qu.:-0.60747 1st Qu.:-0.67350 1st Qu.:-0.65074
#> Median : 0.03675 Median : 0.06059 Median : 0.03110
#> Mean : 0.04401 Mean : 0.01307 Mean : 0.02672
#> 3rd Qu.: 0.73892 3rd Qu.: 0.66442 3rd Qu.: 0.72955
#> Max. : 4.02046 Max. : 2.92444 Max. : 3.19471
#> 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.2471 0.0295 20000 0.1899 0.3050
#> b 0.4709 0.0277 20000 0.4162 0.5253
#> a 0.5367 0.0295 20000 0.4790 0.5944
#> X~~X 1.0690 0.0499 20000 0.9710 1.1668
#> Y~~Y 0.5365 0.0259 20000 0.4855 0.5870
#> M~~M 0.8177 0.0390 20000 0.7417 0.8939
#> Y~1 -0.0027 0.0249 20000 -0.0514 0.0465
#> M~1 -0.0040 0.0302 20000 -0.0634 0.0548
#> X~1 0.0509 0.0341 20000 -0.0148 0.1182
#> indirect 0.2527 0.0203 20000 0.2141 0.2932
#> direct 0.2471 0.0295 20000 0.1899 0.3050
#> total 0.4999 0.0278 20000 0.4454 0.5545
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.2442 0.0295 20000 0.1864 0.3019
#> b 0.4724 0.0278 20000 0.4178 0.5274
#> a 0.5376 0.0294 20000 0.4798 0.5956
#> X~~X 1.0689 0.0503 20000 0.9700 1.1671
#> Y~~Y 0.5364 0.0264 20000 0.4850 0.5878
#> M~~M 0.8191 0.0397 20000 0.7418 0.8961
#> indirect 0.2539 0.0206 20000 0.2152 0.2959
#> direct 0.2442 0.0295 20000 0.1864 0.3019
#> total 0.4981 0.0276 20000 0.4436 0.5521
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.2574 0.0302 20000 0.1981 0.3163
#> b 0.5034 0.0273 20000 0.4491 0.5566
#> a 0.5230 0.0246 20000 0.4734 0.5697
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5447 0.0248 20000 0.4952 0.5931
#> M~~M 0.7264 0.0257 20000 0.6755 0.7759
#> indirect -0.0027 0.0192 20000 0.2259 0.3009
#> direct -0.0038 0.0302 20000 0.1981 0.3163
#> total 0.0492 0.0248 20000 0.4710 0.5680
MCStd(mcmi, alpha = 0.05)
#> Standardized Monte Carlo Confidence Intervals
#> est se R 2.5% 97.5%
#> cp 0.2476 0.0303 20000 0.1954 0.3136
#> b 0.5257 0.0277 20000 0.4507 0.5595
#> a 0.5279 0.0245 20000 0.4741 0.5699
#> X~~X 1.0000 0.0000 20000 1.0000 1.0000
#> Y~~Y 0.5250 0.0250 20000 0.4958 0.5939
#> M~~M 0.7213 0.0255 20000 0.6752 0.7752
#> indirect 0.2775 0.0194 20000 0.2276 0.3030
#> direct 0.2476 0.0303 20000 0.1954 0.3136
#> total 0.5251 0.0248 20000 0.4694 0.5659
Documentation
See GitHub Pages for package documentation.