forked from IanHo2019/DID_Handbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamic_DID_(Sino-US_Trade).do
447 lines (347 loc) · 12.7 KB
/
Dynamic_DID_(Sino-US_Trade).do
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
* This do file runs various dynamic DID specifications using data on China's export to the US in 2000-2009.
* Author: Ian He
* Date: Dec 6, 2023
* Stata Version: 18
clear all
global localdir "D:\research\DID"
global dtadir "$localdir\Data"
global figdir "$localdir\Figure"
********************************************************************************
use "$dtadir\USA_AD_CHN.dta", clear
**# Create a series of dummies for duty impositions
gen period_duty = year - year_des_duty
gen treated = (period_duty < . & period_duty >= 0)
gen Dn3 = (period_duty < -2)
forvalues i = 2(-1)1 {
gen Dn`i' = (period_duty == -`i')
}
forvalues i = 0(1)3 {
gen D`i' = (period_duty == `i')
}
gen D4 = (period_duty >= 4) & (period_duty != .)
**# Classical Dynamic DID
* ssc install reghdfe, replace
* ssc install ftools, replace
* ssc install estout, replace
encode hs06, generate(product) // string variables are not allowed for "xtset" and regressions
local dep = "value quantity company_num m_quantity"
foreach y in `dep'{
quietly{
eststo reg_`y': reghdfe ln_`y' Dn3 Dn2 D0-D4, absorb(product year) vce(cluster product#year)
}
}
estout reg*, keep(D*) ///
coll(none) cells(b(star fmt(3)) se(par fmt(3))) ///
starlevels(* .1 ** .05 *** .01) legend ///
stats(N r2_a, nostar labels("Observations" "Adjusted R-Square") fmt("%9.0fc" 3))
********************************************************************************
**# Sun and Abraham (2021)
* ssc install eventstudyinteract, replace
* ssc install avar, replace
* ssc install event_plot, replace
gen first_union = year_des_duty
gen never_union = (first_union == .)
local dep = "value quantity company_num m_quantity"
foreach y in `dep'{
* Regression
eventstudyinteract ln_`y' Dn3 Dn2 D0-D4, cohort(first_union) control_cohort(never_union) absorb(product year) vce(cluster product#year)
* Visualization
if "`y'"=="value"{
local panel = "A)"
local title = "ln(Value)"
}
if "`y'"=="quantity"{
local panel = "B)"
local title = "ln(Total Quantity)"
}
if "`y'"=="company_num"{
local panel = "C)"
local title = "ln(Number of Exporters)"
}
if "`y'"=="m_quantity"{
local panel = "D)"
local title = "ln(Mean Quantity)"
}
forvalue i = 1/7 {
local m_`i' = e(b_iw)[1,`i']
local v_`i' = e(V_iw)[`i',`i']
}
matrix input matb_sa = (`m_1',`m_2',0,`m_3',`m_4',`m_5',`m_6',`m_7')
mat colnames matb_sa = ld3 ld2 ld1 lg0 lg1 lg2 lg3 lg4
matrix input mats_sa = (`v_1',`v_2',0,`v_3',`v_4',`v_5',`v_6',`v_7')
mat colnames mats_sa = ld3 ld2 ld1 lg0 lg1 lg2 lg3 lg4
event_plot matb_sa#mats_sa, ///
stub_lag(lg#) stub_lead(ld#) ///
ciplottype(rcap) plottype(scatter) ///
lag_opt(msymbol(D) mcolor(black) msize(small)) ///
lead_opt(msymbol(D) mcolor(black) msize(small)) ///
lag_ci_opt(lcolor(black) lwidth(medthin)) ///
lead_ci_opt(lcolor(black) lwidth(medthin)) ///
graph_opt( ///
title("`panel' `title'", size(medlarge) position(11)) ///
xtitle("Period", height(5)) xsize(5) ysize(4) ///
ytitle("Coefficient", height(5)) ///
xline(0, lpattern(dash) lcolor(gs12) lwidth(thin)) ///
yline(0, lpattern(solid) lcolor(gs12) lwidth(thin)) ///
xlabel(-3 "< -2" -2(1)3 4 "> 3", labsize(small)) ///
ylabel(, labsize(small)) ///
legend(order(1 "Coefficient" 2 "95% CI") size(*0.8) rows(1) region(lc(black))) ///
name(sa_`y', replace) ///
graphregion(color(white)) ///
)
}
* net install grc1leg.pkg, replace
grc1leg sa_value sa_quantity sa_company_num sa_m_quantity, ///
legendfrom(sa_value) cols(2) ///
graphregion(fcolor(white) lcolor(white)) ///
name(sa_fig, replace)
gr draw sa_fig, ysize(5) xsize(6.5)
graph export "$figdir\SA_DID_Trade_Destruction.pdf", replace
********************************************************************************
**# Callaway & Sant'Anna (2021)
* ssc install csdid, replace
* ssc install drdid, replace
gen gvar = year_des_duty
recode gvar (. = 0)
local dep = "value quantity company_num m_quantity"
foreach y in `dep'{
quietly csdid ln_`y', ivar(product) time(year) gvar(gvar) method(dripw) wboot(reps(10000)) rseed(1)
csdid_estat event, window(-3 4) estore(cs_`y') wboot(reps(10000)) rseed(1)
}
estout cs_*, keep(T*) ///
coll(none) cells(b(star fmt(3)) se(par fmt(3))) ///
starlevels(* .1 ** .05 *** .01) legend ///
stats(N r2_a, nostar labels("Observations" "Adjusted R-Square") fmt("%9.0fc" 3))
* Visualization
local ylist = "value quantity company_num m_quantity"
foreach y in `ylist'{
if "`y'"=="value"{
local panel = "A)"
local title = "ln(Value)"
}
if "`y'"=="quantity"{
local panel = "B)"
local title = "ln(Total Quantity)"
}
if "`y'"=="company_num"{
local panel = "C)"
local title = "ln(Number of Exporters)"
}
if "`y'"=="m_quantity"{
local panel = "D)"
local title = "ln(Mean Quantity)"
}
event_plot cs_`y', default_look ///
stub_lag(Tp#) stub_lead(Tm#) together ///
graph_opt( ///
xtitle("Period") ytitle("ATT") ///
title("`panel' `title'", size(medlarge) position(11)) ///
xlab(-3(1)4, labsize(small)) ///
ylab(, angle(90) nogrid labsize(small)) ///
legend(lab(1 "Coefficient") lab(2 "95% CI") size(*0.8) rows(1) region(lc(black))) ///
name(cs_`y', replace) ///
)
}
grc1leg cs_value cs_quantity cs_company_num cs_m_quantity, ///
legendfrom(cs_value) cols(2) ///
graphregion(fcolor(white) lcolor(white)) ///
name(cs_fig, replace)
gr draw cs_fig, ysize(5) xsize(6.5)
graph export "$figdir\CS_DID_Trade_Destruction.pdf", replace
********************************************************************************
**# de Chaisemartin & D'Haultfoeuille (2020, 2022)
* ssc install did_multiplegt, replace
egen clst = group(product year) // construct a interaction variable for clustering later
local dep = "value quantity company_num m_quantity"
foreach y in `dep'{
* Regression
did_multiplegt ln_`y' year_des_duty year treated, ///
robust_dynamic dynamic(4) placebo(2) jointtestplacebo ///
seed(1) breps(100) cluster(clst)
* Visualization
if "`y'"=="value"{
local panel = "A)"
local title = "ln(Value)"
}
if "`y'"=="quantity"{
local panel = "B)"
local title = "ln(Total Quantity)"
}
if "`y'"=="company_num"{
local panel = "C)"
local title = "ln(Number of Exporters)"
}
if "`y'"=="m_quantity"{
local panel = "D)"
local title = "ln(Mean Quantity)"
}
forvalue i=1/8 {
local m_`i' = e(estimates)[`i',1]
local v_`i' = e(variances)[`i',1]
}
matrix input matb_DIDl = (`m_1',`m_2',`m_3',`m_4',`m_5',0,`m_7',`m_8')
mat colnames matb_DIDl = lg0 lg1 lg2 lg3 lg4 ld1 ld2 ld3
matrix input mats_DIDl = (`v_1',`v_2',`v_3',`v_4',`v_5',0,`v_7',`v_8')
mat colnames mats_DIDl = lg0 lg1 lg2 lg3 lg4 ld1 ld2 ld3
event_plot matb_DIDl#mats_DIDl, ///
stub_lag(lg#) stub_lead(ld#) ///
ciplottype(rcap) plottype(scatter) ///
lag_opt(msymbol(D) mcolor(black) msize(small)) ///
lead_opt(msymbol(D) mcolor(black) msize(small)) ///
lag_ci_opt(lcolor(black) lwidth(medthin)) ///
lead_ci_opt(lcolor(black) lwidth(medthin)) ///
graph_opt( ///
title("`panel' `title'", size(medlarge) position(11)) ///
xtitle("Period", height(5)) xsize(5) ysize(4) ///
ytitle("Average Effect", height(5)) ///
xline(0, lpattern(dash) lcolor(gs12) lwidth(thin)) ///
yline(0, lpattern(solid) lcolor(gs12) lwidth(thin)) ///
xlabel(-3/4, nogrid labsize(small)) ///
ylabel(, labsize(small)) ///
legend(order(1 "Coefficient" 2 "95% CI") size(*0.8) position(6) rows(1) region(lc(black))) ///
name(DIDl_`y', replace) ///
graphregion(color(white)) ///
)
}
grc1leg DIDl_value DIDl_quantity DIDl_company_num DIDl_m_quantity, ///
legendfrom(DIDl_value) cols(2) ///
graphregion(fcolor(white) lcolor(white)) ///
name(DIDl_fig, replace)
gr draw DIDl_fig, ysize(5) xsize(6.5)
graph export "$figdir\CD_DIDl_Trade_Destruction.pdf", replace
********************************************************************************
**# Borusyak, Jaravel & Spiess (2022)
* ssc install did_imputation, replace
gen Ei = year_des_duty
local ylist = "value quantity company_num m_quantity"
foreach y in `ylist'{
quietly{
eststo imp_`y': did_imputation ln_`y' product year Ei, fe(product year) cluster(clst) horizons(0/4) pretrends(2) minn(0) autosample
}
}
estout imp*, ///
coll(none) cells(b(star fmt(3)) se(par fmt(3))) ///
starlevels(* .1 ** .05 *** .01) legend ///
stats(N r2_a, nostar labels("Observations" "Adjusted R-Square") fmt("%9.0fc" 3))
* Visualization
local ylist = "value quantity company_num m_quantity"
foreach y in `ylist'{
if "`y'"=="value"{
local panel = "A)"
local title = "ln(Value)"
}
if "`y'"=="quantity"{
local panel = "B)"
local title = "ln(Total Quantity)"
}
if "`y'"=="company_num"{
local panel = "C)"
local title = "ln(Number of Exporters)"
}
if "`y'"=="m_quantity"{
local panel = "D)"
local title = "ln(Mean Quantity)"
}
event_plot imp_`y', default_look ///
graph_opt( ///
xtitle("Period") ytitle("Coefficient estimate") ///
title("`panel' `title'", size(medlarge) position(11)) ///
xlab(-2(1)3 4 "> 3", labsize(small)) ///
ylab(, angle(90) nogrid labsize(small)) ///
yline(0, lcolor(gs8) lpattern(dash)) ///
legend(size(*0.8) rows(1) region(lc(black))) ///
name(imp_`y', replace) ///
)
}
grc1leg imp_value imp_quantity imp_company_num imp_m_quantity, ///
legendfrom(imp_value) cols(2) ///
graphregion(fcolor(white) lcolor(white)) ///
name(imp_fig, replace)
gr draw imp_fig, ysize(5) xsize(6.5)
graph export "$figdir\Imputation_DID_Trade_Destruction.pdf", replace
********************************************************************************
**# Wooldridge (2021)
xtset product year
local ylist = "value quantity company_num m_quantity"
foreach y in `ylist'{
if "`y'"=="value"{
local panel = "A) ln(Value)"
}
if "`y'"=="quantity"{
local panel = "B) ln(Total Quantity)"
}
if "`y'"=="company_num"{
local panel = "C) ln(Number of Exporters)"
}
if "`y'"=="m_quantity"{
local panel = "D) ln(Mean Quantity)"
}
xthdidregress twfe (ln_`y') (treated), group(product) vce(cluster clst)
estat aggregation, time ///
graph( ///
title("`panel'", position(11)) ///
xtitle("") ytitle("ATT Estimates") ///
xlabel(, angle(45) labsize(small)) ylabel(, labsize(small)) ///
legend(order(1 "Point Estimate" 2 "95% CI") rows(1) position(6) region(lc(black))) ///
name(etwfe_`y', replace) ///
)
}
grc1leg etwfe_value etwfe_quantity etwfe_company_num etwfe_m_quantity, ///
legendfrom(etwfe_value) cols(2) ///
graphregion(fcolor(white) lcolor(white)) ///
name(etwfe_fig, replace)
gr draw etwfe_fig, ysize(5) xsize(6.5)
graph export "$figdir\ETWFE_DID_Trade_Destruction.pdf", replace
********************************************************************************
**# Dube et al. (2023)
* ssc install lpdid, replace
* ssc install egenmore, replace
local dep = "value quantity company_num m_quantity"
foreach y in `dep' {
* Regression
lpdid ln_`y', time(year) unit(product) treat(treated) pre(3) post(4) cluster(clst) only_event nograph
* Customize the figure titles
if "`y'"=="value"{
local panel = "A) ln(Value)"
}
if "`y'"=="quantity"{
local panel = "B) ln(Total Quantity)"
}
if "`y'"=="company_num"{
local panel = "C) ln(Number of Exporters)"
}
if "`y'"=="m_quantity"{
local panel = "D) ln(Mean Quantity)"
}
* Save estimated results
forvalue i = 1/8 {
local b_`i' = e(results)[`i',1]
local v_`i' = e(results)[`i',2]
}
matrix input mat_lp = (`b_1', `v_1' \ `b_2', `v_2' \ 0, 0 \ `b_4', `v_4' \ `b_5', `v_5' \ `b_6', `v_6' \ `b_7', `v_7' \ `b_8', `v_8')
* Visualization
preserve
svmat mat_lp
keep mat_lp1 mat_lp2
rename mat_lp1 b
rename mat_lp2 se
gen t = _n-4
gen low = b - 1.96 * se
gen high = b + 1.96 * se
twoway (scatter b t if inrange(t, -3, 4), c(l) lc(dknavy) mc(dknavy) msymbol(smdiamond)) ///
(rcap high low t if inrange(t, -3, 4), c(l) lpattern(solid) lc(dknavy)), ///
xline(0, lc(gs11) lp(shortdash)) ///
yline(0, lc(gs11) lp(shortdash)) ///
title("`panel'", position(11)) ///
xti("Relative Year", size(medsmall)) yti("") ///
xlab(-3/4, nogrid) ///
legend(label(1 "ATT") label(2 "95% CI") rows(1) position(6) span region(lc(black))) ///
name(fig_`y', replace)
restore
}
grc1leg fig_value fig_quantity fig_company_num fig_m_quantity, ///
legendfrom(fig_value) cols(2) ///
graphregion(fcolor(white) lcolor(white)) ///
name(lpdid_fig, replace)
gr draw lpdid_fig, ysize(5) xsize(6.5)
graph export "$figdir\LP_DID_Trade_Destruction.pdf", replace