Statistics for Bayesian Models

Daniel Lüdecke

2019-06-04

This vignettes demontrates those functions of the sjstats-package that deal especially with Bayesian models. sjstats provides following functions:

Befor we start, we fit some models, including a mediation-object from the mediation-package, which we use for comparison with brms. The functions work with brmsfit, stanreg and stanfit-objects.

library(sjstats)
library(mediation)
library(brms)

# 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 brms
m1 <- mediate(b1, b2, sims = 1000, treat = "treat", mediator = "job_seek")

# load Bayesian models
m2 <- insight::download_model("brms_mv_6")
m3 <- insight::download_model("brms_zi_2")

Tidy Summary of Bayesian Models

tidy_stan() is no substitute, but rather a convenient alternative to summary(). The major differences are: tidy_stan()

tidy_stan(m3)
#> 
#> # Summary Statistics of Stan-Model
#> 
#> ## Conditional Model:
#> 
#>            estimate std.error      HDI(89%) ratio rhat mcse
#>  Intercept    -0.84      0.28 [-1.44 -0.29]  0.14 1.01 0.02
#>  persons       0.84      0.09 [ 0.66  1.06]  0.10 1.01 0.00
#>  child        -1.15      0.09 [-1.29 -0.98]  0.27 1.00 0.00
#>  camper        0.73      0.09 [ 0.58  0.89]  0.68 1.00 0.00
#> 
#> ## Zero-Inflated Model:
#> 
#>            estimate std.error      HDI(89%) ratio rhat mcse
#>  Intercept    -0.64      0.71 [-1.93  0.52]  0.21    1 0.03
#>  child         1.88      0.32 [ 1.40  2.43]  0.58    1 0.02
#>  camper       -0.83      0.36 [-1.41 -0.24]  0.57    1 0.02

Additional statistics in the output are:

By default, the “estimate” is the median of the posterior distribution, but this can be changed with the typical-argument.

tidy_stan(m3, typical = "mean")
#> 
#> # Summary Statistics of Stan-Model
#> 
#> ## Conditional Model:
#> 
#>            estimate std.error      HDI(89%) ratio rhat mcse
#>  Intercept    -0.83      0.28 [-1.44 -0.29]  0.14 1.01 0.02
#>  persons       0.84      0.09 [ 0.66  1.06]  0.10 1.01 0.00
#>  child        -1.15      0.09 [-1.29 -0.98]  0.27 1.00 0.00
#>  camper        0.73      0.09 [ 0.58  0.89]  0.68 1.00 0.00
#> 
#> ## Zero-Inflated Model:
#> 
#>            estimate std.error      HDI(89%) ratio rhat mcse
#>  Intercept    -0.70      0.71 [-1.93  0.52]  0.21    1 0.03
#>  child         1.89      0.32 [ 1.40  2.43]  0.58    1 0.02
#>  camper       -0.84      0.36 [-1.41 -0.24]  0.57    1 0.02

To also show random effects of multilevel models, use the type-argument.

# printing fixed and random effects of multilevel model
tidy_stan(m3, type = "all")
#> 
#> # Summary Statistics of Stan-Model
#> 
#> ## Conditional Model: Fixed effects
#> 
#>            estimate std.error      HDI(89%) ratio rhat mcse
#>  Intercept    -0.84      0.28 [-1.44 -0.29]  0.14 1.01 0.02
#>  persons       0.84      0.09 [ 0.66  1.06]  0.10 1.01 0.00
#>  child        -1.15      0.09 [-1.29 -0.98]  0.27 1.00 0.00
#>  camper        0.73      0.09 [ 0.58  0.89]  0.68 1.00 0.00
#> 
#> ## Conditional Model: Random effect (Intercept: persons)
#> 
#>            estimate std.error      HDI(89%) ratio rhat mcse
#>  persons.1    -0.01      0.10 [-0.38  0.28]  0.14 1.01 0.00
#>  persons.2     0.02      0.09 [-0.17  0.30]  0.17 1.01 0.00
#>  persons.3    -0.02      0.08 [-0.26  0.18]  0.08 1.01 0.00
#>  persons.4     0.00      0.09 [-0.32  0.33]  0.07 1.01 0.01
#> 
#> ## Zero-Inflated Model: Fixed effects
#> 
#>            estimate std.error      HDI(89%) ratio rhat mcse
#>  Intercept    -0.64      0.71 [-1.93  0.52]  0.21    1 0.03
#>  child         1.88      0.32 [ 1.40  2.43]  0.58    1 0.02
#>  camper       -0.83      0.36 [-1.41 -0.24]  0.57    1 0.02
#> 
#> ## Zero-Inflated Model: Random effect (Intercept: persons)
#> 
#>            estimate std.error      HDI(89%) ratio rhat mcse
#>  persons.1     1.28      0.78 [ 0.08  2.70]  0.20    1 0.03
#>  persons.2     0.25      0.68 [-0.90  1.57]  0.19    1 0.03
#>  persons.3    -0.18      0.71 [-1.51  1.01]  0.22    1 0.03
#>  persons.4    -1.29      0.74 [-2.62 -0.01]  0.23    1 0.03

