*Doing Bayesian Data Analysis* in brms and the tidyverse

*version 1.0.0*

*2022-05-04*

# 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, 2021) using Hamiltonian Monte Carlo. I also prefer plotting and data wrangling with the packages from the **tidyverse** (Wickham et al., 2019; Wickham, 2021). 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()`

and`lead()`

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.

Welcome to version 1.0.0! This is the first full draft in the sense that it contains **brms** versions of all of Kruschke’s JAGS and Stan models, excluding examples that are not possible with the **brms** paradigm (e.g., Section 10.3.2). Noteworthy changes include:

- 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 new`posterior::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`

and`ub`

arguments for priors of`class = Intercept`

(e.g.,`fit7.1a`

,`fit7.1b`

, and`fit7.2`

in Chapter 7); - updating all model fits with
**brms**version 2.17.0; and - various minor code, hyperlink, and typo corrections.

### 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),
- 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),
- 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{kurzDoingBayesianDataAnalysis2022,
= {Doing {{Bayesian}} data analysis in brms and the tidyverse},
title = {Kurz, A. Solomon},
author = {2022},
year = {5},
month = {Version 1.0.0},
edition = {https://bookdown.org/content/3686/}
url }
```

### References

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

*Journal of Statistical Software*,

*80*(1), 1–28. https://doi.org/10.18637/jss.v080.i01

*The R Journal*,

*10*(1), 395–411. https://doi.org/10.32614/RJ-2018-017

*brms: Bayesian regression models using ’Stan’*. https://CRAN.R-project.org/package=brms

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

*Doing Bayesian data analysis: A tutorial with R, JAGS, and Stan*. Academic Press. https://sites.google.com/site/doingbayesiandataanalysis/

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

*Zotero*. https://www.zotero.org/

*tidyverse: Easily install and load the ’tidyverse’*. https://CRAN.R-project.org/package=tidyverse

*Journal of Open Source Software*,

*4*(43), 1686. https://doi.org/10.21105/joss.01686

*Fundamentals of data visualization*. https://clauswilke.com/dataviz/

*cowplot: Streamlined plot theme and plot annotations for ggplot2*[Manual]. https://wilkelab.org/cowplot/