Model fit checks
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
. 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
Posterior summary
examine the posterior samples with posterior_summary
we can also plot the posterior samples for each parameter
Posterior predictive checks
pp_check
is a handy function for visualizing the predictions of the model.
By default it ouputs an overlayed density plot. Another handy visualization is the predictive intervals for each datapoint against it’s true value.
Type pp_check(type = xyz)
for a full list of options.
Let’s assume we have 10 new datapoints, together with their true values,
we can get redictions from our model
Predictions
Each data point gets a predictive distribution that is implied by the posterior samples. Let’s say we have one data point
Our model predicts the following value
To see the full values of values type
Recall that the model is \(y \sim N(\mu, \sigma)\) and hence the predictions incorporate the uncertainty that stems from the variance \(\sigma\). There is a special function, called fitted
for the prediction of the average \(\mu = a + b * Age\)
Note how the estimate is very close to the value proivded by predict
. The difference lies in the predicted percintile intervals, which are much narrower for the fitted
function, as they should be.
Finally there is marginal_effects
which visualizes the effect of the values of the features on the observed outcome $y$. For example we can see the effect of the group as follows
there is mode options to visualize interactions or other details
Model comparison
Let’s say we want to examine if ignoring the age effect makes for a better model. We would model it as follows
One method to compare the models is to compute the approximate leave-one-out cross-validation. The smaller the value the better. Hence we see that model 2 wins here.
Another way to compare models is to compute their weights if they were to be combined
Another method is WAIC, again the smaller the better.
There is also a handy method for k-fold cross validation.
Model Averaging
If we wanted to extract posterior samples from a weighted average of the two models we can use
And to retrieve direct predictions
References
- See documentation of pp_check
- For examples of bayesplot, the package behind
pp_check
- See documentation of predict
- See documentation of fitted
- See documentation of marginal_effects
- See documentation of loo
- See documentation of kfold
- See documentation of posterior_average
- See documentation of pp_average