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.

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

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:

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

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

Version 1.1.0.

Welcome to version 1.1.0! Noteworthy changes include:

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

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:

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,
  title = {Doing {{Bayesian}} data analysis in brms and the tidyverse},
  author = {Kurz, A. Solomon},
  year = {2023},
  month = {1},
  edition = {Version 1.1.0},
  url = {https://bookdown.org/content/3686/}
}

References

BibTeX. (2020). http://www.bibtex.org/
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. (2022g). brms: Bayesian regression models using ’Stan. https://CRAN.R-project.org/package=brms
Heyns, E. (2020). Better BibTeX for zotero. https://retorque.re/zotero-better-bibtex/
Kruschke, J. K. (2015). Doing Bayesian data analysis: A tutorial with R, JAGS, and Stan. Academic Press. https://sites.google.com/site/doingbayesiandataanalysis/
R Core Team. (2022). R: A language and environment for statistical computing. R Foundation for Statistical Computing. https://www.R-project.org/
Roy Rosenzweig Center for History and New Media. (2020). Zotero. https://www.zotero.org/
Wickham, H. (2022). tidyverse: Easily install and load the ’tidyverse’. https://CRAN.R-project.org/package=tidyverse
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
Wilke, C. O. (2019). Fundamentals of data visualization. https://clauswilke.com/dataviz/
Wilke, C. O. (2020b). cowplot: Streamlined plot theme and plot annotations for ggplot2 [Manual]. https://wilkelab.org/cowplot/