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,
  ...
)

Arguments

data

Data frame

X

Predictor variable (all variables should be passed 'bare' in tidyverse style)

Y

Outcome variable

Ms

Mediator variable(s)

CVs

Covariates (in predicting mediators and outcomes)

standardized_all

Logical. Should all coefficients (paths, direct and indirect effects) be standardized?

conf_level

The confidence level to be used for confidence intervals. Must be between 0 and 1 (exclusive), defaults to .95 (i.e. 95% confidence intervals).

seed

Random seed. You should set this to get reproducible results.

bootstraps

Number of bootstraps, defaults to 5000.

...

Options passed on to lavaan::sem().

Value

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.

Details

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").

Examples

# 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 WARNING: 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.0831 0.0116       0 -0.106    -0.0644
#> 2 b        dosprt   -0.150  0.0107       0 -0.168    -0.130 
#> 3 direct   NA        0.284  0.0119       0  0.263     0.303 
#> 4 indirect dosprt    0.0124 0.00201      0  0.00922   0.0163
#> 5 total    NA        0.297  0.0124       0  0.275     0.317 
 
 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.0392 0.0119       0  -0.0649  -0.0212
#> 2 health.agea NA     0.282  0.00832      0   0.266    0.296 
 
# NB: bootstraps = 50 only set to reduce running time - should be 1000+