An introduction to modeling in Stan

Building a binomial model

Welcome to the fourth workshop of the BayesCog course!

Purpose of this workshop

In this tutorial, we’ll take the next step in our Bayesian journey by implementing our globe-tossing model using Stan, a powerful probabilistic programming language. We’ll revisit the same binomial problem, but instead of using grid approximation, we’ll program our model in Stan to sample from the posterior more efficiently.

By the end of this workshop, you will be able to:

  • Write a Stan program with data, parameters and model blocks for a binomial model
  • Translate a mathematical model (likelihood + prior) into Stan syntax and fit it from R using RStan
  • Diagnose convergence using R-hat, effective sample size and trace/rank plots
  • Explain why HMC/NUTS sampling scales to multi-parameter models where grid approximation does not

These are the foundational skills you’ll reuse to implement more complex Bayesian models in the workshops that follow.

Working directory for this workshop

Model code and R scripts for this workshop are once again located in the (/workshops/02.binomial_globe) directory. Remember to use the R.proj file within each folder to avoid manually setting directories!