# Setting inverse gamma prior on random effects variance in brms

Hey all,
I don’t use brms often, so maybe this is a silly question, however I it is also a simple one and I cannot seem to find the answer.
I want to fit a random effects model using brms, I want to set an inverse gamma prior not on the standard deviation of the random effects but on the variances.
Is there any way to do so in brms?

my current code, which sets the prior on the standard deviations, looks like this

``````res = brm(formula = as.formula("V1 ~ 0 + (1 || V2)"),
data    = data,
prior   = c(set_prior("inv_gamma(1,1)" , class = "sd"),
set_prior("inv_gamma(1,1)", class = 'sigma')),
iter    =1000)
``````

It’s possible to hack this via `stanvars` (i.e. pass a flat prior on the sd, and then use `stanvars` to pass the prior statement including the Jacobian adjustment).

But first, I just want to make sure that you’re aware that Stan will not recognize or exploit conjugacy, and there’s absolutely no computational advantage to this particular choice of prior. So the only reason to do this would be if you have specific prior knowledge that is best represented by an inverse gamma, or if you need to re-implement a specific model from somewhere else, including the priors.

3 Likes

Thanks! I’m re-implementing a model from somewhere else including priors, how would this work specifically @jsocolar ?

Thanks to a lot of help by @jsocolar, we arrived at the following code, which should do the trick

``````brm(formula = as.formula("V1 ~ 0 + (1 || V2)"),
data    = data,
prior = set_prior("", class = "sd") + set_prior("", class = "sigma"),
stanvars = stanvar(scode = "sigma^2 ~ inv_gamma(1,1);",
block = "model") + stanvar(scode = "target += 2*sigma;",
block = "model")+
stanvar(scode = "sd_1^2 ~ inv_gamma(1,1);",
block = "model") + stanvar(scode = "target += 2*sd_1;",
block = "model"),
iter    =1000)
``````
1 Like