Skip to contents

Generate Monte Carlo estimates from the output of lavaan::sem().

Usage

fit2mc_out(fit, progress = TRUE)

Arguments

fit

The fit object. This function only supports a lavaan::lavaan object. It can also be a lavaan.mi object returned by semTools::runMI() or its wrapper, such as semTools::sem.mi().

progress

Logical. Display progress or not. Default is TRUE.

Value

A mc_out-class object that can be used for the mc_out argument of indirect_effect(), cond_indirect_effects(), and related functions for forming Monte Carlo confidence intervals.

The object is a list with the number of elements equal to the number of Monte Carlo replications. Each element is a list of the parameter estimates and sample variances and covariances of the variables in each Monte Carlo replication.

Details

This function is for advanced users. do_mc() is a function users should try first because do_mc() has a general interface for input-specific functions like this one.

fit2mc_out() can be used to extract the stored Monte Carlo estimates so that they can be reused by indirect_effect(), cond_indirect_effects() and related functions to form Monte Carlo confidence intervals for effects such as indirect effects and conditional indirect effects.

This approach removes the need to repeat Monte Carlo simulation in each call to indirect_effect(), cond_indirect_effects(), and related functions. It also ensures that the same set of Monte Carlo estimates is used in all subsequent analyses.

See also

do_mc(), the general purpose function that users should try first before using this function.

Examples


library(lavaan)
data(data_med_mod_ab1)
dat <- data_med_mod_ab1
dat$"x:w" <- dat$x * dat$w
dat$"m:w" <- dat$m * dat$w
mod <-
"
m ~ x + w + x:w + c1 + c2
y ~ m + w + m:w + x + c1 + c2
"

fit <- sem(model = mod, data = dat, fixed.x = FALSE,
           baseline = FALSE)
# In real research, R should be 5000 or even 10000.
fit <- gen_mc_est(fit, R = 100, seed = 453253)
fit_mc_out <- fit2mc_out(fit)
#> Stage 1: Simulate estimates
#> Stage 2: Compute implied statistics
out <- cond_indirect_effects(wlevels = "w",
                             x = "x",
                             y = "y",
                             m = "m",
                             fit = fit,
                             mc_ci = TRUE,
                             mc_out = fit_mc_out)
out
#> 
#> == Conditional indirect effects ==
#> 
#>  Path: x -> m -> y
#>  Conditional on moderator(s): w
#>  Moderator(s) represented by: w
#> 
#>       [w]   (w)    ind  CI.lo CI.hi Sig    m~x   y~m
#> 1 M+1.0SD 6.046  0.248  0.025 0.457 Sig  0.342 0.725
#> 2 Mean    4.990  0.024 -0.054 0.125      0.063 0.375
#> 3 M-1.0SD 3.934 -0.006 -0.054 0.040     -0.216 0.026
#> 
#>  - [CI.lo to CI.hi] are 95.0% Monte Carlo confidence intervals with 100
#>    replications.
#>  - The 'ind' column shows the conditional indirect effects.
#>  - ‘m~x’,‘y~m’ is/are the path coefficient(s) along the path conditional
#>    on the moderator(s).
#>