2 + 2 == 5[1] FALSE
version 0.5.0
This ebook 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 Paul Bürkner’s brms package (Bürkner, 2017, 2018, 2022), which makes it easy to fit Bayesian regression models in R (R Core Team, 2022) using Hamiltonian Monte Carlo. 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.
Are you looking for the companion ebook for McElreath’s 1st edition? Click here.
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 Roback and Legler (2021) 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. Another nice alternative is Baumer, Kaplan, and Horton’s (2021), Modern data science with R.
However, 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 second edition of McElreath’s text. I follow the structure of his text, chapter by chapter, translating his analyses into brms and tidyverse-style code. However, some of the sections in the text are composed entirely of equations or 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::median_qi()).chimpanzees, .width = 0.5).m4.1 (i.e., the first model of 4 Geocentric Models). 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("ape", "bayesplot", "brms", "broom", "dagitty", "devtools", "flextable", "GGally", "ggdag", "ggmcmc", "ggrepel", "ggthemes", "ggtree", "ghibli", "gtools", "invgamma", "loo", "patchwork", "posterior", "psych", "rcartocolor", "Rcpp", "remotes", "rstan", "santoku", "StanHeaders", "statebins", "tidybayes", "tidyverse", "viridis", "viridisLite", "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("GuangchuangYu/ggtree")
devtools::install_github("rmcelreath/rethinking")
devtools::install_github("UrbanInstitute/urbnmapr")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 0.1.0 version of this project in November 24, 2020. It was the first full-length and nearly complete draft including material from all the 17 chapters in McElreath’s source material. All brms models were fit with version 2.14.0+.
On December 2, 2020, I released a mini update designed to
sample_n() code with slice_sample(), caught by Randall Pruim;On March 16, 2021, I released version 0.2.0, which included the following major improvements:
s() function (Summary First bonus: Smooth functions with brms::s()), thanks to Gavin Simpson;fitted()-based random draws for the bonus material in Bonus: Let’s use fitted() this time, thanks to an exchange with Ladislas Nalborczyk;geom_area() throughout the book, thanks to insights from Randall Pruim;On September 28, 2022, I released version 0.3.0, which included the following major improvements:
as_draws_df()-oriented workflow to replace the depreciated posterior_samples() function;ub argument;ub argument;sim_happiness() function in Collider bias, thanks to Randall PruimOn January 26, 2023, I released version 0.4.0, which 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); andWelcome to version 0.5.0! Noteworthy changes include:
|>);dplyr::lag(), thanks to James Fern;Some areas of the book could use some fleshing out. The sections I’m particularly anxious to improve are
If you have insights on how to improve these or any other sections, please share your thoughts on GitHub at https://github.com/ASKurz/Statistical_Rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed/issues. The contribution guidelines for this book are listed at https://github.com/ASKurz/Statistical_Rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed/blob/master/CONTRIBUTING.md.
I’d like to thank the following for their helpful contributions:
Science is better when we work together.
This book is licensed under the Creative Commons Zero v1.0 Universal license. 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{kurzStatisticalRethinkingSecondEd2026,
title = {Statistical rethinking with brms, ggplot2, and the tidyverse: {{Second}} edition},
author = {Kurz, A. Solomon},
year = {2026},
month = {jan},
edition = {version 0.5.0},
url = {https://solomon.quarto.pub/sr2/}
}
Comments