By default, 89%-HDI are computed (a convention following McElreath 2015), but other or even multiple HDI can be computed using the prob argument.

# two different HDI for multivariate response model
tidy_stan(m2, prob = c(.5, .95))
#> 
#> # Summary Statistics of Stan-Model
#> 
#> ## Response: jobseek
#> 
#>            estimate std.error     HDI(50%)    HDI(95%) ratio rhat mcse
#>  Intercept     3.67      0.12 [3.76  3.43] [3.92 1.57]  1.57    1    0
#>  treat         0.07      0.05 [0.10 -0.03] [0.17 2.21]  2.21    1    0
#>  econ_hard     0.05      0.02 [0.07  0.01] [0.10 1.98]  1.98    1    0
#>  sex          -0.01      0.05 [0.03 -0.10] [0.08 2.17]  2.17    1    0
#>  age           0.00      0.00 [0.01 -0.00] [0.01 1.32]  1.32    1    0
#> 
#> ## Response: depress2
#> 
#>            estimate std.error      HDI(50%)     HDI(95%) ratio rhat mcse
#>  Intercept     2.21      0.15 [ 2.31  1.92] [ 2.50 1.54]  1.54    1    0
#>  treat        -0.04      0.04 [-0.00 -0.12] [ 0.04 1.96]  1.96    1    0
#>  job_seek     -0.24      0.03 [-0.22 -0.29] [-0.18  1.9]  1.90    1    0
#>  econ_hard     0.15      0.02 [ 0.16  0.11] [ 0.19 2.06]  2.06    1    0
#>  sex           0.11      0.04 [ 0.13  0.02] [ 0.19 1.99]  1.99    1    0
#>  age           0.00      0.00 [ 0.00 -0.00] [ 0.00 1.08]  1.08    1    0

Summary of Mediation Analysis

mediation() is another summary function, especially for mediation analysis, i.e. for multivariate response models with casual mediation effects.

Let us recall the models:

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, cores = 4)

Here, treat is the treatment effect, job_seek is the mediator effect, f1 describes the mediator model and f2 describes the outcome model.

mediation() returns a data frame with information on the direct effect (median value of posterior samples from treatment of the outcome model), mediator effect (median value of posterior samples from mediator of the outcome model), indirect effect (median 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 (median 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.

The simplest call just needs the model-object.

mediation(m2)
#> 
#> # Causal Mediation Analysis for Stan Model
#> 
#>   Treatment: treat
#>    Mediator: job_seek
#>    Response: depress2
#> 
#>                  Estimate    HDI (90%)
#>   Direct effect:    -0.04 [-0.11 0.03]
#> Indirect effect:    -0.02 [-0.04 0.00]
#>    Total effect:    -0.05 [-0.13 0.02]
#> 
#> Proportion mediated: 28.14% [-79.57% 135.86%]

Typically, mediation() finds the treatment and mediator variables automatically. If this does not work, use the treatment and mediator arguments to specify the related variable names. For all values, the 90% HDIs are calculated by default. Use prob to calculate a different interval.

Here is a comparison with the mediation package. Note that the summary()-output of the mediation package shows the indirect effect first, followed by the direct effect.

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, prob = .95)
#> 
#> # Causal Mediation Analysis for Stan Model
#> 
#>   Treatment: treat
#>    Mediator: job_seek
#>    Response: depress2
#> 
#>                  Estimate    HDI (95%)
#>   Direct effect:    -0.04 [-0.12 0.04]
#> Indirect effect:    -0.02 [-0.04 0.01]
#>    Total effect:    -0.05 [-0.15 0.03]
#> 
#> Proportion mediated: 28.14% [-178.65% 234.94%]

If you want to calculate mean instead of median values from the posterior samples, use the typical-argument. Furthermore, there is a print()-method, which allows to print more digits.

mediation(m2, typical = "mean", prob = .95) %>% print(digits = 4)
#> 
#> # Causal Mediation Analysis for Stan Model
#> 
#>   Treatment: treat
#>    Mediator: job_seek
#>    Response: depress2
#> 
#>                  Estimate        HDI (95%)
#>   Direct effect:  -0.0395 [-0.1244 0.0450]
#> Indirect effect:  -0.0158 [-0.0400 0.0086]
#>    Total effect:  -0.0553 [-0.1482 0.0302]
#> 
#> Proportion mediated: 28.5975% [-178.1953% 235.3902%]

As you can see, the results are similar to what the mediation package produces for non-Bayesian models.