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(
alist(
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[!is.na(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
)