# Non-centered prior for intercept

Am trying to get a bit better at working with priors. But I am really struggling to conceptualise how to specify priors for a multi-level negative-binomial model.

Suppose we follow 30 groups (`group`) each for 10 years (`year`), and measure the number events (`n`) that occur in the population (`pop`)

``````library(tidyverse)
library(tidybayes)
library(brms)

set.seed(1234)

#make up some data
d <- tibble(group = 1:30,
pop = round(runif(n=30, min=10000, max=100000), digits=0)) %>%
crossing(year=1:10) %>%
mutate(n = rnbinom(n=300, mu=50, size=10))
``````

Now we can define a model with a non-centred intercept, and sample from the prior only:

``````#model with non-centred prior
m_prior <- brm(
n ~ 0 + Intercept + year + (1 | group) + offset(log(pop)),
data = d,
family = negbinomial(),
sample_prior = "only",
prior = prior(normal(log(50), 10), class = b, coef = Intercept) +
prior(gamma(0.01, 0.01), class = shape) +
prior(normal(0, 0.01), class = b) +
prior(exponential(1), class=sd)
)
``````

Our prior for the intercept seems reasonable for each group-year strata. (We could do a lot better, but will leave like this for this example).

``````#prior predictive plot for groups
newdata = d,
re_formula = ~( 1 | group)) %>%
ggplot() +
stat_halfeye(aes(x=year, y=.epred)) +
geom_point(data = d, colour="red", shape=2,
aes(x=year, y=n)) +
scale_y_log10(label=comma) +
facet_wrap(group~.)
``````

But obviously, when we try to predict the total number of events per year, our prior hugely underestimates this, and will cause problems for the posterior when we come to model the data.

``````#prior predictive plot for overall events per year
newdata = d,
re_formula = NA) %>%
ggplot() +
stat_halfeye(aes(x=year, y=.epred)) +
geom_point(data = d %>% group_by(year) %>% summarise(.epred=sum(n)), colour="red", shape=2,
aes(x=year, y=.epred)) +
scale_y_log10(label=comma)
``````

So, my question is: given we have prior knowledge of the total number of events per year, and how they likely are split by group, how can we specify priors to capture this.

(apologies if this is a daft question, and I should just revert to centred parameterisation!)

Thanks