run_mediation: Conduct (parallel) mediation analysis

View source: R/med_mod_code.R

run_mediationR Documentation

Conduct (parallel) mediation analysis

Description

Runs mediation analysis with one or more parallel mediators (using the lavaan package). The results can then be plotted with plot_mediation().

Usage

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

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

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.

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) 
 # 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
 
 attr(res, "CV_coefficients")
 
# NB: bootstraps = 50 only set to reduce running time - should be 1000+

LukasWallrich/timesaveR documentation built on Nov. 29, 2024, 4:47 a.m.