How to brms?
Let’s assume that our data \(y\) is real valued and our predictor is “Age” and a group number.
We would like to model this data according to the real-valued predictor age
, as well as the categorical predictor grp
. What can go wrong?
Encode as.factor
categorical features
Let’s imagine we want to create a simple model of the form \(y \sim N(\mu, \sigma)\) where \(\mu = a_i + b \cdot Age\), where \(a_i\) is the intercept for i-th group. That can be easily encoded in the brms formula
but it’s crucial that the data is encoded accordingly
and then fit
Examine the posterior
Let’s say that we fit our model without caring much about enconding the data
and examine the posterior samples with posterior_summary
The estimate for the effect of group is wrong. We need to refit the model using the encoded data.
Fit model to new data?
How to fit an existing model to new data? Use ` update`.
Generate Stan code
This is a great feature that helps understand the model and automate a lot of coding
How to define the model?
Let’s imagine we want to create a simple model of the form \(y \sim N(\mu, \sigma)\) where \(\mu = a + b \cdot Age\). That model can be defined in two ways
The number 1
can be ommitted in this case. To confirm it run the following code, does it output TRUE
?
If instead we wanted \(\mu = b \cdot Age\) we would write y ~ 0 + age
, to indicate that the constant term is 0.
References
- See documentation for
update
- See documentation for
posterior_summary
- See documentation for
brmsformula