Adaptive priors other than Gaussians for group-level effects

Is there a way to use priors other than Gaussians for group-level effects in brms?

For example, here’s how we would fit a model with an adaptive gamma prior in rethinking:

fit1 <- map2stan(
        y ~ dpois(lambda),
        lambda <- theta[state_county]*n,
        theta[state_county] ~ dgamma2(mu, scale),
        mu ~ dgamma(12, 2),
        scale ~ dgamma(1.5, 1)
    data = d,
    start = list(theta = rep(5, nlevels(d$state_county)))

The data is grouped by “state_county”, “y” is the number of occurrences, and “n” is the exposure. The dgamma2 distribution is part of rethinking. It’s a re-parameterization of the usual gamma distribution which allows for more stable estimation of its parameters:

dgamma2(x, mu, scale) = dgamma(x, shape = mu/scale, scale = scale)

I know I could fit essentially the same model using a log link and an adaptive normal prior in brms:

fit2 <- brm(
    y ~ offset(log(n)) + (1 | state_county),
    family = poisson("log"),
    data = d

But I am specifically trying to fit it on the original scale with an adaptive gamma prior. (Also, sampling for this brms model is seriously slow compared to the rethinking one. I’m not sure why.)

For completeness, the data I’m trying to fit comes from Gelman’s website for the book “Bayesian Data Analysis”. It needed some minor reformatting to be easily read by R, so I’ve hosted the reformatted files here. And here is the code to import this data for use in these models:

d1 <- read.delim("gd80to84.txt")
d2 <- read.delim("gd85to89.txt")
cancer <- rbind(d1, d2)
cancer <- cancer[!$state) & cancer$pop > 0,]
cancer$state <- as.factor(toTitleCase(tolower(gsub(".", " ", cancer$state, fixed = TRUE))))
cancer$county <- as.factor(toTitleCase(tolower(gsub(".", " ", cancer$county, fixed = TRUE))))
cancer$state_county <- paste(cancer$state, "-", cancer$county)
cancer$state_county <- as.factor(cancer$state_county)
d <- data.frame(
    y = cancer$dc,
    n = cancer$pop * 1e-5,
    state_county = cancer$state_county
1 Like

Hi Antonio,

this is currently not possible in brms and likely never will. See for a discussion about this and my explanation why I don’t think it is worth implementing that in brms.


Hi Paul,

Thanks for the link, that definitely makes sense. Do you have an idea for why the brms model in my question samples so slowly? The rethinking model goes through 2000 iterations in under 10 minutes, but the brms model took over an hour and a half to do the same with max_treedepth = 15 and still had 12 transitions that exceeded that depth.

Thanks again,


I can only guess, but it may be a problem of too wide priors. Try to set informative priors on the intercept as well as on the standard deviations of the counties.