2 + 2[1] 4
version 1.4.0
Andrew Hayes’s (2018) text, Introduction to mediation, moderation, and conditional process analysis: A regression-based approach, has become a staple in social science graduate education. Hayes’s work has been from a frequentist OLS perspective. This book is an effort to connect his work with the Bayesian paradigm. Herein I refit his models with my favorite R (R Core Team, 2022) package for Bayesian regression, Paul Bürkner’s brms (Bürkner, 2017, 2018, 2022). I also prefer data wrangling and plotting using the tidyverse (Wickham et al., 2019; Wickham, 2022). But to be clear, this ebook is not meant to stand alone. It’s a supplement to the second edition of the original textbook.
The source code of the ebook is available here. This ebook is powered by Quarto.
I’m not a statistician and I have no formal background in computer science. But I met a great statistics mentor in grad school who was enthusiastic, knowledgeable, and very generous with his time. In one of his stats electives, we used Hayes’s first edition text and I learned a lot in that semester.
Yet a large portion of my training has been out of the classroom, working with messy real-world data, and looking online for help. One of the great resources I happened on was idre, the UCLA Institute for Digital Education. They perform a variety of services, but I benefited the most from was their portfolio of richly annotated textbook examples. Their online tutorials are among the earliest inspirations for this ebook. More so than my old statistics seminar lecture notes, high-quality and freely-available resources like this are where most of my day-to-day data analysis skills come from. We need more resources like this.
Hayes’s work has become influential in many corners of the academy, including my own–psychology. His PROCESS macro (2018) has been aimed at SPSS (IBM Corporation, 2020) and SAS (SAS/IML Software, 2020) users, which is understandable given their popularity in the social sciences. Over the past few years, I’ve moved away from proprietary programs like SPSS to R. R free, open source, and I find it a more flexible and useful tool for data analysis. In fairness, Hayes expanded his second edition to include R code, which is a great move forward. But his work is done from a frequentist OLS perspective and there have been a lot of exciting developments in the world of applied Bayesian statistics. If you’re an R user and want to learn about Bayesian data analysis, I think Bürkner’s brms is the best package around. It’s flexible, uses reasonably-approachable syntax, has sensible defaults, and offers a wide array of post-processing convenience functions. In addition, the R code in Hayes’s second edition does not leverage the power of the tidyverse. The purpose of this ebook is to connect Hayes’s insights into regression with the Bayesian paradigm. We’ll do so within the free and open-source R ecosystem, highlighting the Bayesian brms package, and using functions from the tidyverse and friends (e.g., tidybayes, Kay, 2022) to streamline our code.
If you’re looking at this ebook, I’m guessing you’re either a graduate student or a post-graduate academic or researcher of some sort, which comes with the presumption you have at least a 101-level foundation in statistics. If you’re rusty, check out Roback and Legler’s free (2021) text, Beyond multiple linear regression: Applied generalized linear models and multilevel models in R or Navarro’s free (2019) text, Learning statistics with R. I’m also presuming you are at least vaguely familiar with Bayesian statistics. For those totally new to Bayesian regression and HMC estimation, look at Bürkner’s (2017) paper, watch a few of these engaging lectures, or even start with either of my (2026b, 2026a) books based on the two (2015, 2020) editions of McElreath’s Statistical rethinking. I’m also presuming a basic working fluency in R and a vague idea about what the tidyverse is. If you’re totally new to R, consider starting with Peng’s (2022) R programming for data science. And the best introduction to the tidyvese-style of data analysis I’ve found is Grolemund and Wickham’s (2017) R for data science.
That said, you do not need to be totally fluent in statistics or R. Otherwise why would you need this ebook, anyway? IMO, the most important things are curiosity, a willingness to try, and persistent tinkering. I love this stuff. Hopefully you will, too.
This ebook is not meant to stand alone. It’s a supplement to the second edition of Hayes’s text. I follow the structure of his text, chapter by chapter, translating his analyses into brms and tidyverse code. However, many 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 ebook will be sparse or blank. I imagine students might reference this ebook as they progress through Hayes’s text. I also imagine working data analysts might use this ebook in conjunction with Hayes’s 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. I also like to highlight some of the unique strengths Bayesian analyses brings to the table, such as the ease with which you can depict uncertainty with density plots, interval ribbons around regression lines, and spaghetti plots. For more on some of these topics, chapters 3, 7, and 28 in R4DS, Healy’s (2018) Data visualization: A practical introduction, or Wickham’s (2016) ggplot2: Elegant graphics for data analysis.
In this ebook, I use a handful of formatting conventions gleaned from R4DS and R markdown: The definitive guide.
2 + 2[1] 4
brm()).:: between the package name and the function (e.g., brms::fitted()).d or size = 2).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("tidyverse", "rstan", "brms", "bayesplot", "broom", "cowplot", "directlabels", "GGally", "ggdist", "ggthemes", "HDInterval", "MASS", "palettetown", "psych")
install.packages(packages, dependencies = T)A couple 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.
devtools::install_github("EdwinTh/dutchmasters")
devtools::install_github("ropenscilabs/ochRe")It’s possible you’ll have problems installing some of these packages. Here are the likely suspects and where you can find help:
For a brief rundown of the version history, we have:
I released the 1.0.0 version of this book in July of 2018. It was a complete first draft.
In December 21, 2019, came the 1.1.0 update. It included a lot of changes, the most notable of which were that,
mvbind() and mvbf() functions,.rda files in the fits folder on GitHub,In August 8, 2021, came the 1.2.0 update, with the following noteworthy changes:
In January 24, 2023, came the 1.3.0 update, which included the following noteworthy changes:
posterior_samples() function with the new posterior::as_draws_df()-based workflow;tidyr::expand() with a more appropriate tidyr::expand_grid() workflow, thanks to insights from Desislava Petkova;linewidth argument for several ggplot2 geoms (see here);Welcome to version 1.4.0! Noteworthy changes include:
It’s unclear what future versions of this book may hold. Candidates include
We’ll see. In the meantime, feel free to make suggestions or report typos and coding goofs at https://github.com/ASKurz/recoding-Hayes-2018/issues.
Before we move on, I’d like to thank the following for their helpful contributions:
For information on how you might help improve this ebook, go to https://github.com/ASKurz/recoding-Hayes-2018/blob/master/CONTRIBUTING.md.
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 make sure you give me the appropriate credit the same way you would for any other scholarly resource. Here’s the citation information:
@book{kurzRecodingIntroduction2026,
title = {Recoding {{Introduction to Mediation, Moderation, and Conditional Process Analysis}}},
author = {Kurz, A. Solomon},
year = {2026},
month = {1},
edition = {version 1.4.0},
url = {https://solomon.quarto.pub/immcpa2/}
}
Comments