How to set priors in `brms`
Generate simple data
Let’s assume that our data $y$ is real valued and our predictor is “Age”.
Understanding the priors
Let’s imagine we want to create a simple model of the form \(y \sim N(\mu, \sigma)\) where \(\mu = a + b \cdot Age\).
To understand the model we need to use get_prior
and make_stancode
. We need to explore the output of the two to learn how to set custom priors.
The best way to enter priors is to save the output dataframe of get_priors
and edit it directly. Let’s first explore the resulting Stan code to understand where the priors will be set
We see that parameters are b; // population-level effects
, real temp_Intercept; // temporary intercept
, and real<lower=0> sigma // residual SD
. Let’s match that to the dataframe of priors
Let’s give normal priors to the
And check that the generated Stan code reflects the change
Note that if we had made a typo or passed a distribution that is not part of Stan we would get an error. The following code is not evaluated, if it was it would raise an error.
References
- See documenation for
get_prior