-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstractedFunctions.R
127 lines (113 loc) · 5.1 KB
/
abstractedFunctions.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
# Abstracted functions for more customizeable crime simulation
# Still need some work
treatAsVRI <- function(violentCrimeCollapsed,treatedDistricts,treatTime,controlTime) {
# input:
# violentCrimeCollapsed: collapsed dataset with crime counts
# treatedDistricts: the districts to treat (vector of numbers),
# treatTime: time Period interval() lubridate
# controlTime: time Period (as above) to treat as the control period
#treatment dates are feb 2012 through August 2012, and pre-period 6 months before that
treatPeriod <- interval(ymd(20120201), ymd(20120831))
sixMonthPre <- int_shift(treatPeriod,-as.duration(treatPeriod))
#treatedDistricts <- c(7,11)
# --> end parameters
violentCrimeCollapsed$treatDistrict = violentCrimeCollapsed$District %in% treatedDistricts
violentCrimeCollapsed$Period = NA
violentCrimeCollapsed$Period[violentCrimeCollapsed$day %within% treatPeriod] <- "treat"
violentCrimeCollapsed$Period[violentCrimeCollapsed$day %within% sixMonthPre] <- "control"
primt(table(violentCrimeCollapsed$Period))
return(violentCrimeCollapsed)
}
crimeddiff <- function(violentCrimeCollapsed) {
# input: a collapsed dataset with day, count, Period (= treat, control) and treatDistrict
# output: coeffecient on Period= treat and treatDistrictTRUE
#standard differences in differences:
diffTable <-
violentCrimeCollapsed %>%
filter(!is.na(Period)) %>%
group_by(Period,treatDistrict) %>%
summarise(totalCount = log(sum(count)))
# what is the diff-n-diff estimator?
with(diffTable,
lm(totalCount ~ treatDistrict*Period)) %>%
coefficients %>% `[`(4)
}
placeboWrapper <- function(violentCrimeCollapsed,
numDistrictsToTreat,
intervalPeriod,
iterNumber
)
{
#input:
#output: a data-frame of coeffecients
allDistricts <- violentCrimeCollapsed[["District"]] %>% unique
replicate(n = iterNumber, expr = {
violentCrimeCollapsed %>%
treatAsVRI(
treatedDistricts = allDistricts %>% sample(numDistrictsToTreat),
#let's use defaults:
treatTime <- interval(ymd(20120201), ymd(20120831)),
controlTime <- int_shift(treatPeriod,-as.duration(treatPeriod))
) %>%
crimeddiff
}
)
}
quickRes <-
placeboWrapper(violentCrimeCollapsed,
2,
0,
40)
treatAsVRI <- function(violentCrimeCollapsed,treatedDistricts,treatTime,controlTime) {
# input:
# violentCrimeCollapsed: collapsed dataset with crime counts
# treatedDistricts: the districts to treat (vector of numbers),
# treatTime: time Period interval() lubridate
# controlTime: time Period (as above) to treat as the control period
#treatment dates are feb 2012 through August 2012, and pre-period 6 months before that
treatPeriod <- interval(ymd(20120201), ymd(20120831))
sixMonthPre <- int_shift(treatPeriod,-as.duration(treatPeriod))
#treatedDistricts <- c(7,11)
# --> end parameters
violentCrimeCollapsed$treatDistrict = violentCrimeCollapsed$District %in% treatedDistricts
violentCrimeCollapsed$Period = NA
violentCrimeCollapsed$Period[violentCrimeCollapsed$day %within% treatPeriod] <- "treat"
violentCrimeCollapsed$Period[violentCrimeCollapsed$day %within% sixMonthPre] <- "control"
print(table(violentCrimeCollapsed$Period))
return(violentCrimeCollapsed)
}
crimeddiff <- function(violentCrimeCollapsed) {
# input: a collapsed dataset with day, count, Period (= treat, control) and treatDistrict
# output: coeffecient on Period= treat and treatDistrictTRUE
#standard differences in differences:
diffTable <-
violentCrimeCollapsed %>%
filter(!is.na(Period)) %>%
group_by(Period,treatDistrict) %>%
summarise(totalCount = log(sum(count)))
# what is the diff-n-diff estimator?
with(diffTable,
lm(totalCount ~ treatDistrict*Period)) %>%
coefficients %>% `[`(4)
}
placeboWrapper <- function(violentCrimeCollapsed,
numDistrictsToTreat,
intervalPeriod,
iterNumber
)
{
#input:
#output: a data-frame of coeffecients
allDistricts <- violentCrimeCollapsed[["District"]] %>% unique
replicate(n = iterNumber, expr = {
violentCrimeCollapsed %>%
treatAsVRI(
treatedDistricts = allDistricts %>% sample(numDistrictsToTreat),
#let's use defaults:
treatTime <- interval(ymd(20120201), ymd(20120831)),
controlTime <- int_shift(treatPeriod,-as.duration(treatPeriod))
) %>%
crimeddiff
}
)
}