`mediation()`

is a short summary for multivariate-response
mediation-models, i.e. this function computes average direct and average
causal mediation effects of multivariate response models.

## Usage

```
mediation(model, ...)
# S3 method for class 'brmsfit'
mediation(
model,
treatment,
mediator,
response = NULL,
centrality = "median",
ci = 0.95,
method = "ETI",
...
)
# S3 method for class 'stanmvreg'
mediation(
model,
treatment,
mediator,
response = NULL,
centrality = "median",
ci = 0.95,
method = "ETI",
...
)
```

## Arguments

- model
A

`brmsfit`

or`stanmvreg`

object.- ...
Not used.

- treatment
Character, name of the treatment variable (or direct effect) in a (multivariate response) mediator-model. If missing,

`mediation()`

tries to find the treatment variable automatically, however, this may fail.- mediator
Character, name of the mediator variable in a (multivariate response) mediator-model. If missing,

`mediation()`

tries to find the treatment variable automatically, however, this may fail.- response
A named character vector, indicating the names of the response variables to be used for the mediation analysis. Usually can be

`NULL`

, in which case these variables are retrieved automatically. If not`NULL`

, names should match the names of the model formulas,`names(insight::find_response(model, combine = TRUE))`

. This can be useful if, for instance, the mediator variable used as predictor has a different name from the mediator variable used as response. This might occur when the mediator is transformed in one model, but used "as is" as response variable in the other model. Example: The mediator`m`

is used as response variable, but the centered version`m_center`

is used as mediator variable. The second response variable (for the treatment model, with the mediator as additional predictor),`y`

, is not transformed. Then we could use`response`

like this:`mediation(model, response = c(m = "m_center", y = "y"))`

.- centrality
The point-estimates (centrality indices) to compute. Character (vector) or list with one or more of these options:

`"median"`

,`"mean"`

,`"MAP"`

(see`map_estimate()`

),`"trimmed"`

(which is just`mean(x, trim = threshold)`

),`"mode"`

or`"all"`

.- ci
Value or vector of probability of the CI (between 0 and 1) to be estimated. Default to

`0.95`

(`95%`

).- method

## Value

A data frame with direct, indirect, mediator and
total effect of a multivariate-response mediation-model, as well as the
proportion mediated. The effect sizes are median values of the posterior
samples (use `centrality`

for other centrality indices).

## Details

`mediation()`

returns a data frame with information on the
*direct effect* (mean value of posterior samples from `treatment`

of the outcome model), *mediator effect* (mean value of posterior
samples from `mediator`

of the outcome model), *indirect effect*
(mean value of the multiplication of the posterior samples from
`mediator`

of the outcome model and the posterior samples from
`treatment`

of the mediation model) and the total effect (mean
value of sums of posterior samples used for the direct and indirect
effect). The *proportion mediated* is the indirect effect divided
by the total effect.

For all values, the `89%`

credible intervals are calculated by default.
Use `ci`

to calculate a different interval.

The arguments `treatment`

and `mediator`

do not necessarily
need to be specified. If missing, `mediation()`

tries to find the
treatment and mediator variable automatically. If this does not work,
specify these variables.

The direct effect is also called *average direct effect* (ADE),
the indirect effect is also called *average causal mediation effects*
(ACME). See also *Tingley et al. 2014* and *Imai et al. 2010*.

## Note

There is an `as.data.frame()`

method that returns the posterior
samples of the effects, which can be used for further processing in the
different bayestestR package.

## References

Imai, K., Keele, L. and Tingley, D. (2010) A General Approach to Causal Mediation Analysis, Psychological Methods, Vol. 15, No. 4 (December), pp. 309-334.

Tingley, D., Yamamoto, T., Hirose, K., Imai, K. and Keele, L. (2014). mediation: R package for Causal Mediation Analysis, Journal of Statistical Software, Vol. 59, No. 5, pp. 1-38.

## Examples

```
# \donttest{
library(mediation)
library(brms)
library(rstanarm)
# load sample data
data(jobs)
set.seed(123)
# linear models, for mediation analysis
b1 <- lm(job_seek ~ treat + econ_hard + sex + age, data = jobs)
b2 <- lm(depress2 ~ treat + job_seek + econ_hard + sex + age, data = jobs)
# mediation analysis, for comparison with Stan models
m1 <- mediate(b1, b2, sims = 1000, treat = "treat", mediator = "job_seek")
# Fit Bayesian mediation model in brms
f1 <- bf(job_seek ~ treat + econ_hard + sex + age)
f2 <- bf(depress2 ~ treat + job_seek + econ_hard + sex + age)
m2 <- brm(f1 + f2 + set_rescor(FALSE), data = jobs, refresh = 0)
#> Compiling Stan program...
#> Start sampling
# Fit Bayesian mediation model in rstanarm
m3 <- suppressWarnings(stan_mvmer(
list(
job_seek ~ treat + econ_hard + sex + age + (1 | occp),
depress2 ~ treat + job_seek + econ_hard + sex + age + (1 | occp)
),
data = jobs,
refresh = 0
))
#> Fitting a multivariate glmer model.
#>
#> Please note the warmup may be much slower than later iterations!
summary(m1)
#>
#> Causal Mediation Analysis
#>
#> Quasi-Bayesian Confidence Intervals
#>
#> Estimate 95% CI Lower 95% CI Upper p-value
#> ACME -0.0157 -0.0387 0.01 0.19
#> ADE -0.0438 -0.1315 0.04 0.35
#> Total Effect -0.0595 -0.1530 0.02 0.21
#> Prop. Mediated 0.2137 -2.0277 2.70 0.32
#>
#> Sample Size Used: 899
#>
#>
#> Simulations: 1000
#>
mediation(m2, centrality = "mean", ci = 0.95)
#> # Causal Mediation Analysis for Stan Model
#>
#> Treatment: treat
#> Mediator : job_seek
#> Response : depress2
#>
#> Effect | Estimate | 95% ETI
#> ----------------------------------------------------
#> Direct Effect (ADE) | -0.040 | [-0.125, 0.045]
#> Indirect Effect (ACME) | -0.016 | [-0.041, 0.009]
#> Mediator Effect | -0.240 | [-0.295, -0.183]
#> Total Effect | -0.056 | [-0.139, 0.032]
#>
#> Proportion mediated: 27.87% [-169.41%, 225.15%]
#>
mediation(m3, centrality = "mean", ci = 0.95)
#> # Causal Mediation Analysis for Stan Model
#>
#> Treatment: treat
#> Mediator : job_seek
#> Response : depress2
#>
#> Effect | Estimate | 95% ETI
#> ----------------------------------------------------
#> Direct Effect (ADE) | -0.039 | [-0.128, 0.051]
#> Indirect Effect (ACME) | -0.018 | [-0.043, 0.006]
#> Mediator Effect | -0.241 | [-0.296, -0.185]
#> Total Effect | -0.057 | [-0.150, 0.037]
#>
#> Proportion mediated: 31.54% [-197.50%, 260.58%]
#>
# }
```