Regression and Other Stories - Examples

Introduction

  • 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 + codebooks) 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). Most data folders hve also some codebook explaining the column names.

  • 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

library("rprojroot")
root<-has_file(".ROS-Examples-root")$make_fix_file()

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

Download code and data

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.

Source: examples.Rmd.

The Python version keeps data handling explicit and uses lapylace for Stan-backed generalized linear models, so the statistical model can be read from a formula rather than from handwritten Stan.

from pathlib import Path
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import lapylace as lp

sys.path.append(str(Path.cwd().parent / 'python'))
from data import ros_path

Data

wells = pd.read_csv(ros_path('Arsenic/data', 'wells.csv'))
wells.head()
switch arsenic dist dist100 assoc educ educ4
0 1 2.36 16.826000 0.16826 0 0 0.0
1 1 0.71 47.321999 0.47322 0 0 0.0
2 0 2.07 20.966999 0.20967 0 10 2.5
3 1 1.15 21.486000 0.21486 0 12 3.0
4 1 1.10 40.874001 0.40874 1 14 3.5

Notes

  • Source computation blocks represented: 0.
  • Data paths are expressed through the shared ros_path() helper.
  • Formula-based Bayesian regressions are routed through lapylace.stan_glm().
  • Plotting and simulation work uses NumPy, pandas, matplotlib idioms.