Hello everyone, I’ve been learning Bayesian statistics with brms for a while now, and I’ve used Bayesian linear regression in some cases. My programme (Master’s in Public Health) doesn’t deal with Bayesian statistics at all, only frequentist NHST stuff so it’s all been overwhelming. I just feel like I’m drowning in statistics that I don’t even grasp.
Anyway, I’ve been trying to set my own priors recently and I’m not sure what the Intercept prior is meant to do. For starters, I’ve always used treatment/dummy coding wherein the first factor of predictors is chosen as the reference and all the coefficients are offsets from that reference. Before, I hadn’t really had a problem with treatment coding since ANOVAs and post-hocs deal with the comparisons and all I had to do was just check p-values. When I started learning mixed models and later Bayesian regression, I carried that mentality, relying on emmeans to do contrasts for me.
However, now that I’m starting to add priors, I’m beginning to realise that putting priors on them can be complex. For example, if I have two categorical predictors, the intercept represents the first level of both predictors, so how am I supposed to put a prior on that? During my search, I found that Intercepts, at least as brms implements them, are mean-centred, not raw numbers, so I’d have to combine two priors into one and then centre them according the total mean… somehow.
Anyway, that’s where I found sum/deviation coding where the intercept instead represents the total mean and the coefficients are offsets from that total mean. It seems like my problems are solved: I don’t have to think about combining multiple priors nor about mean-centring them.
Thing is, now that the intercept is a total mean, does it make sense to put a prior on it? I might know the effects of a particular treatment, be it from experts or prior research, but not the mean of the whole study population. To test my idea, I simulated a dataset with a response variable composed of two categorical predictors.
library(tidyverse)
library(brms)
library(emmeans)
Sys.setenv(RSTUDIO = 1) # For Positron users
# Assume we're building this:
# y = group + gender
# group is categorical with four levels: Control, Trt1, Trt2, Trt3.
# Control will have a mean of 20, Trt1 of 30 (diff 10), Trt2 of 27 (diff 7)
# and Trt3 of 17 (diff -3) plus some random noise.
# gender is categorical with two levels: Male and Female. Male has a mean
# of 10 while Female has 5, and they are additive to group.
# The grand mean is 31.
set.seed(67)
db5 <- tibble(Ctrl = rnorm(n = 200, mean = 20, sd = 5),
Trt1 = rnorm(n = 200, mean = 30, sd = 5),
Trt2 = rnorm(n = 200, mean = 27, sd = 5),
Trt3 = rnorm(n = 200, mean = 17, sd = 5),
Male = rnorm(n = 200, mean = 10, sd = 5),
Female = rnorm(n = 200, mean = 5, sd = 5)) |>
pivot_longer(cols = c(Ctrl:Trt3),
names_to = "group",
values_to = "values") |>
pivot_longer(cols = c(Male, Female),
names_to = "gender",
values_to = "gen_val") |>
mutate(# I want to set priors on the treatments, not the control
group = factor(group, levels = c("Trt1", "Trt2", "Trt3", "Ctrl")),
gender = as.factor(gender),
response = values + gen_val)
contrasts(db5$group) <- contr.sum(4)
contrasts(db5$gender) <- contr.sum(2)
I then run ‘get_prior’ to see the coefficients on which I can put a prior, and I see that Intercept already has the data’s total mean without me doing anything:
> get_prior(formula = response ~ group + gender, data = db5)
prior class coef group resp dpar nlpar lb ub tag source
(flat) b default
(flat) b gender1 (vectorized)
(flat) b group1 (vectorized)
(flat) b group2 (vectorized)
(flat) b group3 (vectorized)
student_t(3, 31.2, 10.1) Intercept default
student_t(3, 0, 10.1) sigma 0 default
Since brms already puts in the total mean, why should I change it? Since I’m missing factors (the Ctrl factor of the group predictor and the Male factor of the gender predictor), is it a matter of using the Intercept to better estimate the missing factors? But, wouldn’t sticking to the default brms priors be logical?
From my point of view, I can set priors on my coefficients and let brms handle the rest for the Intercept. Can this lead to disaster? Does it? I don’t know, and I hope my midnight rambling makes sense. I am willing to answer any follow-up questions if you have any.
My computer details:
- Operating System: macOS Golden Gate 27.2, MacBook Air M1
- brms version: 2.23.0
- R version : 4.6.1