Doing Bayesian Data Analysis in brms and the tidyverse
version 1.1.0
2023-01-25
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 (Bürkner, 2017, 2018, 2022g), which makes it easier 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.
This ebook is not meant to stand alone. It’s a supplement to the second edition of Kruschke’s (2015) 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.
<- 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")
packages
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.
::install_github("clauswilke/colorblindr")
remotes::install_github("dill/beyonce")
devtools::install_github("ropenscilabs/ochRe") devtools
It’s possible you’ll have problems installing some of these packages. Here are some likely suspects and where you can find help:
- for difficulties installing brms, go to https://github.com/paul-buerkner/brms#how-do-i-install-brms or search around in the brms section of the Stan forums; and
- for difficulties installing rstan, go to https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started.
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:
- reproducing the simulation necessary for Figure 7.3 (see GitHub issue #14) with help from Cardy Moten III (@cmoten);
- with guidance from Bjørn Peare Bartholdy (@bbartholdy), Mladen Jovanović (@mladenjovanovic), Cory Whitney (@CWWhitney), and Brenton M. Wiernik (@bwiernik), we improved in-text citations and reference sections using BibTex (BibTeX, 2020), Better BibTeX (Heyns, 2020), and zotero (Roy Rosenzweig Center for History and New Media, 2020);
- the plot resolution increased with
fig.retina = 2.5
; and - small code, hyperlink, and typo corrections.
Version 0.3.0.
The 0.3.0 update came in September 22, 2020. Noteworthy changes included:
- adding the Kruschke-style model diagrams throughout the text (e.g., Figure 8.5);
- adding chapter-specific plotting schemes with help from the cowplot package (Wilke, 2020b), Wilke’s (2019) Fundamentals of data visualization, and many other great color-scheme packages;
- an overhaul to the plotting workflow in Section 6.4.1; and
- updating all model fits with brms version 2.13.5.
Version 0.4.0.
The 0.4.0 update came in May 6, 2021. Noteworthy changes included:
- using the Metropolis algorithm to fit the bivariate Bernoulli model for Figure 7.6 (Section 7.4.3), thanks to help from Omid Ghasemi;
- corrections to mistakes around the
lag()
andlead()
functions in Section 7.5.2; - an added bonus section clarifying the pooled standard deviation for standardized mean differences (Section 16.3.0.1);
- refining the custom
stat_wilke()
plotting function in Chapter 18; - an overhaul of the bonus section covering effect sizes (Section 19.6);
- refining/correcting the threshold workflow for univariable logistic regression models (Chapter 21);
- fixing the divergent transitions issue for the robust logistic regression model by adding boundaries on the prior (Section 21.3);
- corrections to a few incorrectly computed effect sizes in Chapter 23;
- the addition of a new bonus section (Section 22.3.3.1.1) highlighting the benefits of the intercepts-only softmax model;
- expansions to the material on censored data (Section 25.4) and the addition of a brief introduction to truncated data (Section 25.4.4); and
- updating all HMC fits to the current version of brms (2.15.0).
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., Section 10.3.2). Noteworthy changes included:
- two new solutions for the conditional logistic models of Chapter 22 thanks to the generous efforts by Henrik Singmann and Mattan Ben-Shachar;
- replacing the depreciated
posterior_samples()
function with the newposterior::as_draws_df()
-based workflow; - adding a new solution for the multivariate Bernoulli model with different trial numbers via the
resp_subset()
function in Section 7.4.4.1; - improving the efficiency of the intercept-only Bernoulli models with the new
lb
andub
arguments for priors ofclass = Intercept
(e.g.,fit7.1a
,fit7.1b
, andfit7.2
in Chapter 7); - updating all model fits with brms version 2.17.0; and
- various minor code, hyperlink, and typo corrections.
Version 1.1.0.
Welcome to version 1.1.0! Noteworthy changes include:
- replacing my incorrect use of
tidyr::expand()
with a more appropriatetidyr::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.
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),
- the data necessary to properly reproduce the HMC proposal schematic presented in Section 14.1 and Figures 14.1 through 14.3 (issue #21), and
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:
- Bjørn Peare Bartholdy (@bbartholdy),
- David Baumeister (@xdavebx),
- Mattan Ben-Shachar (@mattansb),
- Paul-Christian Bürkner (@paul-buerkner),
- Christopher Flach (@flachboard),
- Andrew Gelman (@andrewgelman),
- Omid Ghasemi (@OmidGhasemi21),
- Mladen Jovanović (@mladenjovanovic),
- Matthew Kay (@mjskay),
- TJ Mahr (@tjmahr),
- Cardy Moten III (@cmoten),
- Lukas Neugebauer (@LukasNeugebauer),
- Demetri Pananos (@Dpananos),
- Desislava Petkova (@dipetkov),
- Peter Ralph (@petrelharp),
- Henrik Singmann (@singmann),
- Aki Vehtari (@avehtari),
- Matti Vuorre (@mvuorre),
- Cory Whitney (@CWWhitney), and
- Brenton M. Wiernik (@bwiernik).
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{kurzDoingBayesianDataAnalysis2023,
= {Doing {{Bayesian}} data analysis in brms and the tidyverse},
title = {Kurz, A. Solomon},
author = {2023},
year = {1},
month = {Version 1.1.0},
edition = {https://bookdown.org/content/3686/}
url }