Doing Bayesian Data Analysis in brms and the tidyverse

version 1.2.0

Author

A. Solomon Kurz

Published

April 15, 2025

What and why

Kruschke began his text with “This book explains how to actually do Bayesian data analysis, by real people (like you), for realistic data (like yours).” In the same way, this project is designed to help those real people do Bayesian data analysis. My contribution is converting Kruschke’s JAGS and Stan code for use in Bürkner’s brms package (, , ), which makes it easier to fit Bayesian regression models in R () using Hamiltonian Monte Carlo. I also prefer plotting and data wrangling with the packages from the tidyverse (; ). So we’ll be using those methods, too.

This ebook is not meant to stand alone. It’s a supplement to the second edition of Kruschke’s () Doing Bayesian data analysis: A tutorial with R, JAGS, and Stan. Please give the source material some love.

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://rstudio.com/products/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", "coda", "cowplot", "cubelyr", "devtools", "fishualize", "GGally", "ggdist", "ggExtra", "ggforce", "ggmcmc", "ggridges", "ggthemes", "janitor", "lisa", "loo", "palettetown", "patchwork", "psych", "remotes", "rstan", "santoku", "scico", "tidybayes", "tidyverse")

install.packages(packages, dependencies = TRUE)

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.

remotes::install_github("clauswilke/colorblindr")
devtools::install_github("dill/beyonce")
devtools::install_github("ropenscilabs/ochRe")

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

We have updates

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

Version 0.1.0.

I released the 0.1.0 version of this project in February 17, 2020. It was the first [fairly] complete draft including material from all the chapters in Kruschke’s text. The supermajority of Kruschke’s JAGS and Stan models were fit brms 2.11.5. The results were saved in the fits folder on GitHub and most of the results are quite comparable to those in the original text. We also reproduced most of the data-related figures and tables and little subpoints and examples sprinkled throughout Kruschke’s prose.

Version 0.2.0.

The 0.2.0 update came in May 19, 2020. Noteworthy changes included:

Version 0.3.0.

The 0.3.0 update came in September 22, 2020. Noteworthy changes included:

Version 0.4.0.

The 0.4.0 update came in May 6, 2021. Noteworthy changes included:

Version 1.0.0.

The big 1.0.0 update came in May 4, 2022. This was the first full draft in the sense that it contained brms versions of all of Kruschke’s JAGS and Stan models, excluding examples not currently possible with the brms paradigm (e.g., ). Noteworthy changes included:

Version 1.1.0.

The 1.1.0 update came in January 25, 2023. Noteworthy changes included:

  • replacing my incorrect use of tidyr::expand() with a more appropriate tidyr::expand_grid() workflow, thanks to insights from Desislava Petkova;
  • adopting the new linewidth argument for several ggplot2 geoms (see here); and
  • minor prose and code edits throughout.

Version 1.2.0.

Welcome to version 1.2.0! Noteworthy changes include:

Some minor issues remain.

There are some minor improvements I’d like to add in future versions. Most importantly, a few simulations, figures, and models are beyond my current skill set. I’ve opened separate GitHub issues for the most important ones and they are as follows:

  • the effective-sample-size simulations in Section 7.5.2 and the corresponding plots in Figures 7.13 and 7.14 (issue #15),
  • several of the simulations in Sections 11.1.4, 11.3.1, and 11.3.2 and their corresponding figures (issues #16, #17, #18, and #19),
  • the stopping-rule simulations in Section 13.3.2 and their corresponding figures (issue #20).

If you know how to conquer any of these unresolved challenges, I’d love to hear all about it. In addition, please feel free to open a new GitHub issue if you find any flaws in the other sections of the ebook.

Thank-you’s are in order

Before we enter the primary text, I’d like to thank the following for their helpful contributions:

License and citation

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 please give me the appropriate credit the same way you would for any other scholarly resource. Here’s the citation information:

@book{kurzDoingBayesianDataAnalysis2025,
  title = {Doing {{Bayesian}} data analysis in brms and the tidyverse},
  author = {Kurz, A. Solomon},
  year = {2025},
  month = {4},
  edition = {Version 1.2.0},
  url = {https://bookdown.org/content/3686/}
}