-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.Rmd
91 lines (70 loc) · 3.59 KB
/
index.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
title: "Causal Inference: What If. R and Stata code for Exercises"
author:
- Book by M. A. Hernán and J. M. Robins
- R code by Joy Shi and Sean McGrath
- Stata code by Eleanor Murray and Roger Logan
- R Markdown code by Tom Palmer
date: "`r format(Sys.Date(), '%d %B %Y')`"
site: bookdown::bookdown_site
documentclass: book
#biblio-style: apalike
link-citations: yes
description: "Code examples from Causal Inference: What If by M. A. Hernán and J. M. Robins https://www.hsph.harvard.edu/miguel-hernan/causal-inference-book/"
geometry: margin=1in
fontsize: 10pt
linestretch: 1.1
bibliography: bibliography.bib
papersize: a4
urlcolor: blue
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = '#>')
```
# Preface{-}
This book presents code examples from @ci-book, which is available in draft form from the following webpage.
https://www.hsph.harvard.edu/miguel-hernan/causal-inference-book/
The R code is based on the code by Joy Shi and Sean McGrath given [here](https://cdn1.sph.harvard.edu/wp-content/uploads/sites/1268/1268/20/Rcode_CIpart2.zip).
The Stata code is based on the code by Eleanor Murray and Roger Logan given [here](https://cdn1.sph.harvard.edu/wp-content/uploads/sites/1268/2019/11/stata_part2.zip).
This repo is rendered at <https://remlapmot.github.io/cibookex-r/>. Click the download button above for the pdf and eBook versions.
```{r, echo=FALSE, out.width="65%", fig.align="center"}
knitr::include_graphics("figs/download.png")
```
## Downloading the code
The repo is available on GitHub [here](https://github.com/remlapmot/cibookex-r). There are a number of ways to download the code.
Either,
* click the green *Clone or download* button then choose to *Open in Desktop* or *Download ZIP*.
```{r, echo=FALSE, out.width="65%", fig.align="center"}
knitr::include_graphics("figs/clone-or-download.png")
```
The *Desktop* option means open in the [GitHub Desktop](https://desktop.github.com/) app (if you have that installed on your machine). The *ZIP* option will give you a zip archive of the repo, which you then unzip.
* or fork the repo into your own GitHub account and then clone or download your forked repo to your machine.
```{r, echo=FALSE, out.width="65%", fig.align="center"}
knitr::include_graphics("figs/fork.png")
```
## Installing dependency packages
It is easiest to open the repo in RStudio, as an RStudio project, by doubling click the `.Rproj` file. This makes sure that R's working directory is at the top level of the repo. If you don't want to open the repo as a project set the working directory to the top level of the repo directories using `setwd()`. Then run:
```{r eval=FALSE}
# install.packages("devtools") # uncomment if devtools not installed
devtools::install_dev_deps()
```
## 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`](https://here.r-lib.org/) package to reference the data subdirectory).
```{r, results='hide', message=FALSE, warning=FALSE}
library(here)
```
```{r}
dataurls <- list()
stub <- "https://cdn1.sph.harvard.edu/wp-content/uploads/sites/1268/"
dataurls[[1]] <- paste0(stub, "2012/10/nhefs_sas.zip")
dataurls[[2]] <- paste0(stub, "2012/10/nhefs_stata.zip")
dataurls[[3]] <- paste0(stub, "2017/01/nhefs_excel.zip")
dataurls[[4]] <- paste0(stub, "1268/20/nhefs.csv")
temp <- tempfile()
for (i in 1:3) {
download.file(dataurls[[i]], temp)
unzip(temp, exdir = "data")
}
download.file(dataurls[[4]], here("data", "nhefs.csv"))
```
\mainmatter