*Statistical rethinking* with brms, ggplot2, and the tidyverse

*version 1.2.0*

*2020-10-11*

# This is a love letter

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, 2020a) when doing Bayesian regression in R. It’s just spectacular. I also prefer plotting with ggplot2 (Wickham, 2016; Wickham, Chang, et al., 2020), and coding with functions and principles from the tidyverse (Wickham, 2019; Wickham, Averick, et al., 2019).

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.

## Why this?

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, 2020). 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.

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, François, 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, Gabry, et al., 2019; Vehtari et al., 2017; Yao et al., 2018), bayesplot (Gabry et al., 2019; Gabry & Mahr, 2019), and tidybayes (Kay, 2020b).

## 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, 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 (2019) *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.

## How to use and understand this project

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).

- R code blocks and their output appear in a gray background. E.g.,

`## [1] FALSE`

- Functions are in a typewriter font and followed by parentheses, all atop a gray background (e.g.,
`brm()`

). - 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.,`tidybayes::mode_hdi()`

). - R objects, such as data or function arguments, are in typewriter font atop gray backgrounds (e.g.,
`chimpanzees`

,`.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`m`

with a`b`

to stand for the brms package.

## You can do this, too

This project is powered by Yihui Xie’s (2020) 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 (2020) *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 on GitHub at https://github.com/ASKurz/Statistical_Rethinking_with_brms_ggplot2_and_the_tidyverse.

## We have updates

For a brief rundown of the version history, we have:

### Version 0.9.0.

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.

### Version 1.0.0.

In April 19, 2019 came the 1.0.0 version. Some of the major changes were:

- All models were refit with brms, 2.8.0.
- Adopting the
`seed`

argument 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
`coeftab()`

function. - 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.3.1 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).

### Version 1.0.1.

In May 5, 2019 came the 1.0.1 version, which finally added a PDF version of the book. Other noteworthy changes included:

- 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 et al., 2019)

### Version 1.1.0.

In March 1, 2020 came the 1.1.0 version. Noteworthy changes were:

- substantial expansions to Sections 10.3.1 (multinomial regression) and 11.3.2 (negative binomial regression),
- the addition of a new section in Chapter 15 (15.9) encouraging others to code in public,
- refitting all models with the current official version of brms, version 2.12.0,
- discussions (Section 8.3.2) on the new
`Bulk_ESS`

and`Tail_ESS`

summaries of HMC effective sample size (Vehtari, Gelman, et al., 2019), - saving all fits as external files in the new GitHub fits folder, primarily with the
`file`

argument, - extensive use of the patchwork package (Pedersen, 2019) for combining ggplots,
- improving/updating some of the tidyverse code (e.g., using
`tidyr::crossing()`

), - updates to the
`brms::custom_family()`

-related code in Section 11.3.1 to match brms 2.11.0 updates, - replacing the depreciated
`brms::marginal_effects()`

with`brms::conditional_effects()`

(see issue #735), - replacing the depreciated
`brms::stanplot()`

with`brms::mcmc_plot()`

, - increased the plot resolution with
`fig.retina = 2.5`

, - refreshed hyperlinks, and
- various typo corrections.

### Version 1.2.0.

Welcome to version 1.2.0! Noteworthy changes include:

- the correct solution to the first multinomial model in Section 10.16;
- a coherent workflow for the Gaussian process model from Section 13.4;
- corrections to some of the post-processing workflows for the measurement-error models in Section 14.1;
- refitting all models with the current official version of brms, version 2.13.5;
- improved in-text citations and reference sections using BibTex (
*BibTeX*, 2020), Better BibTeX (*Better BibTeX for Zotero*, 2020), and zotero (*Zotero | Your Personal Research Assistant*, 2020); and - minor prose, hyperlink, and code edits throughout.

## Is this material obsolete?

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, 2020b). Use whatever you find helpful.

## Thank-you’s are in order

Before we move on, I’d like to thank the following for their helpful contributions:

- Shaan Amin (@Shaan-Amin),
- Louis Bliard (@lbiard),
- Paul-Christian Bürkner (@paul-buerkner),
- Joseph V. Casillas (@jvcasillas),
- Andrew Collier (@datawookie),
- Marco Colombo (@mcol),
- Jeff Hammerbacher (@hammer),
- Matthew Kay (@mjskay),
- Katrin Leinweber (@katrinleinweber),
- TJ Mahr (@tjmahr),
- Federico Marini (@federicomarini),
- Stijn Masschelein (@stijnmasschelein),
- JungSu Oh (@js-oh),
- Colin Quirk (@colinquirk),
- Rishi Sadhir (@RishiSadhir),
- Jon Spring (@jonspring),
- Phil Straforelli (@pstraforelli),
- Richard Torkar (@torkar),
- Aki Vehtari (@avehtari), and
- Matti Vuorre (@mvuorre).

### References

*Better BibTeX for zotero :: Better BibTeX for zotero*. (2020). https://retorque.re/zotero-better-bibtex/

*BibTeX*. (2020). http://www.bibtex.org/

Bryan, J., the STAT 545 TAs, & Hester, J. (2020). *Happy Git and GitHub for the useR*. https://happygitwithr.com

Bürkner, P.-C. (2017). brms: An R package for Bayesian multilevel models using Stan. *Journal of Statistical Software*, *80*(1), 1–28. https://doi.org/10.18637/jss.v080.i01

Bürkner, P.-C. (2018). Advanced Bayesian multilevel modeling with the R package brms. *The R Journal*, *10*(1), 395–411. https://doi.org/10.32614/RJ-2018-017

Bürkner, P.-C. (2020a). *brms: Bayesian regression models using ’Stan’*. https://CRAN.R-project.org/package=brms

Carpenter, B., Gelman, A., Hoffman, M. D., Lee, D., Goodrich, B., Betancourt, M., Brubaker, M., Guo, J., Li, P., & Riddell, A. (2017). Stan: A probabilistic programming language. *Journal of Statistical Software*, *76*(1). https://doi.org/10.18637/jss.v076.i01

Gabry, J., & Mahr, T. (2019). *bayesplot: Plotting for Bayesian models*. https://CRAN.R-project.org/package=bayesplot

Gabry, J., Simpson, D., Vehtari, A., Betancourt, M., & Gelman, A. (2019). Visualization in Bayesian workflow. *Journal of the Royal Statistical Society: Series A (Statistics in Society)*, *182*(2), 389–402. https://doi.org/10.1111/rssa.12378

Gelman, A., Goodrich, B., Gabry, J., & Vehtari, A. (2019). R-squared for Bayesian regression models. *The American Statistician*, *73*(3), 307–309. https://doi.org/10.1080/00031305.2018.1549100

Grolemund, G., & Wickham, H. (2017). *R for data science*. O’Reilly. https://r4ds.had.co.nz

Healy, K. (2018). *Data visualization: A practical introduction*. Princeton University Press. https://socviz.co/

Henry, L., & Wickham, H. (2020). *purrr: Functional programming tools*. https://CRAN.R-project.org/package=purrr

Kay, M. (2020b). *tidybayes: Tidy data and ’geoms’ for Bayesian models*. http://mjskay.github.io/tidybayes

Kurz, A. S. (2020b). *Statistical rethinking with brms, ggplot2, and the tidyverse: Second edition* (version 0.0.3). https://bookdown.org/content/4857/

Legler, J., & Roback, P. (2019). *Broadening your statistical horizons: Generalized linear models and multilevel models*. https://bookdown.org/roback/bookdown-bysh/

McElreath, R. (2015). *Statistical rethinking: A Bayesian course with examples in R and Stan*. CRC press. https://xcelab.net/rm/statistical-rethinking/

McElreath, R. (2020a). *rethinking R package*. https://xcelab.net/rm/software/

McElreath, R. (2020b). *Statistical rethinking: A Bayesian course with examples in R and Stan* (Second Edition). CRC Press. https://xcelab.net/rm/statistical-rethinking/

Pedersen, T. L. (2019). *patchwork: The composer of plots*. https://CRAN.R-project.org/package=patchwork

Peng, R. D. (2019). *R programming for data science*. https://bookdown.org/rdpeng/rprogdatascience/

R Core Team. (2020). *R: A language and environment for statistical computing*. R Foundation for Statistical Computing. https://www.R-project.org/

Vehtari, A., Gabry, J., Magnusson, M., Yao, Y., & Gelman, A. (2019). *loo: Efficient leave-one-out cross-validation and WAIC for bayesian models*. https://CRAN.R-project.org/package=loo

Vehtari, A., Gelman, A., & Gabry, J. (2017). Practical Bayesian model evaluation using leave-one-out cross-validation and WAIC. *Statistics and Computing*, *27*(5), 1413–1432. https://doi.org/10.1007/s11222-016-9696-4

Vehtari, A., Gelman, A., Simpson, D., Carpenter, B., & Bürkner, P.-C. (2019). Rank-normalization, folding, and localization: An improved \(\widehat{R}\) for assessing convergence of MCMC. *arXiv Preprint arXiv:1903.08008*. https://arxiv.org/abs/1903.08008?

Wickham, H. (2016). *ggplot2: Elegant graphics for data analysis*. Springer-Verlag New York. https://ggplot2-book.org/

Wickham, H. (2019). *tidyverse: Easily install and load the ’tidyverse’*. https://CRAN.R-project.org/package=tidyverse

Wickham, H. (2020). *The tidyverse style guide*. https://style.tidyverse.org/

Wickham, H., Averick, M., Bryan, J., Chang, W., McGowan, L. D., François, R., Grolemund, G., Hayes, A., Henry, L., Hester, J., Kuhn, M., Pedersen, T. L., Miller, E., Bache, S. M., Müller, K., Ooms, J., Robinson, D., Seidel, D. P., Spinu, V., … Yutani, H. (2019). Welcome to the tidyverse. *Journal of Open Source Software*, *4*(43), 1686. https://doi.org/10.21105/joss.01686

Wickham, H., Chang, W., Henry, L., Pedersen, T. L., Takahashi, K., Wilke, C., Woo, K., Yutani, H., & Dunnington, D. (2020). *ggplot2: Create elegant data visualisations using the grammar of graphics*. https://CRAN.R-project.org/package=ggplot2

Wickham, H., François, R., Henry, L., & Müller, K. (2020). *dplyr: A grammar of data manipulation*. https://CRAN.R-project.org/package=dplyr

Wilke, C. O. (2019). *Fundamentals of data visualization*. https://clauswilke.com/dataviz/

Xie, Y. (2020). *bookdown: Authoring books and technical documents with R Markdown*. https://CRAN.R-project.org/package=bookdown

Xie, Y., Allaire, J. J., & Grolemund, G. (2020). *R markdown: The definitive guide*. Chapman and Hall/CRC. https://bookdown.org/yihui/rmarkdown/

Yao, Y., Vehtari, A., Simpson, D., Gelman, A., & others. (2018). Using stacking to average Bayesian predictive distributions (with discussion). *Bayesian Analysis*, *13*(3), 917–1007. https://doi.org/10.1214/17-BA1091

*Zotero | Your personal research assistant*. (2020). https://www.zotero.org/