Statistical rethinking 2 with rstan and the tidyverse

version 0.0.3

Author

A. Solomon Kurz

Published

September 12, 2024

Preface

What and why

This book is based on the second edition of Richard McElreath’s (2020) text, Statistical rethinking: A Bayesian course with examples in R and Stan. My contributions show how to fit the models he covered with rstan (Stan Development Team, 2024a), which allows one to fit Bayesian models in R (R Core Team, 2022) using the Hamiltonian Monte Carlo algorithms provided by Stan (Stan Development Team, 2024b). I also prefer plotting and data wrangling with the packages from the tidyverse (Wickham et al., 2019; Wickham, 2022), so we’ll be using those methods, too.

This is a sibling book of my translation of McElreath’s second edition into brms code (Kurz, 2023), which you can find by clicking here.

How this compares with the brms translation, and what this means for readers

I think rstan is harder to use than brms. Its syntax is more technical, and it doesn’t follow many of the conventions used by other popular statistics packages, such as stats and lme4. rstan is also more general than brms, allowing for a variety of unconventional and bespoke models. Therefore I’m presuming the readership for this book will be intermediate to advanced in both statistical skills, and general R programming skills. I will still take a pedagogical tone in this book, but my pace will be a little faster than in my other books.

Also, because this is sibling book to my brms translation, I will not replicate all my previous work. Rather, the code in this book will be more tightly focused on reproducing the models and primary analyses with an rstan + tidyverse framework. The supporting prose will be sparser, and I will have fewer quotes from the text. If you want a richer engagement with McElreath’s text, you probably want my brms translation instead.

R setup

To get the full benefit from this ebook, you’ll need some software. Happily, everything will be free (provided you have access to a decent personal computer and an good internet connection).

First, you’ll need to install R, which you can learn about at https://cran.r-project.org/.

Though not necessary, your R experience might be more enjoyable if done through the free RStudio interface, which you can learn about at https://posit.co/products/open-source/rstudio/.

Once you have installed R, execute the following to install the bulk of the add-on packages. This may take a few minutes to finish. Go make yourself a coffee.

packages <- c("bayesplot", "dagitty", "devtools", "GGally", "ggdag", "patchwork", "posterior", "remotes", "rstan", "tidybayes", "tidyverse", "tigris")

install.packages(packages, dependencies = TRUE)

A couple of the other packages are not officially available via the Comprehensive R Archive Network (CRAN; https://cran.r-project.org/). You can download them directly from GitHub by executing the following.

devtools::install_github("stan-dev/cmdstanr")
devtools::install_github("rmcelreath/rethinking")

It’s possible you’ll have problems installing some of these packages. Here are some likely suspects and where you can find help:

Conventions

As in my other books, I use a handful of formatting conventions gleaned from R4DS, The tidyverse style guide (Wickham, 2020), and R markdown: The definitive guide (Xie et al., 2020).

  • R code blocks and their output appear in a gray background. E.g.,
2 + 2 == 5
[1] FALSE
  • R and the names of specific package (e.g., rstan) are in boldface font.
  • Functions are in a typewriter font and followed by parentheses, all atop a gray background (e.g., stan()).
  • When I want to make explicit the package a given function comes from, I insert the double-colon operator :: between the package name and the function (e.g., posterior::as_draws_df()).
  • R objects, such as data or function arguments, are in typewriter font atop gray backgrounds (e.g., chimpanzees, .width = 0.89).
  • You can detect hyperlinks by their typical blue-colored font.

For the rstan code, I’m also taking cues from the Stan Program Style Guide section of the Stan User’s Guide. However, as I’m relatively new to rstan, don’t be surprised if you find some quirks an inconsistencies.

Warning

This book is a side-product of another project for which I needed to learn more about rstan. It was also a good opportunity to learn more about making a Quarto-based ebook (Allaire et al., 2024). For this 0.0.3 release, I’m only up through Chapter 13. I will continue to chip away on this as my schedule permits, but I am setting no goals and no deadlines. On the one hand, it would be nice to have another finished book in my portfolio; on the other hand, I really don’t like using rstan and I still much prefer a brms-based workflow. The remaining chapters in this book may find themselves fleshed out in quick succession, or this whole project may lay fallow. We’ll see…

Comments!

This is my first ebook with comments sections! Please play nice. I don’t plan on heavily monitoring the comments, but if needed I reserve the right to delete, block, and so on.

Thank-you’s are in order

I’d like to thank the following for their helpful contributions:

Many of these folks have shared rstan code that a directly helped make this book possible. I especially appreciate and recommend Arel-Bundock’s website Statistical Rethinking 2 with Stan and R, wherein you can find rendered Rmarkdown files with loads of rstan code on many of the same problems I’m tackling in this book. If you compare our works, you’ll see I have been heavily influenced by Arel-Bundock’s rstan style.

License and citation

This book is licensed under the Creative Commons Zero v1.0 Universal license, the details for which you can about here. In short, you can use my work. Just make sure you give me the appropriate credit the same way you would for any other scholarly resource. Here’s the basic citation information:

@book{kurzStatisticalRethinking2rstan2024,
  title = {Statistical rethinking 2 with rstan and the tidyverse},
  author = {Kurz, A. Solomon},
  year = {2024},
  month = {september},
  edition = {version 0.0.3},
  url = {https://solomon.quarto.pub/sr2rstan/},
  doi = {https://doi.org/10.5281/zenodo.13755933}
}