You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/tutorials/r_brms/brms_eng/workshop_1_mcmc_en_brms_eng.Rmd
+174-3Lines changed: 174 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -632,7 +632,7 @@ The model is fitted using the `brm()` function. The syntax is very similar to fu
632
632
-`file` and `file_refit` to save the model object after it has been fitted. If you run the code again and the model has already been saved, `brm()` will simply load this model instead of refitting it.
So we need to estimate two parameters: $\beta_0$ and $\beta_1$ We use the same MCMC parameters as before. The only thing we need to adjust is the choice `family = poisson()`.
# Fit Poisson model with random intercept per site
905
905
fit_poisson2 <- brm(
906
906
formula = sp_rich ~ habitat + (1|site),
@@ -1062,6 +1062,177 @@ comp_waic %>%
1062
1062
Both based on the PPC and the comparisons with different model selection criteria, we can conclude that the second Poisson model with random intercepts fits the data best. In principle, we could have expected this based on our own intuition and the design of the study, i.e. the use of the Poisson distribution to model numbers and the use of random intercepts to control for a hierarchical design (habitats nested within sites).
1063
1063
1064
1064
1065
+
## Deep Dive: `rstan`
1066
+
### Stan: What? Why?!
1067
+
The `brms` package is a convenience wrapper for the `rstan` package, which in turn ports `stan` functionality to R.
1068
+
Stan is a modeling framework written in the `C` programming language, which implements many probabilistic ("Bayesian") modeling tools.
1069
+
More info can be found on [the Stan website](https://mc-stan.org).
1070
+
1071
+
1072
+
The advantage of `brms` is usability: many functions work out-of-the-box, with reasonable default values, and a syntax that is similar to what frequentists are habituated to.
1073
+
However, the relative ease-of-use comes at the cost of flexibility, and do some degree, readability.
1074
+
1075
+
In contrast, Stan and `rstan` lean more to the mathematical formulation of models.
1076
+
Every aspect of the model has to be explicitly set, which can be an advantage (e.g. if you face non-standard use cases), or disadvantage (e.g. if you secify models in non-optional ways).
1077
+
1078
+
1079
+
To briefly give an impression, we will build the same models as above, using the Stan framework.
1080
+
1081
+
1082
+
```{r}
1083
+
library("rstan")
1084
+
conflicted::conflicts_prefer(rstan::extract)
1085
+
conflicted::conflicts_prefer(brms::loo)
1086
+
```
1087
+
1088
+
### Model Definition
1089
+
RMarkdown can handle `stan` code chunks, though more general model definition is outsourced to a separate "*.stan" file.
1090
+
The simple poisson model resembles [one of the `stan`-dard examples](https://mc-stan.org/docs/stan-users-guide/posterior-prediction.html#posterior-prediction-for-regressions), which you can refer to for all further details and more.
Sampling does pretty much the same as above, since at the core, `brms` is just `stan`.
1137
+
1138
+
```{r stan_sampling}
1139
+
stan_poisson_fit <- sampling(
1140
+
stan_poisson_model,
1141
+
list(
1142
+
N = nrow(ants_df),
1143
+
habitat_obs = as.integer(ants_df$habitat)-1,
1144
+
sp_rich = ants_df$sp_rich
1145
+
),
1146
+
iter = niter,
1147
+
chains = nchains,
1148
+
cores = nparallel
1149
+
)
1150
+
1151
+
stan_poisson_fit
1152
+
```
1153
+
1154
+
1155
+
A convenient way of quickly inspecting model fits is `shinystan`.
1156
+
It opens a browser with shiny plots.
1157
+
1158
+
```{r eval=FALSE}
1159
+
library("shinystan")
1160
+
launch_shinystan(stan_poisson_fit)
1161
+
```
1162
+
1163
+
1164
+
Behold: model outcome is exactly as above.
1165
+
In the present case, the benefit from turning to `stan` is very limited.
1166
+
In other cases, it might pay off.
1167
+
1168
+
Know that Stan is there for you, do not hesitate to turn to its extensive documentation, and do not fear to give it a try!
1169
+
1170
+
1171
+
### Homework: Hierarchical Model
1172
+
To take your modeling skills even further, you may implement and sample the "random intercept" model.
1173
+
In "Bayesian" terms, the [general terminology is "hierarchical" model](https://mc-stan.org/docs/stan-users-guide/regression.html#hierarchical-regression).
1174
+
1175
+
1176
+
Below is the code which considers site-specific intercepts.
1177
+
It works slightly different from the `+(1|site)` approach above: the upper one is an "offset" parametrization, whereas this time we use a hyperprior.
1178
+
These are two common strategies often worth interchanging, with marginal or substantial effects on sampler performance.
With Stan, po(i)ssibilities are almost endless - don't get lost in model building!
1234
+
1235
+
1065
1236
# Final model results
1066
1237
1067
1238
When we look at the model fit object, we see results that are similar to results we see when we fit a frequentist model. On the one hand we get an estimate of all parameters with their uncertainty, but on the other hand we see that this is clearly the output of a Bayesian model. We get information about the parameters we used for the MCMC algorithm, we get a 95% credible interval (CI) instead of a confidence interval and we also get the $\hat{R}$ value for each parameter as discussed earlier.
0 commit comments