2 + 2 == 5[1] FALSE
version 1.4.0
I love McElreath’s (2015) 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 (Bürkner, 2017, 2018, 2022) when doing Bayesian regression in R. It’s just spectacular. I also prefer plotting with ggplot2 (Wickham, 2016; Wickham et al., 2022), and coding with functions and principles from the tidyverse (Wickham et al., 2019; Wickham, 2022).
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.
Are you looking for the companion ebook for McElreath’s 2nd edition? Click here.
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 (McElreath, 2020a). The rethinking package is a part of the R ecosystem, which is great because R is free and open source (R Core Team, 2022). McElreath has made the source code for rethinking publicly available, too. Since he completed his text, many other packages have been developed to help users of the R ecosystem interface with Stan (Carpenter et al., 2017). Of those alternative packages, I think Bürkner’s brms is the best for general-purpose Bayesian data analysis. Its 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. Yet at the time I released the first version of this ebook, there were no textbooks on the market that highlight the brms package, which seemed like an evil worth correcting. Though for a noteworthy new contribution, see Nicenboim et al. (2022).
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, such as dplyr (Wickham et al., 2020) and purrr (Henry & Wickham, 2020), 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 tidyverse style of coding is generally easier to learn and sufficiently powerful that these packages can accommodate the bulk of your wrangling data needs. I also find tidyverse-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 (Vehtari et al., 2017; Vehtari et al., 2022; Yao et al., 2018), bayesplot (Gabry et al., 2019; Gabry & Mahr, 2022), and tidybayes (Kay, 2022).
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, which suggests you have at least a 101-level foundation in statistics. If you’re rusty, consider checking out the free text books by Legler and Roback (2019) or Navarro (2019) 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 (2022) R programming for data science. For an introduction to the tidyvese-style of data analysis, the best source I’ve found is Grolemund and Wickham’s (2017) R for data science (R4DS), 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.
This project is not meant to stand alone. It’s a supplement to the first edition of McElreath’s 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 (2018) Data visualization: A practical introduction, Wilke’s (2019) Fundamentals of data visualization or Wickham’s (2016) ggplot2: Elegant graphics for data analysis.
In this project, 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).
2 + 2 == 5[1] FALSE
brm()).:: between the package name and the function (e.g., tidybayes::mode_hdi()).chimpanzees, .width = .5).m4.1 (i.e., the first model of Chapter 4). I primarily followed that convention, but replaced the m with a b to stand for the brms package.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 will probably take a few minutes to finish. Go make yourself a coffee.
packages <- c(
"bayesplot", "brms", "broom", "devtools", "GGally", "ggbeeswarm", "ggExtra", "ggmcmc",
"ggrepel", "ggthemes", "ghibli", "invgamma", "loo", "patchwork", "posterior",
"rcartocolor", "tidybayes", "tidyverse", "tigris", "viridis", "wesanderson")
install.packages(packages, dependencies = T)A few 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("EdwinTh/dutchmasters")
devtools::install_github("gadenbuie/ggpomological")
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:
For a brief rundown of the version history, we have:
I released the initial 0.9.0 version of this project in September 26, 2018. It was a full first draft and set the stage for all others.
In April 19, 2019 came the 1.0.0 version. Some of the major changes were:
seed argument within the brm() function made the model results more reproducible.coeftab() function.mi() function in Section 14.2.1)In May 5, 2019 came the 1.0.1 version, which finally added a PDF version of the book. Other noteworthy changes included:
In March 1, 2020 came the 1.1.0 version. Noteworthy changes were:
Bulk_ESS and Tail_ESS summaries of HMC effective sample size (Vehtari et al., 2021),file argument,tidyr::crossing()),brms::custom_family()-related code in Beta-binomial to match brms 2.11.0 updates,brms::marginal_effects() with brms::conditional_effects() (see issue #735),brms::stanplot() with brms::mcmc_plot(), andfig.retina = 2.5.I released the 1.2.0 version in October 11, 2020. Noteworthy changes included:
I released the 1.3.0 version in January 25, 2023. Noteworthy changes included:
tidyr::expand() with a more appropriate tidyr::expand_grid() workflow, thanks to insights from Desislava Petkova;linewidth argument for several ggplot2 geoms (see here);Welcome to version 1.4.0! Noteworthy changes include:
The first edition of McElreath’s text now has a successor, Statistical rethinking: A Bayesian course with examples in R and Stan: Second Edition (McElreath, 2020b). Though the second edition kept a lot of the content from the first, it is a substantial revision and expansion. The book is longer and wildly ambitious in its scope. To be blunt, I believe McElreath moved to quickly in his revision and I suspect many applied readers might need to reference the first edition from time to time to time just to keep up with the content of the second. If McElreath ever releases a third edition, I hope he finds a happy compromise between the first two. So in the meantime, I believe there’s a place for both first and second editions of his text. Accordingly, I believe this ebook should not be considered outdated relative to my ebook translation of the second edition (Kurz, 2026). Use whatever you find helpful.
Before we move on, I’d like to thank the following for their helpful contributions:
This book is licensed under the GNU General Public License v3.0. You can learn the details, 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 citation information:
@book{kurzStatisticalRethinking2026,
title = {Statistical rethinking with brms, ggplot2, and the tidyverse},
author = {Kurz, A. Solomon},
year = {2026},
month = {jan},
edition = {version 1.4.0},
url = {https://solomon.quarto.pub/sr/}
}
Comments