forked from codedthinking/eventstudy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheventbaseline.ado
118 lines (106 loc) · 3.98 KB
/
eventbaseline.ado
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
*! version 0.7.2 23jan2024
program eventbaseline, eclass
version 18
syntax [, pre(integer 1) post(integer 3) baseline(string) generate(string) level(real 95)] [graph]
if ("`level'" == "") {
local level 95
}
if ("`baseline'" == "") {
local baseline "-1"
}
if (e(cmd) != "xthdidregress") {
display in red "eventbaseline can only be used after xthdidregress"
error 198
}
local T1 = `pre'-1
local K = `pre'+`post'+1
local depvar = e(depvar)
local cohortvar = e(cohortvar)
local timevar = e(timevar)
tempname bad_coef bad_Var Wcum W0 b V Nobs coefplot tlabels
tempvar exclude esample
* exclude observations outside of the event window
quietly generate `exclude' = cond(`cohortvar' == 0, 0, !inrange(`timevar' - `cohortvar', -`pre', `post'))
quietly generate `esample' = e(sample) & (`exclude' == 0)
quietly count if `esample'
local Nobs = r(N)
quietly estat aggregation, dynamic(-`T1'/`post')
matrix `bad_coef' = r(b)
matrix `bad_Var' = r(V)
matrix `Wcum' = I(`K')
forvalues i = 1/`pre' {
forvalues j = 1/`i' {
matrix `Wcum'[`j', `i'] = -1.0
}
}
matrix `Wcum' = `Wcum'[1..., 1..`pre'-1], `Wcum'[1..., `pre'+1..`pre'+`post'+1]
if ("`baseline'" == "average") {
matrix `W0' = I(`K') - (J(`K', `pre', 1/`pre'), J(`K', `post'+1, 0))
}
else if ("`baseline'" == "atet") {
matrix `W0' = (J(1, `pre', -1/`pre'), J(1, `post'+1, 1/(`post'+1)))
}
else {
if (!inrange(`baseline', -`pre', -1)) {
display in red "Baseline must be between -`pre' and -1"
error 198
}
matrix `W0' = I(`K')
local bl = `pre' + `baseline' + 1
forvalues i = 1/`K' {
matrix `W0'[`i', `bl'] = `W0'[`i', `bl'] - 1.0
}
}
matrix `b' = `bad_coef' * `Wcum'' * `W0''
matrix `V' = `W0' * `Wcum' * `bad_Var' * `Wcum'' * `W0''
if ("`baseline'" == "atet") {
local colnames "ATET"
}
else {
* label coefficients
forvalues t = -`pre'/`post' {
local colnames `colnames' `t'
}
}
matrix colname `b' = `colnames'
matrix colname `V' = `colnames'
matrix rowname `V' = `colnames'
matrix `coefplot' = J(`K', 4, .)
matrix colname `coefplot' = xvar b ll ul
local tlabels ""
forvalues t = -`pre'/`post' {
local tlabels `tlabels' `t'
local i = `t' + `pre' + 1
matrix `coefplot'[`i', 1] = `t''
matrix `coefplot'[`i', 2] = `b'[1, `i']
matrix `coefplot'[`i', 3] = `b'[1, `i'] + invnormal((100-`level')/200) * sqrt(`V'[`i', `i'])
matrix `coefplot'[`i', 4] = `b'[1, `i'] - invnormal((100-`level')/200) * sqrt(`V'[`i', `i'])
}
tempname coef lower upper
if ("`generate'" != "") {
capture frame drop `generate'
frame create `generate' time coef lower upper
forvalues t = -`pre'/`post' {
local i = `t' + `pre' + 1
scalar `coef' = `b'[1, `i']
scalar `lower' = `b'[1, `i'] + invnormal((100-`level')/200) * sqrt(`V'[`i', `i'])
scalar `upper' = `b'[1, `i'] - invnormal((100-`level')/200) * sqrt(`V'[`i', `i'])
frame post `generate' (`t') (`coef') (`lower') (`upper')
}
frame `generate': tsset time
frame `generate': format coef lower upper %9.3f
}
ereturn post `b' `V', obs(`Nobs') esample(`esample')
ereturn local depvar `depvar'
ereturn local cmd eventstudy
ereturn local cmdline eventstudy `0'
_coef_table_header, title(Event study relative to `baseline') width(62)
display
_coef_table, bmat(e(b)) vmat(e(V)) level(`level') ///
depname(`depvar') coeftitle(ATET)
if ("`graph'" == "graph") {
hetdid_coefplot, mat(`coefplot') title(Event study relative to `baseline') ///
ylb(`depvar') xlb("Length of exposure to the treatment") ///
yline(0) legend(off) level(`level') yline(0, extend) ytick(0, add) ylabel(0, add) xlabel(`tlabels')
}
end