-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ratings_Causality.R
451 lines (305 loc) · 18.8 KB
/
Ratings_Causality.R
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# Setting the Working Directory
setwd("~/UniLu/the")
takehome_ratings <- read.csv("takehome_ratings.csv", ";",header= T)
View(takehome_ratings)
dim(takehome_ratings)
str(takehome_ratings)
summary(takehome_ratings)
# 1) In my dataset (takehome_ratings.csv), I first calculate the individual causal effect (ICE)
# by subtracting the potential income under high ratings from the potential
# income under low ratings for each unit.
library(dplyr)
bla <- takehome_ratings
takehome <- takehome_ratings %>% mutate(bla$y1 - bla$y0) %>% rename( ice = "bla$y1 - bla$y0")
# 2) Before I calculate the causal effect of the individual groups, I first calculate the difference
# between the outcomes of the groups that received a treatment and the groups that did not receive a treatment.
ATE_wrong <- sum(takehome$ice) / nrow(takehome)
# 3) Now I calculate the average treatment effect for the
# Treatment Group and Control Group.
# Number in the treatment group
countTG <- nrow(filter(takehome, d == 1))
countTG
# Number in the control group
countCG <- nrow(filter(takehome, d == 0))
countCG
ATT_Filter <- filter(takehome, d == 1)
ATT_wrong <- sum(ATT_Filter$ice) / countTG
ATT_wrong
ATC_Filter <- filter(takehome, d == 0)
ATC_wrong <- sum(ATC_Filter$ice) / countCG
ATC_wrong
# 4) Check if there are any Selection Bias:
potential_outcome_under_control_1 <- sum(ATT_Filter$y0) / countTG
potential_outcome_under_control_1
potential_outcome_under_control_0 <- sum(ATC_Filter$y0) / countCG
potential_outcome_under_control_0
Selection_bias <- potential_outcome_under_control_1 - potential_outcome_under_control_0
Selection_bias
# 5) Additional: Calculate the Naive estimate:
naive_estimate <- ATT_wrong + Selection_bias
naive_estimate
# 6) Check if we forgot a variable
# Now lets heck whether the variable Quality was not considered (omitted variable test)
# Like exercise one. Both Means are different, so we can say that it is not colider, but cofounder!
mean(takehome$y[takehome$d==0])
mean(takehome$y[takehome$d==1])
# Because this is a coufounder, we have to take the onmitted variable into account as well.
# As we can see here, the coefficient d has slightly decreased. For this we have the variable Quality,
# which can also help explain this model now. As we can see here, the R-Squared has improved a bit.
# However, the F statistics have decreased a bit. The significance remains about the same for both.
(lm(y ~ d, takehome))
summary(lm(y ~ d + quality, takehome))
# 7) Use various randomization measures.
# Bernoulli trials:
# Completely randomized experiment:
# Completely randomized experiment within each block of the variable quality:
# Stratified randomized experiment:
takehome_randomized <- takehome
library(randomizr)
set.seed(12345)
takehome_randomized$bernoulli <- simple_ra(9934, prob=0.5)
takehome_randomized$complrandom <- complete_ra(N=9934, m=9934/2)
takehome_randomized$block <- block_ra(blocks = takehome_randomized$quality, prob = 0.5)
takehome_randomized$stratified <- strata_rs(strata = takehome_randomized$quality, prob = 0.5)
# 8) Execute steps 3 to 4 again for all radon nominations.
rm(takehome_randomized)
# Above I have created four columns, which performs the assignment using Radnomization. But I prefer it if I
# only have one column and overwrite it!
# For this reason I will now display both
takehome_randomized <- takehome
set.seed(12345)
takehome_randomized$assignment <-simple_ra(9934, prob=0.5)
# Now I calculate the average treatment effect for the
# Treatment Group and Control Group.
# Number in the treatment group
countTG <- nrow(filter(takehome_randomized, assignment == 1))
countTG
# Number in the control group
countCG <- nrow(filter(takehome_randomized, assignment == 0))
countCG
ATT_Filter <- filter(takehome_randomized, assignment == 1)
ATT_wrong <- sum(ATT_Filter$ice) / countTG
ATT_wrong
ATC_Filter <- filter(takehome_randomized, assignment == 0)
ATC_wrong <- sum(ATC_Filter$ice) / countCG
ATC_wrong
# ) Check if there are any Selection Bias:
potential_outcome_under_control_1 <- sum(ATT_Filter$y0) / countTG
potential_outcome_under_control_1
potential_outcome_under_control_0 <- sum(ATC_Filter$y0) / countCG
potential_outcome_under_control_0
Selection_bias <- potential_outcome_under_control_1 - potential_outcome_under_control_0
Selection_bias
set.seed(12345)
takehome_randomized$assignment <-complete_ra(N=9934, m=9934/2)
# Now I calculate the average treatment effect for the
# Treatment Group and Control Group.
# Number in the treatment group
countTG <- nrow(filter(takehome_randomized, assignment == 1))
countTG
# Number in the control group
countCG <- nrow(filter(takehome_randomized, assignment == 0))
countCG
ATT_Filter <- filter(takehome_randomized, assignment == 1)
ATT_wrong <- sum(ATT_Filter$ice) / countTG
ATT_wrong
ATC_Filter <- filter(takehome_randomized, assignment == 0)
ATC_wrong <- sum(ATC_Filter$ice) / countCG
ATC_wrong
# ) Check if there are any Selection Bias:
potential_outcome_under_control_1 <- sum(ATT_Filter$y0) / countTG
potential_outcome_under_control_1
potential_outcome_under_control_0 <- sum(ATC_Filter$y0) / countCG
potential_outcome_under_control_0
Selection_bias <- potential_outcome_under_control_1 - potential_outcome_under_control_0
Selection_bias
set.seed(12345)
takehome_randomized$assignment <-block_ra(blocks = takehome_randomized$quality, prob = 0.5)
# Now I calculate the average treatment effect for the
# Treatment Group and Control Group.
# Number in the treatment group
countTG <- nrow(filter(takehome_randomized, assignment == 1))
countTG
# Number in the control group
countCG <- nrow(filter(takehome_randomized, assignment == 0))
countCG
ATT_Filter <- filter(takehome_randomized, assignment == 1)
ATT_wrong <- sum(ATT_Filter$ice) / countTG
ATT_wrong
ATC_Filter <- filter(takehome_randomized, assignment == 0)
ATC_wrong <- sum(ATC_Filter$ice) / countCG
ATC_wrong
# 4) Check if there are any Selection Bias:
potential_outcome_under_control_1 <- sum(ATT_Filter$y0) / countTG
potential_outcome_under_control_1
potential_outcome_under_control_0 <- sum(ATC_Filter$y0) / countCG
potential_outcome_under_control_0
Selection_bias <- potential_outcome_under_control_1 - potential_outcome_under_control_0
Selection_bias
set.seed(12345)
takehome_randomized$assignment <-strata_rs(strata = takehome_randomized$quality, prob = 0.5)
# Now I calculate the average treatment effect for the
# Treatment Group and Control Group.
# Number in the treatment group
countTG <- nrow(filter(takehome_randomized, assignment == 1))
countTG
# Number in the control group
countCG <- nrow(filter(takehome_randomized, assignment == 0))
countCG
ATT_Filter <- filter(takehome_randomized, assignment == 1)
ATT_wrong <- sum(ATT_Filter$ice) / countTG
ATT_wrong
ATC_Filter <- filter(takehome_randomized, assignment == 0)
ATC_wrong <- sum(ATC_Filter$ice) / countCG
ATC_wrong
# 4) Check if there are any Selection Bias:
potential_outcome_under_control_1 <- sum(ATT_Filter$y0) / countTG
potential_outcome_under_control_1
potential_outcome_under_control_0 <- sum(ATC_Filter$y0) / countCG
potential_outcome_under_control_0
Selection_bias <- potential_outcome_under_control_1 - potential_outcome_under_control_0
Selection_bias
#########################################################################################################################################
# Exercise 2 - Matching (14 points) #
# #
# #
########################################################################################################################################
library(dplyr)
library(tidyverse)
library(MatchIt)
library("cobalt")
setwd("~/UniLu/the")
load("schools.RData")
schools
school <- schools
head(schools)
view(schools)
school <- school %>% mutate( if_else(homework > 3, 1, 0)) %>% rename( treat = "if_else(homework > 3, 1, 0)")
# IDs interessieren mich nicht!
school[1:2] <- list(NULL)
school %>% group_by(treat) %>% summarise_all(funs(mean))
# 2) I start with the general comparison by comparing all columns by grouping by treatment.
sex <- school %>% group_by(treat, sex) %>% summarise_all(funs(mean))
parented <- school %>% group_by(treat, parented) %>% summarise_all(funs(mean))
sex <- school %>% group_by(treat, sex) %>% summarise_all(funs(mean))
race <- school %>% group_by(treat, race) %>% summarise_all(funs(mean))
sctype <- school %>% group_by(treat, sctype) %>% summarise_all(funs(mean))
urban <- school %>% group_by(treat, urban) %>% summarise_all(funs(mean))
region <- school %>% group_by(treat, region) %>% summarise_all(funs(mean))
school %>% group_by(treat, homework) %>% summarise_all(funs(mean))
#Let the matching begin
# For this reason, the propensity score is now calculated, whereby it is calculated for three
# different distance metrics
# logistically, probalistically and mahalanobis. (Steps 1 to 4 from page 29 of lecture 6 are performed)
#Logit as distance metric
##################################################################################################################
psm.1_l <- matchit(treat~parented + sex + race + sctype + urban +region +ses + ratio,
data=school, method = "nearest", ratio=1, distance = "logit")
psm.1_l
love.plot(psm.1_l) # Absolute mean difference should be closer to 0
summary(psm.1_l)
plot(psm.1_l, type = "hist")
plot(psm.1_l, type = "jitter")
matcheddata1_l <- match.data(psm.1_l)
matcheddata1_l
summary(lm(math~treat, matcheddata1_l))
psm.2_l <- matchit(treat~parented + sex + race + sctype + urban +region +ses + ratio,
data=school, method = "nearest", ratio=2, distance = "logit")
psm.2_l # Unmatched 154, way too much
love.plot(psm.2_l) # Sehr schlecht, die angepasteten Punkte sind sehr weit entfertn von 0
summary(psm.2_l)
matcheddata2_l <- match.data(psm.2_l)
matcheddata2_l
summary(lm(math~treat, matcheddata2_l))
psm.3_l <- matchit(treat~parented + sex + race + sctype + urban +region +ses + ratio,
data=school, method = "nearest", ratio=3, distance = "logit")
psm.3_l
love.plot(psm.3_l) # Unmatched 154, way too much
summary(psm.3_l)
matcheddata3_l <- match.data(psm.3_l)
matcheddata3_l
summary(lm(math~treat, matcheddata3_l))
psm.4_l <- matchit(treat~parented + sex + race + sctype + urban +region +ses + ratio,
data=school, method = "nearest", ratio=4, distance = "logit")
psm.4_l
love.plot(psm.4_l)
summary(psm.4_l)
matcheddata4_l <- match.data(psm.4_l)
matcheddata4_l
summary(lm(math~treat, matcheddata4_l))
#Probit as distance metric
##################################################################################################################
psm.1_p <- matchit(treat~parented + sex + race + sctype + urban +region +ses + ratio,
data=school, method = "nearest", ratio=1, distance = "probit")
psm.1_p
love.plot(psm.1_p)
summary(psm.1_p)
plot(psm.1_p, type = "hist")
plot(psm.1_p, type = "jitter")
matcheddata1_p <- match.data(psm.1_p)
matcheddata1_p
summary(lm(math~treat, matcheddata1_p))
psm.2_p <- matchit(treat~parented + sex + race + sctype + urban +region +ses + ratio,
data=school, method = "nearest", ratio=2, distance = "probit")
psm.2_p
love.plot(psm.2_p)
summary(psm.2_p)
matcheddata2_p <- match.data(psm.2_p)
matcheddata2_p
summary(lm(math~treat, matcheddata2_p))
psm.3_p <- matchit(treat~parented + sex + race + sctype + urban +region +ses + ratio,
data=school, method = "nearest", ratio=3, distance = "probit")
psm.3_p
love.plot(psm.3_p)
summary(psm.3_p)
matcheddata3_p <- match.data(psm.3_p)
matcheddata3_p
summary(lm(math~treat, matcheddata3_p))
psm.4_p <- matchit(treat~parented + sex + race + sctype + urban +region +ses + ratio,
data=school, method = "nearest", ratio=4, distance = "probit")
psm.4_p
love.plot(psm.4_p)
summary(psm.4_p)
matcheddata4_p <- match.data(ppsm.4_p)
matcheddata4_p
summary(lm(math~treat, matcheddata4_p))
#Mahalanobis as distance metric
##################################################################################################################
psm.1_m <- matchit(treat~parented + sex + race + sctype + urban +region +ses + ratio,
data=school, method = "nearest", ratio=1, distance = "mahalanobis")
psm.1_m
love.plot(psm.1_m )
summary(psm.1_m )
matcheddata1_m <- match.data(psm.1_m )
matcheddata1_m
summary(lm(math~treat, matcheddata1_m))
psm.2_m <- matchit(treat~parented + sex + race + sctype + urban +region +ses + ratio,
data=school, method = "nearest", ratio=2, distance = "mahalanobis")
psm.2_m
love.plot(psm.2_m )
summary(psm.2_m )
matcheddata2_m <- match.data(psm.2_m )
matcheddata2_m
summary(lm(math~treat, matcheddata2_m))
psm.3_m <- matchit(treat~parented + sex + race + sctype + urban +region +ses + ratio,
data=school, method = "nearest", ratio=3, distance = "mahalanobis")
psm.3_m
love.plot(psm.3_m)
summary(psm.3_m)
matcheddata3_m <- match.data(psm.3_m)
matcheddata3_m
summary(lm(math~treat, matcheddata3_m))
psm.4_m <- matchit(treat~parented + sex + race + sctype + urban +region +ses + ratio,
data=school, method = "nearest", ratio=4, distance = "mahalanobis")
psm.4_m
love.plot(psm.4_m)
summary(psm.4_m)
matcheddata4_m <- match.data(psm.4_m)
matcheddata4_m
summary(lm(math~treat, matcheddata4_m))
# 6. Calculate the effect of the treatment on the outcome in the matched dataset (I've tried something here.)
library("Zelig")
#
z.out1 <- zelig(math ~ parented + sex + race + sctype + urban + region +ses + ratio , data = match.data(psm.1_l, "control"), model = "ls")
x.out1 <- setx(z.out1, data = match.data(psm.1_l, "treat"), cond = TRUE)
s.out1 <- sim(z.out1, x = x.out1)