This book presents code examples from the Causal Inference Book by Hernán and Robins, which is available in draft form from the following webpage.

The R code is based on the code by Joy Shi and Sean McGrath given here.

Packages to install

To install the R packages required for this book please copy/fork the repository and run:

# install.packages('devtools') # uncomment if devtools not
# installed

Downloading the datasets

We assume that you have downloaded the data from the Causal Inference Book website and saved it to a data subdirectory. You can do this manually or with the following code (nb. we use the here package to reference the data subdirectory).

dataurls <- list()
dataurls[[1]] <- ""
dataurls[[2]] <- ""
dataurls[[3]] <- ""
dataurls[[4]] <- ""

temp <- tempfile()
for (i in 1:3) {
    download.file(dataurls[[i]], temp)
    unzip(temp, exdir = "data")

download.file(dataurls[[4]], here("data", "nhefs.csv"))