Statistical Rethinking with brms, ggplot2, and the tidyverse
This is a love letter
I love McElreath’s Statistical Rethinking text. It’s the entry-level textbook for applied researchers I spent years looking for. McElreath’s freely-available lectures on the book are really great, too.
However, I prefer using Bürkner’s brms package when doing Bayeian regression in R. It’s just spectacular. I also prefer plotting with Wickham’s ggplot2, and coding with functions and principles from the tidyverse, which you might learn about here or here.
So, this project is an attempt to reexpress the code in McElreath’s textbook. His models are re-fit with brms, the figures are reproduced or reimagined with ggplot2, and the general data wrangling code now predominantly follows the tidyverse style.
I’m not a statistician and I have no formal background in computer science. Though I benefited from a suite of statistics courses in grad school, a large portion of my training has been outside of the classroom, working with messy real-world data, and searching online for help. One of the great resources I happened on was idre, the UCLA Institute for Digital Education, which offers an online portfolio of richly annotated textbook examples. Their online tutorials are among the earliest inspirations for this project. We need more resources like them.
With that in mind, one of the strengths of McElreath’s text is its thorough integration with the rethinking package. The rethinking package is a part of the R ecosystem, which is great because R is free and open source. And McElreath has made the source code for rethinking publically available, too. Since he completed his text, many other packages have been developed to help users of the R ecosystem interface with Stan. Of those alternative packages, I think Bürkner’s brms is the best for general-purpose Bayesian data analysis. It’s flexible, uses reasonably-approachable syntax, has sensible defaults, and offers a vast array of post-processing convenience functions. And brms has only gotten better over time. To my knowledge, there are no textbooks on the market that highlight the brms package, which seems like an evil worth correcting.
In addition, McElreath’s data wrangling code is based in the base R style and he made most of his figures with base R plots. Though there are benefits to sticking close to base R functions (e.g., less dependencies leading to a lower likelihood that your code will break in the future), there are downsides. For beginners, base R functions can be difficult both to learn and to read. Happily, in recent years Hadley Wickham and others have been developing a group of packages collectively called the tidyverse. These tidyverse packages (e.g., dplyr, tidyr, purrr) were developed according to an underlying philosophy and they are designed to work together coherently and seamlessly. Though not all within the R community share this opinion, I am among those who think the tydyverse style of coding is generally easier to learn and sufficiently powerful that these packages can accommodate the bulk of your data needs. I also find tydyverse-style syntax easier to read. And of course, the widely-used ggplot2 package is part of the tidyverse, too.
To be clear, students can get a great education in both Bayesian statistics and programming in R with McElreath’s text just the way it is. Just go slow, work through all the examples, and read the text closely. It’s a pedagogical boon. I could not have done better or even closely so. But what I can offer is a parallel introduction on how to fit the statistical models with the ever-improving and already-quite-impressive brms package. I can throw in examples of how to perform other operations according to the ethic of the tidyverse. And I can also offer glimpses of some of the other great packages in the R + Stan ecosystem, such as loo, bayesplot, and tidybayes.
My assumptions about you
If you’re looking at this project, I’m guessing you’re either a graduate student, a post-graduate academic, or a researcher of some sort. So I’m presuming you have at least a 101-level foundation in statistics. If you’re rusty, consider checking out Legler and Roback’s free bookdown text, Broadening Your Statistical Horizons before diving into Statistical Rethinking. I’m also assuming you understand the rudiments of R and have at least a vague idea about what the tidyverse is. If you’re totally new to R, consider starting with Peng’s R Programming for Data Science. And the best introduction to the tidyvese-style of data analysis I’ve found is Grolemund and Wickham’s R for Data Science, which I extensively link to throughout this project.
That said, you do not need to be totally fluent in statistics or R. Otherwise why would you need this project, anyway? IMO, the most important things are curiosity, a willingness to try, and persistent tinkering. I love this stuff. Hopefully you will, too.
How to use and understand this project
This project is not meant to stand alone. It’s a supplement to McElreath’s Statistical Rethinking text. I follow the structure of his text, chapter by chapter, translating his analyses into brms and tidyverse code. However, some of the sections in the text are composed entirely of equations and prose, leaving us nothing to translate. When we run into those sections, the corresponding sections in this project will sometimes be blank or omitted, though I do highlight some of the important points in quotes and prose of my own. So I imagine students might reference this project as they progress through McElreath’s text. I also imagine working data analysts might use this project in conjunction with the text as they flip to the specific sections that seem relevant to solving their data challenges.
I reproduce the bulk of the figures in the text, too. The plots in the first few chapters are the closest to those in the text. However, I’m passionate about data visualization and like to play around with color palettes, formatting templates, and other conventions quite a bit. As a result, the plots in each chapter have their own look and feel. For more on some of these topics, check out chapters 3, 7, and 28 in R4DS, Healy’s Data Visualization: A practical introduction, or Wilke’s Fundamentals of Data Visualization.
- R code blocks and their output appear in a gray background. E.g.,
2 + 2 == 5
##  FALSE
- Functions are in a typewriter font and followed by parentheses, all atop a gray background (e.g.,
- 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.,
- R objects, such as data or function arguments, are in typewriter font atop gray backgrounds (e.g.,
.width = .5).
- You can detect hyperlinks by their typical blue-colored font.
- In the text, McElreath indexed his models with names like
m4.1(i.e., the first model of Chapter 4). I primarily followed that convention, but replaced the
bto stand for the brms package.
You can do this, too
This project is powered by Yihui Xie’s bookdown package, which makes it easy to turn R markdown files into HTML, PDF, and EPUB. Go here to learn more about bookdown. While you’re at it, also check out Xie, Allaire, and Grolemund’s R Markdown: The Definitive Guide. And if you’re unacquainted with GitHub, check out Jenny Bryan’s Happy Git and GitHub for the useR. I’ve even blogged about what it was like putting together the first version of this project.
The source code of the project is available here.
We have updates
I released the initial 0.9.0 version of this project in September 26, 2018. In April 19, 2019 came the 1.0.0 version. Some of the major changes were:
- All models were refit with the current official version of brms, 2.8.0.
- Adopting the
seedargument within the
brm()function made the model results more reproducible.
- The loo package was updated. As a consequence, our workflow for the WAIC and LOO changed, too.
- I improved the brms alternative to McElreath’s
- I made better use of the tidyverse, especially some of the purrr functions.
- Particularly in the later chapters, there’s a greater emphasis on functions from the tidybayes package.
- Chapter 11 contains the updated brms 2.8.0 workflow for making custom distributions, using the beta-binomial model as the example.
- Chapter 12 received a new bonus section contrasting different methods for working with multilevel posteriors.
- Chapter 14 received a new bonus section introducing Bayesian meta-analysis and linking it to multilevel and measurement-error models.
- With the help of others within the community, I corrected many typos and streamlined some of the code (e.g., dropped an unnecessary use of the
mi()function in section 14.2.1)
- And in some cases, I corrected sections that were just plain wrong (e.g., some of my initial attempts in section 3.3 were incorrect).
In response to some reader requests, we finally have a PDF version! Making that happen required some formatting adjustments, resulting in version 1.0.1. Noteworthy changes include:
- Major revisions to the LaTeX syntax underlying many of the in-text equations (e.g., dropping the “eqnarray” environment for “align*“)
- Adjusting some of the image syntax
- Updating the reference for the Bayesian \(R^2\) (Gelman, Goodrich, Gabry, & Vehtari, 2018)
Though we’re into version 1.0.1, there’s room for improvement. There are still two models that need work. The current solution for model 10.6 is wrong, which I try to make clear in the prose. It also appears that the Gaussian process model from section 13.4 is off. Both models are beyond my current skill set and friendly suggestions are welcome. In addition to modeling concerns, typos may yet be looming and I’m sure there are places where the code could be made more streamlined, more elegant, or just more in-line with the tidyverse style. Which is all to say, I hope to release better and more useful updates in the future.
Before we move on, I’d like to thank the following for their helpful contributions: Paul-Christian Bürkner (@paul-buerkner), Andrew Collier (@datawookie), Jeff Hammerbacher (@hammer), Matthew Kay (@mjskay), TJ Mahr (@tjmahr), Stijn Masschelein (@stijnmasschelein), Colin Quirk (@colinquirk), Rishi Sadhir (@RishiSadhir), Richard Torkar (@torkar), Aki Vehtari (@avehtari).