-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy path.Rhistory
More file actions
116 lines (116 loc) · 4.01 KB
/
.Rhistory
File metadata and controls
116 lines (116 loc) · 4.01 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# this chunk contains code that sets global options for the entire .Rmd.
# we use include=FALSE to suppress it from the top of the document, but it will still appear in the appendix.
knitr::opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE, linewidth=60)
# you can include your libraries here:
library(tidyverse)
# and any other options in R:
options(scipen=999)
# nothing in this chunk will be printed in the main document
# except the output of the code.
print("hello world")
# everything in this chunk will be printed in the main document,
# plus the output of the code.
print("hello world")
# everything in this chunk will be printed in the main document,
# but the code won't run and so the output won't be included.
print("hello world")
# here we define alpha, so that we have an object to reference in-line
alpha <- 12345
# making a simple scatterplot using the cars data
plot(cars)
# making a simple table of the mtcars data
knitr::kable(mtcars[1:5,], caption = "The first five rows of mtcars")
# this chunk generates the complete code appendix.
# eval=FALSE tells R not to re-run (``evaluate'') the code here.
unlink("OneDrive - London School of Economics/PhD/Classes/MY457/pset_template_cache", recursive = TRUE)
install.packages("tinytex")
tinytex::install_tinytex()
tinytex:::install_prebuilt()
knitr::opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE, linewidth=60)
# you can include your libraries here:
library(tidyverse)
# and any other options in R:
options(scipen=999)
library(readr)
cses5 <- read_csv("OneDrive - London School of Economics/PhD/Papers/Voting Paper/Process/cses5.csv")
View(cses5)
head(cses5)
summary(cses5)
install.packages("dplyr")
library(dplyr)
filtered_data <- cses_data %>%
filter(E1006_UNALPHA3 == "ITA")
cses_data <- read.csv("cses5.csv")
setwd("/Users/elenapro/Library/CloudStorage/OneDrive-LondonSchoolofEconomics/PhD/Classes/MY457/lse-my457.github.io")
setwd("/Users/elenapro/Library/CloudStorage/OneDrive-LondonSchoolofEconomics/PhD/Classes/MY457/lse-my457.github.io/seminars/seminar4/coding")
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(ggplot2)
library(AER)
# SIMULATE DATA
# PARAMETERS
N <- 10000
U <- rnorm(N, mean = 5, sd = 3)
b0 <- 2
b1 <- 1.5
# POTENTIAL OUTCOMES
y0 <- b0 + b1 * U + rnorm(N)
y1 <- y0 + mean(y0) + rnorm(N)
y1[which(y0 < median(y0))] <- y1[which(y0 < median(y0))] / 2
# CREATE DATAFRAME
df <- cbind(y0, y1, U) %>% as_tibble()
# GENERATE TYPES OF COMPLIANCE
type <- rep(NA, 10000)
type[which(y1 > median(y1))] <-
sample(c(rep('Complier', 3500), rep('Always Taker', 1000), rep('Never Taker', 500)))
type[which(y1 < median(y1))] <-
sample(c(rep('Complier', 1500), rep('Always Taker', 1500), rep('Never Taker', 2000)))
df$type <- type
# CREATE INSTRUMENT
df$z <- sample(c(rep(0, 5000), rep(1, 5000)))
# CREATE TREATMENT ASSIGNMENT
df <- df %>%
mutate(d = case_when(type == 'Always Taker' ~ 1,
type == 'Never Taker' ~ 0,
(type == 'Complier' & z == 0) ~ 0,
(type == 'Complier' & z == 1) ~ 1))
# REAL OUTCOMES
df <- df %>% mutate(y = case_when(d == 0 ~ y0, d == 1 ~ y1))
# TRUE ATE
true_ate <- t.test(df$y1, df$y0, paired = TRUE)
true_ate
# NAIVE "ATE"
naive_ate <- lm(y ~ d, data = df)
true_ate
summary(naive_ate)
# 1. Effect of Z on Y
mean(df$y[df$z==1])-mean(df$y[df$z==0])
y.on.z <- lm(y ~ z, data = df)
summary(y.on.z)
itt_est <- coef(y.on.z)[2]
###
# 2. Effect of Z on D
mean(df$d[df$z==1])-mean(df$d[df$z==0])
d.on.z <- lm(d ~ z, data = df)
summary(d.on.z)
prop_compliers <- coef(d.on.z)[2]
###
# 3. WALD ESTIMATE
itt_est/prop_compliers
# 2SLS using lm
df$d_hat <- predict(d.on.z)
iv_2sls_2 <- lm(y ~ d_hat, data = df)
summary(iv_2sls_2)
# 2SLS using ivreg
iv_2sls <- ivreg(y ~ d | z, data = df)
summary(iv_2sls)
df <- df %>% mutate(y_diff = y1 - y0)
p1 <- ggplot(data = df, aes(x = y_diff, fill = type)) +
geom_density(alpha = 0.2) + ggtitle("All Units")
p1
p2 <- ggplot(data = df[df$d == 1, ], aes(x = y_diff, fill = type)) +
geom_density(alpha = 0.2) + ggtitle("Treated Units")
p2
p3 <- ggplot(data = df[df$d == 0, ], aes(x = y_diff, fill = type)) +
geom_density(alpha = 0.2) + ggtitle("Control Units")
p3