—title: “Regression Discontinuity Example” author: “Sida Peng” date: “May 24, 2018” output: html_document — # Setup

knitr::opts_chunk$set(echo = TRUE)


#INSTALLATION
#install.packages("testthat", "randomForest", "devtools", "chron")

##Windows: Install Rtools 3.4 from https://cran.r-project.org/bin/windows/Rtools/
##Mac: Will ask if you want to install developer tools (do, and re-install)
#devtools::install_github("susanathey/causalTree")

##Mac/Linux: 
#devtools::install_github("swager/gradient-forest", subdir = "r-package/gradient.forest")

##Windows:
##Download gradient.forest_0.0.0.1.tar.gz from Jake's website
#install.packages("C:/path/to/gradient.forest_0.0.0.1.tar.gz", repos = NULL, type="source")

setwd("C:/Users/jlariv/OneDrive/Econ 404/Solutions")

dat <- read.table('lee_2008_data.txt', header = TRUE)
x <- dat$difdemshare
y <- dat$demsharenext
plot(x, y)

idx = which(x<0)
dt0 = data.frame(x = x[idx], y = y[idx])
dt1 = data.frame(x = x[-idx], y = y[-idx])

fit0 = lm(y~x, data = dt0)
summary(fit0)
## 
## Call:
## lm(formula = y ~ x, data = dt0)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.42701 -0.04733  0.00672  0.06322  0.86396 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.432948   0.004123  105.02   <2e-16 ***
## x           0.296906   0.011132   26.67   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1334 on 2738 degrees of freedom
## Multiple R-squared:  0.2062, Adjusted R-squared:  0.2059 
## F-statistic: 711.3 on 1 and 2738 DF,  p-value: < 2.2e-16
fit1 = lm(y~x, data = dt1)
summary(fit1)
## 
## Call:
## lm(formula = y ~ x, data = dt1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.89406 -0.07029 -0.00344  0.08076  0.44707 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.551179   0.003832   143.8   <2e-16 ***
## x           0.342884   0.007172    47.8   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1418 on 3816 degrees of freedom
## Multiple R-squared:  0.3746, Adjusted R-squared:  0.3744 
## F-statistic:  2285 on 1 and 3816 DF,  p-value: < 2.2e-16
RDeffect = coef(fit1)[1]-coef(fit0)[1]
std = sqrt(coef(summary(fit0))[1, "Std. Error"]^2 + coef(summary(fit1))[1, "Std. Error"]^2)
RDeffect
## (Intercept) 
##   0.1182314
std
## [1] 0.005628466