library(brms)
library(ggplot2)
library(marginaleffects)
library(modelsummary)
options(brms.backend = "cmdstanr")
theme_set(theme_minimal())
<- read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/Stat2Data/Titanic.csv")
titanic <- subset(titanic, PClass != "*")
titanic
<- Survived ~ SexCode + Age + PClass
f
<- brm(f,
mod_prior data = titanic,
prior = c(prior(normal(0, .2), class = b)),
cores = 4,
sample_prior = "only")
<- brm(f,
mod_posterior data = titanic,
cores = 4,
prior = c(prior(normal(0, .2), class = b)))
Bayesians often advocate for the use of prior predictive checks (Gelman et al. 2020). The idea is to simulate from the model, without using the data, in order to refine the model before fitting. For example, we could draw parameter values from the priors, and use the model to simulate values of the outcome. Then, could inspect those to determine if the simulated outcomes (and thus the priors) make sense substantively. Prior predictive checks allow us to iterate on the model without looking at the data multiple times.
One major challenge lies in interpretation: When the parameters of a model are hard to interpret, the analyst will often need to transform before they can assess if the generated quantities make sense, and if the priors are an appropriate representation of available information.
In this post I show how to use the marginaleffects
and brms
packages for R
to facilitate this process. The benefit of the approach described below is that it allows us to conduct prior predictive checks on the actual quantities of interest. For example, if the ultimate quantity that we want to estimate is a contrast or an Average Treatment Effect, then we can use marginaleffects
to simulate the specific quantity of interest using just the priors and the model.
In this example, we create two model objects with brms
. In one of them, we set sample_prior="only"
to indicate that we do not want to use the dataset at all, and that we only want to use the priors and model for simulation:
Now, we use the avg_comparisons()
function from the marginaleffects
package to compute contrasts of interest:
<- list(
cmp "Prior" = avg_comparisons(mod_prior),
"Posterior" = avg_comparisons(mod_posterior))
Finally, we compare the results with and without the data in tables and plots:
modelsummary(
cmp,output = "markdown",
statistic = "conf.int",
fmt = fmt_significant(2),
gof_map = NA,
shape = term : contrast ~ model)
Prior | Posterior | |
---|---|---|
Age mean(+1) | 0.0022 | -0.0058 |
[-0.3840, 0.3820] | [-0.0079, -0.0036] | |
PClass mean(2nd) - mean(1st) | 0.0022 | -0.19 |
[-0.4003, 0.3893] | [-0.27, -0.12] | |
PClass mean(3rd) - mean(1st) | -0.0023 | -0.38 |
[-0.3847, 0.3983] | [-0.45, -0.31] | |
SexCode mean(1) - mean(0) | -0.00096 | 0.49 |
[-0.37995, 0.38494] | [0.43, 0.55] |
<- lapply(names(cmp), \(x) transform(posteriordraws(cmp[[x]]), Label = x))
draws <- do.call("rbind", draws)
draws
ggplot(draws, aes(x = draw, color = Label)) +
xlim(c(-1, 1)) +
geom_density() +
facet_wrap(~term + contrast, scales = "free")
This kind of approach is particularly useful with more complicated models, such as this one with categorical outcomes. In such models, it would be hard to know if a normal prior is appropriate for the different parameters:
<- brm(
modcat_posterior ~ SexCode + Age,
PClass prior = c(
prior(normal(0, 3), class = b, dpar = "mu2nd"),
prior(normal(0, 3), class = b, dpar = "mu3rd")),
family = categorical(link = logit),
cores = 4,
data = titanic)
<- brm(
modcat_prior ~ SexCode + Age,
PClass prior = c(
prior(normal(0, 3), class = b, dpar = "mu2nd"),
prior(normal(0, 3), class = b, dpar = "mu3rd")),
family = categorical(link = logit),
sample_prior = "only",
cores = 4,
data = titanic)
<- posteriordraws(comparisons(modcat_prior))
pd
comparisons(modcat_prior) |> summary()
rowid term group contrast
Min. : 1.0 Length:4536 Length:4536 Length:4536
1st Qu.:189.8 Class :character Class :character Class :character
Median :378.5 Mode :character Mode :character Mode :character
Mean :378.5
3rd Qu.:567.2
Max. :756.0
estimate conf.low conf.high
Min. :-4.877e-03 Min. :-0.8074854 Min. :0.0000075
1st Qu.: 0.000e+00 1st Qu.:-0.3366492 1st Qu.:0.0160804
Median : 0.000e+00 Median :-0.0950372 Median :0.0930379
Mean : 7.242e-06 Mean :-0.2026954 Mean :0.2030676
3rd Qu.: 0.000e+00 3rd Qu.:-0.0159494 3rd Qu.:0.3377531
Max. : 7.390e-03 Max. :-0.0000107 Max. :0.8351262
predicted_lo predicted_hi predicted tmp_idx
Min. :0.000e+00 Min. :0.0000000 Min. :0.000e+00 Min. : 1.0
1st Qu.:0.000e+00 1st Qu.:0.0000000 1st Qu.:0.000e+00 1st Qu.:189.8
Median :2.046e-05 Median :0.0000235 Median :2.046e-05 Median :378.5
Mean :1.741e-02 Mean :0.0171463 Mean :1.723e-02 Mean :378.5
3rd Qu.:2.955e-03 3rd Qu.:0.0032384 3rd Qu.:2.955e-03 3rd Qu.:567.2
Max. :2.008e-01 Max. :0.2007756 Max. :2.008e-01 Max. :756.0
PClass SexCode Age
Length:4536 Min. :0.000 Min. : 0.17
Class :character 1st Qu.:0.000 1st Qu.:21.00
Mode :character Median :0.000 Median :28.00
Mean :0.381 Mean :30.40
3rd Qu.:1.000 3rd Qu.:39.00
Max. :1.000 Max. :71.00
comparisons(modcat_posterior) |> summary()
rowid term group contrast
Min. : 1.0 Length:4536 Length:4536 Length:4536
1st Qu.:189.8 Class :character Class :character Class :character
Median :378.5 Mode :character Mode :character Mode :character
Mean :378.5
3rd Qu.:567.2
Max. :756.0
estimate conf.low conf.high predicted_lo
Min. :-0.1419286 Min. :-0.210772 Min. :-0.067719 Min. :0.02491
1st Qu.:-0.0122378 1st Qu.:-0.038345 1st Qu.:-0.008248 1st Qu.:0.21562
Median : 0.0005095 Median :-0.008441 Median : 0.005352 Median :0.29648
Mean :-0.0001550 Mean :-0.033547 Mean : 0.033965 Mean :0.33282
3rd Qu.: 0.0262578 3rd Qu.: 0.010624 3rd Qu.: 0.089668 3rd Qu.:0.45937
Max. : 0.1384263 Max. : 0.051932 Max. : 0.224784 Max. :0.89571
predicted_hi predicted tmp_idx PClass
Min. :0.02682 Min. :0.02523 Min. : 1.0 Length:4536
1st Qu.:0.22428 1st Qu.:0.21562 1st Qu.:189.8 Class :character
Median :0.31562 Median :0.29877 Median :378.5 Mode :character
Mean :0.33285 Mean :0.33283 Mean :378.5
3rd Qu.:0.42031 3rd Qu.:0.43970 3rd Qu.:567.2
Max. :0.90807 Max. :0.89571 Max. :756.0
SexCode Age
Min. :0.000 Min. : 0.17
1st Qu.:0.000 1st Qu.:21.00
Median :0.000 Median :28.00
Mean :0.381 Mean :30.40
3rd Qu.:1.000 3rd Qu.:39.00
Max. :1.000 Max. :71.00