---
title: "Ch_11_Lab_Alcohol_and_Crime_KEY"
output: word_document
date: ""
---

## Preparation

```{r setup, include=TRUE, message = FALSE, warning = FALSE}
require(knitr)
require(haven)
require(dplyr)

opts_chunk$set(echo = TRUE)
options(digits = 3)
opts_chunk$set(root.dir = "C:/Users/baileyma/Documents/Textbook/OUP/Supplementals_Jan2019")
  ## Set path  in "setup" chunk to appropriate folder.
  ## For example: opts_knit$set(root.dir ="C:/Data")  
```

```{r message = FALSE, include = FALSE}
rm(list = ls(all = TRUE))   # Remove objects from the previous session

# Load data: data saved as object named "dta"
load(file = "C:/Users/baileyma/Documents/Textbook/OUP/Supplementals_Jan2019/Data_sets/Ch11_Lab_AlcoholCrime.RData")
```

Carpenter and  Dobkin (2015) analyzes the relationship between alcohol and crime using a regression discontinuity design.

In this lab we will replicate the results and then explore different specifications.  As you will see, some reasonable alternative specifications yield quite different results.  The goal here will be to see if we can make any progress in explaining why the results vary as we vary the specification.

From the codebook: "The individual level arrest records are collapsed into arrest counts by age in days for each crime type  in the SAS program "P01 Estimate Age Profile of Crime Rates.sas". This program uses populations estimated from the census in "P00 Estimate Population Denominators.sas" to compute arrest rates per 10,000."

The main variables are 

- *all_r*: number of arrrests across all categories.  Our main dependent variable. Arrest data is also broken down for *violent_r*, *property_r*, *ill_drugs_r* and *alcohol_r*
- *post* a dumm variable indicating the individual is over 21 years of age
- *linear*: the number of years from being age 21 (that is, *linear* = 1.0 indicates 22 year olds.  Negative values means the individual is under 21 and positive values indicate the individual is over than 21.
- *square*: the squared value of linear
- *linear_post*: interaction of linear and post
- *square_post*: interaction of square and post
- birthday dummies: e.g., birthday_19 is a dummy for a person's 19th birthday and birthday_19_1 is a dummy for the day after a person's 19th birthday. Use this code in your formula: + birthday_19  + birthday_19_1 + birthday_20  + birthday_20_1 + birthday_21  + birthday_21_1 + birthday_22 + birthday_22_1 + birthday_23 + birthday_23_1

See Carpenter, Christopher and Carlos Dobkin. 2015. The Minimum Legal Drinking Age and Crime. *The Review of Economics and Statistics.* 97:2, 521-524.

#### (a) Replicate the main result using the specification for Table 1. In this model, the RD allows for differential quadratic models above and below the threshold and also has dummy variables for birthdays.  The data is limited to observations within 2 years of 21.

```{r message = FALSE}

```



#### (b)	Estimate a series of models with slightly different specifications. First, try the above specification with only linear age variable, first fixing slope to be the same above and below the threshhold and then allowing slope to vary above and below the threshhold.

```{r}

```

#### (c)	Now see what happens when you use a different window sizes.  Feel free to experiment (but only need to report one specification.)
```{r}
```



#### (d)	To figure out what is going on, let's create some figures. First, let's re-create Figure 1 in the paper. I provide the code to create a binned graph for the alcohol arrest rate.  Your task is to add another arrest category to the plot.

```{r figure, echo=FALSE, fig.cap="Figure 1 in Carpenter and Dobkin (2015)", out.width = "20%", fig.align="center", out.extra="style='transform:rotate(180deg)'"}
knitr::include_graphics("C:/Users/baileyma/Documents/Textbook/OUP/Supplementals_Jan2019/Labs_R/Ch11_Lab_Alcohol and Crime_Figure.jpg")
```



```{r}
# The original paper used 2 week intervals for bins, using the following to 
# generate a number indicating age in years grouped into 2 week intervals
dta$age_fortnight = 21 + (14*floor(dta$days_to_21/14))/365

# Create a variable placing each observation in a "bin"
dta$bin  = rep(1:length(unique(dta$age_fortnight)), table(dta$age_fortnight))

# Use "window" to set how wide the window is above and below 21; e.g. window = 2
# limits years 19 to 23
window = 2

# Create a dataframe that contains average crime rates grouped by bin
# Use the dplyr package to simplify data organization
dta_bin <-  dta %>% 
  select(days_to_21, all_r, property_r, age_fortnight, alcohol_r, bin) %>%
  group_by(bin) %>%
  summarise(property_r_bin = mean(property_r), 
            all_r_bin = mean(all_r), 
            alcohol_r_bin = mean(alcohol_r), 
            age_fortnight_bin = mean(age_fortnight)) %>%
  filter(age_fortnight_bin >= 21- window & age_fortnight_bin <= 21 + window)


```

#### (d)	Now create a figure based on a linear model.  You can try different window sizes but need only report one. Do this for alcohol (and feel free to add other crimes, but not required.)
```{r}

# Create a dataframe that contains average crime rates grouped by bin, limited to the 
# window set by the window parameter
window = 4
library(dplyr)
dta_bin <-  dta %>% 
  select(days_to_21, all_r, property_r, age_fortnight, alcohol_r, bin) %>%
  group_by(bin) %>%
  summarise(property_r_bin = mean(property_r), 
            all_r_bin = mean(all_r), 
            alcohol_r_bin = mean(alcohol_r), 
            age_fortnight_bin = mean(age_fortnight)) %>%
  filter(age_fortnight_bin >= 21- window & age_fortnight_bin <= 21 + window)


```

#### (e)	Discuss if and how one could test for covariate balance for this particular data set.

