Should I change intercept priors when using sum coding?

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

Hey, sorry but I don’t know the answer to your question. I just wanted to point you towards this thread where the sum-to-zero constraint has been implemented in brms. You could model the categorical effects hierarchically (with a shared scale) and retain the intercept as the average of your categories. This makes reasoning about priors a lot easier!

By switching to sum coding, you aligned your model to how brms handles the prior for the intercept (as you said, it expects a prior for the average case, not the baseline/reference). You can also do the opposite. By writing your model as response ~ 0 + Intercept +group + gender you switch off mean centering and your prior for the intercept should now be based on prior information about the reference category:

> default_prior(response ~ 0 + Intercept +group + gender, data = db5)
                 prior class       coef group resp dpar nlpar lb ub tag       source
                (flat)     b                                                 default
                (flat)     b genderMale                                 (vectorized)
                (flat)     b  groupTrt1                                 (vectorized)
                (flat)     b  groupTrt2                                 (vectorized)
                (flat)     b  groupTrt3                                 (vectorized)
                (flat)     b  Intercept                                 (vectorized)
 student_t(3, 0, 10.1) sigma                                   0             default

(I have also recoded the group factor so that Ctrl is the reference level)

Following the logic of your example, every existing study that examines the effect of each treatment would also contain information about the control group. In this setting, our expectation about the baseline is usually better informed compared to any of the treatments (what is the expected variation of the response in the absence of any intervention?). And when that is true, brms’ default handling of intercept priors is inconvenient (so it is great that it can be easily bypassed).

Contrast your example to a case where you have three continuous predictors. By centering them, you make the intercept represent an average individual. And then how brms handles intercepts (and their priors) becomes really convenient.

Hey, thank you for the resource. Honestly I am… drowning in terms I don’t know but I’ll try my best to understand!

Thank you for your response.

I’ve actually read some stuff about using response ~ 0 + group + gender before but I had weird results with priors. It wasn’t until I tried it with an explicit Intercept term that it worked. It’s certainly interesting but I think I’ll stick with sum coding for the moment – since the goal is learning it. Plus, remembering y ~ 0 + Intercept + x sounds a bit complicated compared to just y ~ x, especially when I’m going to be moving on to interactions and random effects later on.

I’ve not really ‘got it’ yet so I’ll simulate some data and get back at you on that!

I agree the y ~ 0 + Intercept is odd, but the issues around it are very important to understand if you’re going to use non-default priors and predictors that have not been mean centered with brms. However there is a coding alternative with equivalent results, which is you can use the conventional formula syntax, but set center = FALSE within bf(), such as bf(y ~ x, center = FALSE). I walked this issue out in some detail in this blog post: Learn Stan with brms, Part III | A. Solomon Kurz

Is it a parameter of the model? It always makes sense to have priors on parameters. If you don’t have priors, you don’t even have a proper (in the technical sense of defining a joint density) Bayesian model. An improper model can still have a proper posterior, but you can’t do things like prior predictive checks or simulation-based calibration checks.

Now you can achieve that in a lot of different ways and which prior to choose will depend on your problem and what you know about it.

This requires an addition assumption that 0 is the population average of each covariate. You can achieve this by centering the covariates. But otherwise, you might have covariates that are all positive, and zero might even be an illegal value.

Here’s a simple example:

vector[N] y;
vector[N] x;
vector[K] alpha;
real mu;
real<lower=0> sigma;
real beta;
...
alpha[k] ~ normal(mu, sigma);  
y[n] ~ normal(alpha[k] + beta * x[n], sigma);

Here beta is a fixed effect and alpha is a “random” or varying effect. If we were to add an intercept, normal(gamma + alpha[k] + ..., ), the model would not be identified because I can add to gamma and subtract from all alpha and get the same data distribution.

An equivalent formulation is

alpha[k] ~ normal(0, sigma);
y[n] ~ normal(mu + alpha[k], sigma);

Now it’s easy to see that adding a gamma global intercept is not identified.

If you want a gentle introduction, I recommend my own:

Jumping into this kind of parameterization mess gets ugly quickly in both Bayesian and frequentist regression. The main things you want to learn about regression will hold in both Bayesian and non-Bayesian approaches. Then, I tried to write a gentle introduction to hierarchical models and parameterizations here:

If it wasn’t clear, this is a symmetric alternative to pinning an effect to zero to identify a regression. the new sum-to-zero parameterization we have is very nice computationally. This is something that works the same way to identify a likelihood in both Bayesian and frequentist statistics. In frequentist stats, you can make the same regularization argument that the sum-to-zero approach is symmetric.