Runs mediation analysis with one or more parallel mediators (using the lavaan
package). The results can then be plotted with plot_mediation()
.
run_mediation(
data,
X,
Y,
Ms,
CVs = NULL,
standardized_all = TRUE,
conf_level = 0.95,
seed = NULL,
bootstraps = 5000,
...
)
Data frame
Predictor variable (all variables should be passed 'bare' in tidyverse style)
Outcome variable
Mediator variable(s)
Covariates (in predicting mediators and outcomes)
Logical. Should all coefficients (paths, direct and indirect effects) be standardized?
The confidence level to be used for confidence intervals. Must be between 0 and 1 (exclusive), defaults to .95 (i.e. 95% confidence intervals).
Random seed. You should set this to get reproducible results.
Number of bootstraps, defaults to 5000.
Options passed on to lavaan::sem()
.
Tibble with direct, total and indirect effects, based on bootstrap resamples. In addition, 'a' coefficients for paths from X to mediators and 'b' coefficients for paths from mediators to Y are returned. Coefficients for covariates are returned as an attribute - see the example.
Note that covariates (if given) are used in predicting each mediator and the outcome. The
coefficients for covariates are returned as an attribute to the main results,
given that they are not always reported. To access them, use attr(res, "CV_coefficients")
if you have saved the return of this function in res
. The lavaan code to estimate the model is
also returned as an attribute, access it with attr(res, "lavaan_code")
.
# Might the link between depression and self-reported health be partly explained
# by reductions in physical activity level, when holding age constant?
set.seed(4321)
res <- run_mediation(ess_health, fltdpr, health, dosprt, agea, bootstraps = 50)
#> Due to a bug in lavaan, run_mediation might not currently be stable - see https://github.com/yrosseel/lavaan/issues/275.
#> Specify missing = "listwise" or estimator = "ML" as a workaround
#> Warning: lavaan->lav_data_full():
#> some cases are empty and will be ignored: 1588.
# Note that high number of bootstraps fails - in that case, CIs might not be reliable -
# this is likely due to the limited range of the variables here
res
#> # A tibble: 5 × 7
#> type mediator est se pvalue ci.lower ci.upper
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 a dosprt -0.0822 0.0120 0 -0.105 -0.0548
#> 2 b dosprt -0.147 0.0125 0 -0.174 -0.128
#> 3 direct NA 0.283 0.0113 0 0.262 0.302
#> 4 indirect dosprt 0.0121 0.00201 0 0.00773 0.0163
#> 5 total NA 0.295 0.0110 0 0.275 0.314
attr(res, "CV_coefficients")
#> # A tibble: 2 × 7
#> DV CV est se pvalue ci.lower ci.upper
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 dosprt.agea NA -0.0365 0.0112 0 -0.0570 -0.0171
#> 2 health.agea NA 0.281 0.0108 0 0.264 0.308
# NB: bootstraps = 50 only set to reduce running time - should be 1000+