• The code and data are provided to fully reproduce the examples and figures in the book. They can be a good way to see what the code does. Different people have different styles of code. The code here is not supposed to be a model. The statistical analyses and graphs in the book are intended to be models for good practice, but the code here is meant to be simple with minimal dependencies.
  • For R programming basics see Appendix A of Regression and Other Stories. If you want to learn more, see our recommendations for R programming and visualization with R.

  • The folders below (ending /) point to the code (.R and .Rmd) and data folders (.csv or .txt) in github, and .html -files point to knitted notebooks.
  • Most examples have cleaned data in .csv file in data subfolder for easy experimenting. For completeness and reproducibility, the data subfolders have also the raw data and *_setup.R file showing how the data pre-processing has been done (to do the exercises and follow along with the examples, you don’t need to worry about the setup code).
  • For easy access to data sets, there is an R package rosdata. You can install it with a command remotes::install_github("avehtari/ROS-Examples",subdir = "rpackage"). Then you can access data, for example, as library(rosdata), data(wells), head(wells). You can get the list of data sets with ?rosdata.

  • When running the notebooks, to avoid need to switch the working directory, rprojroot package is used to set the project root directory. The downloaded git repository can be placed anywhere you like and you can rename the ROS-Examples directory if you wish. When running the code, it is sufficient that the working directory is any directory in the ROS-Examples (or renamed). Running

will find the file .ROS-Examples-root which is in the ROS-Examples directory, and will set the full path according to that. Then, for example,

wells <- read.csv(root("Arsenic/data","wells.csv"))

finds the wells.csv file, no matter where you have placed or renamed the ROS-Examples directory. When you switch to another example, there is no need to switch the working directory.

Examples by chapters

1 Introduction

2 Data and measurement

3 Some basic methods in mathematics and probability

4 Generative models and statistical inference

5 Simulation

6 Background on regression modeling

7 Linear regression with a single predictor

8 Fitting regression models

9 Prediction and Bayesian inference

10 Linear regression with multiple predictors

11 Assumptions, diagnostics, and model evaluation

12 Transformations

13 Logistic regression

14 Working with logistic regression

15 Other generalized linear models

16 Design and sample size decisions

17 Poststratification and missing-data imputation

18 Causal inference basics and randomized experiments

19 Causal inference using regression on the treatment variable

20 Observational studies with all confounders assumed to be measured

21 More advanced topics in causal inference

22 Advanced regression and multilevel models

Appendix A

Examples alphabetically

Tidyverse code

Bill Behrman has revised all the example code to use Tidyverse

brms + tidyverse code

Solomon A. Kurz is revising all the example code to use brms and tidyverse

Python code

Ravin Kumar, Tomás Capretto, and Osvaldo Martin are porting ROS examples to Python using bambi (BAyesian Model-Building Interface) which has similar formula syntax as rstanarm and brms.

Julia code

Rob J. Goedman is porting ROS examples to Julia.