forked from bcallaway11/did_chapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_study_reg_weights.R
115 lines (103 loc) · 2.94 KB
/
event_study_reg_weights.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
#-----------------------------------------------------------------------------
#
# event_study_reg_weights.R
#
# * This is code/functions for computing weights on underlying
# an event study regression.
#
# * In order to run this code, one should first run a Callaway &
# Sant'Anna att_gt function with a universal base period,
# w/o covariates and using the "nevertreated" comparison group,
# and have saved this in a variable called `cs_res` The data
# should also be saved in a variable called `this_data`.
#
#-----------------------------------------------------------------------------
# recover info about time periods
tlist <- unique(this_data$year)
nT <- length(tlist)
# recover unique event times
e.seq <- sort(unique(this_data$e[this_data$e != -1]))
# number of units
n <- length(unique(this_data$id))
# list of groups excluding never treated
glist <- sort(unique(this_data$G))[-1]
#' Function to recover group-time average treatment effects
#' from a CS att_gt call
#'
#' @param g group
#' @param e event time
#'
#' @return the value of ATT(g,g+e)
get_attgt <- function(g,e) {
attgt_idx <- cs_res$group == g & cs_res$t == g+e
if( !(any(attgt_idx)) ) return(0)
attgt <- cs_res$att[attgt_idx]
attgt
}
#' Function to compute weights given by relative sizes of groups
#'
#' @param g group
#'
#' @return P(G=g)
p_g <- function(g) {
mean(this_data$G==g)
}
#' Function to compute first part of particular elements of h_e
#'
#' @param e event time
#' @param g group
#' @param t time period
#'
#' @return first component of weights
he <- function(e,g,t) {
( 1*(g+e==t) - 1*( (g+e) %in% tlist )/nT) * (g != 0)
}
#' Function to compute vector of h_e
#'
#' @param g group
#' @param t time period
#'
#' @return vector of h_e
hinner <- function(g,t) {
sapply(e.seq, he, g=g,t=t)
}
#' Function to compute h(g,t) from chapter; this applies `hinner`
#' and then de-means it
#'
#' @param g group
#' @param t time period
#'
#' @return vector of demeaned h_e
h <- function(g,t) {
hinner(g,t) - apply(sapply(glist, function(gg) hinner(gg,t)*p_g(gg)), 1, sum)
}
#' Function to compute event study regression weights or event
#' study regression components
#'
#' @param g group
#' @param e event time
#' @param weightonly whether or not to return only the weights or
#' to return the weights multiplied times the corresponding values
#' of ATT(g,g+e)
#'
#' @return weights for group at particular event time or contribution
#' of a particular ATT(g,g+e)
es_weights <- function(g,e,weightonly=FALSE) {
part1 <- solve(t(ddotDit)%*%Dit/n)
part2 <- as.matrix(h(g,g+e))*( (g+e) %in% tlist )*(g!=0)*p_g(g)
if (!weightonly) part2 <- part2*get_attgt(g,e)
part1 %*% part2
}
#' Function to recover event study regression parameters from
#' underlying components
#'
#' @return event study regression parameters
es_weights_reg <- function() {
out <- matrix(data=0, nrow=length(e.seq))
for (e in e.seq) {
for (g in glist) {
out <- out + es_weights(g,e)
}
}
out
}