Implementing the panel event study using Stata
来自:苏格拉底大王(I am serious with Socrates.)
用事件研究法开展政策评估的过程, 手把手教学文章!
原创 计量经济圈 计量经济圈 2023-02-06 21:19 发表于新加坡
Clarke, D., & Tapia-Schythe, K. (2021). Implementing the panel event study. The Stata Journal, 21(4), 853–884.
Many studies estimate the impact of exposure to some quasi-experimental policy or event using a panel event study design. These models, as a generalized extension of ‘differencein-differences’ or two-way fixed effect models, allow for dynamic lags and leads to the event of interest to be estimated, while also controlling for fixed factors (often) by area and time. In this paper we discuss the set-up of the panel event study design in a range of situations, and lay out a number of practical considerations for its estimation. We describe a Stata command eventdd that allows for simple estimation, inference, and visualization of event study models in a range of circumstances. We then provide a number of examples to illustrate eventdd’s use and flexibility, as well as its interaction with various native Stata routines, and other relevant user-written libraries such as reghdfe and boottest.
https://www.bilibili.com/video/BV1Fy4y1Q78E/
考虑一个包含个体(州,state)s和时间t的面板。我们感兴趣的是估计事件发生在不同时间和不同州的影响。Events表示记录在个体s中采用该事件的时间段t,yst表示结果,面板事件的设定如下:

这里的µs和λt是个体和时间固定效应,Xst是(可选地)时变控制变量,而εst是一个未观察到的误差项。在(1)中感兴趣事件的滞后期和领先期定义如下:

可以看到滞后和领先期是关于事件时期的二值变量。(2)和(5)中的J和K表示滞后和领先超过J期和K期的时期累积。一期的滞后或领先期被忽略以捕获事件已发生和不发生的区域之间的基准差异。在(1)的设定下,基准期是j=1的情况。


面板事件研究是标准的双向固定效应(有时称为DID)模型的扩展,在该模型中,在处理状态下事件发生后的所有时期都包含一个“事件后(Post Event)”指标:

(6)

eventdd varlist [if ] [ in ] [ weight ], timevar(timevar) ci(string)
[method baseline(#) level(#) accum lags(#) leads(#) noend noline keepbal(varname) absorb(varname) wboot wboot_op(string) balanced inrange graph_op(string) ci_op(string) coef_op(string) endpoints_op(string) * ]
Options 选项
- timevar 必选项。指定的时间变量应包含一个标准化值,其中0对应一个给定个体的感兴趣事件发生的时间段,-1表示事件前的一期,1表示事件后的一期,以此类推。对于没有发生该事件的任何个体(控制组),此变量应该包含缺失的值。
- ci(string)是一个必需的选项,指示命令将生成的图形类型。rarea (带区域阴影)、rcap (带封顶尖钉)、rline (带线条),rcap 是默认的图表类型。只能指定一种类型,并且所有的时间间隔都将是同一类型。该外观可以用ci_op()进行修改。
- baseline(#)指定了事件研究的参考基期,这是一个基期省略(baseline omitted category)的类别,应该对事件研究输出中的所有其他周期进行比较。默认情况下,如公式1所示,这个值设置为−1。
- level(#)指定置信区间的百分比置信水平。默认为置信水平(95),也可以按设置的置信水平设置。这设置了回归输出的置信区间的水平,以及事件研究图和矩阵。如果需要野聚类标准误(wild clustered standard errors),会被被传递给boottest命令。
- accum指定所有超过某些指定值的周期都应累积到最终点,在方程1中用J和K表示。例如,如果把滞后期(#)和领先期(#)都设置为10,那么accum将在回归和图形输出中显示一个单一系数,捕获处置前/处置后10个或更多的周期。如果没有指定accum,所有可能的滞后期和领先期将包括在模型和图形输出中。
- lags(#)表示在事件研究中需要考虑的事件前时期的最大数量。这个选项需要在accum, keepbal 或者 inrange之后才能使用。只允许使用整数值。
- leads(#)表示在事件研究中需要考虑的事件后时期的最大数量。这个选项需要在accum, keepbal 或者 inrange之后才能使用。只允许使用整数值。
- noend当指定accum选项时,图形输出中不显示累计端点。
- keepbal(varname)指定仅保留面板中平衡的个体以进行估算。这里的变量名表示个体的面板变量(如州)。在这种情况下,“balance”指的是在日历时间上的平衡。下面讨论的另一种选项((balanced)只允许在图形输出中考虑相对于处理的平衡滞后期和领先期。
- ols指的是使用Statat的regress命令估计事件研究模型的图形输出。在这种情况下,个体的固定效应和时间固定效应必须包含在命令语法中指示的变量列表中。这是默认的估计方法。
- fe指的是使用Statat的extreg命令估计事件研究模型的图形输出。在这种情况下,数据必须在使用之前进行xtset,并且个体固定效应不应该包含在命令语法中指示的变量列表中。时间固定效应仍然需要包含在命令语法中指示的变量列表中。
- hdfe指的是使用Correia(2016)的用户编写的reghdfe命令(如果已安装)来估计事件研究模型的图形输出。如果指定了此选项,则还应指定absorb(varlist)选项,以指示在回归中应控制哪些固定效应。absorb(varlist)中指示的任何固定效应都不应包含在命令语法中指示的变量列表中。此选项不能与wboot选项结合使用。
- absorb(varlist)仅在指定hdfe估计选项时才需要此选项。varlist确定要吸收的固定效应(例如个体固定效应)。有关更多详细信息,请参阅reghdfe(如果已安装)。
- wboot表示由野聚类标准误(wild clustered standard errors)进行估计来生成输出图形。当指定时,每个滞后期和领先期的置信区间将使用野聚类自助法(wild cluster bootstrap)来计算。这需要Roodman(2015)的用户编写的boottest(如果已安装)。此选项不能与hdfe估计选项结合使用。
- wboot_op(string)允许包含野聚类自助法中允许的任何其他选项,包括seed(#)为基于模拟的计算设置种子并复制置信区间,以及bootclust(varname)指定哪个变量来进行野聚类自助,等等。当设定置信水平(默认为95)时,应该在命令的置信水平选项中指示,并将其传递给wboot_op()。
- balanced只有“平衡”的滞后期和领先期被绘制出来。这将产生一个只显示每个处理样本都有数据的滞后期和领先期的图片。因此,绘制的所有系数都将基于数据中的所有个体。虽然只绘制平衡的滞后期和领先期,但所有的单位和时间段将包括在事件研究的估计中。
- inrange只绘制了指定的滞后期和领先期。虽然只绘制指定的的滞后期和领先期,但所有个体和时间时期都将包括在事件研究的估计中。
- noline指的是在x轴上的事件之前的行不显示在图形输出上。
- graph_op(string)允许包含twoway_options中允许的任何其他绘图选项,包括title_options, added_lines_options, axis_label_options等等。这也允许对图形轴使用替代标签。如果没有设定,将提供一个标准的图形输出。
- ci_op(string)允许包含twoway_rarea、twoway_rcap或twoway_rline中允许的置信区间的任何图形选项,这取决于ci()中指示的CI类型;包括area_options,line_options和connect_options等。这并不允许使用graph_op()的一般选项。如果没有指定,则将提供一个标准的图形输出。
- coef_op(string)允许包含散点图中允许系数的任何图形选项,包括marker_options和marker_label_options等。这并不允许使用graph_op()的一般选项。如果没有指定,则将提供一个标准的图形输出。
- endpoints_op(string)允许包含在散点中允许的端点系数的任何图形选项,包括marker_options和marker_label_options等。这仅在指定accum和不允许使用graph_op()的一般选项时可用。如果没有指定,则将提供一个标准的图形输出。
- *可以包括regress、xtreg或reghdfe允许的任何其他估计选项,并将其传递给指定的估计命令。这允许包含聚类标准误( clustered standard errors)或其他方差估计,如自举(bootstrap)或重叠(jackknife)估计(见vce_option),以及与面板事件研究回归模型中的潜在估计或推断相关的任何其他选项。
- Returned Objects 返回结果
Macros: 宏
e(cmd) eventdd
e(cmdline) command as typed 命令的类型
e(depvar) name of dependent variable 因变量的名称
e(wtype) weight type 权重类型
e(clustvar) name of cluster variable 聚类变量名称
e(vce) vcetype specified in vce() 在vce()中指定的vce类型
e(vcetype) title used to label Std. Err. 标准误差的标题
Matrices: 矩阵
e(b) coefficient vector 系数向量
e(V) variance-covariance matrix of the estimators 方差-协方差估计矩阵
e(leads) all event leads, their lower bound, the point estimate, and their upper bound
所有事件的领先期,包括它们的下界、点估计和它们的上界
e(lags) all event lags, their lower bound, the point estimate, and their upper bound
所有事件的滞后期,包括它们的下界、点估计和它们的上界
e(V_lags_leads) variance-covariance matrix of the lags and leads estimators
滞后期和领先期的方差-协方差估计矩阵
4 Examples based on an Empirical Application 基于经验应用的实例
. webuse set www.damianclarke.net/stata/
(prefix now "http://www.damianclarke.net/stata")
. webuse bacon_example.dta, clear
(Stevenson and Wolfers (2006) divorce example, provided by Goldring et al.) //读取数据
. gen timeToTreat = year - _nfd //设置变量
(429 missing values generated)
第二步是根据公式1-5来估计事件研究。在本例中,事件研究模型的一般形式包括所有滞后期和领先期是:

(8)
. #delimit ;
delimiter now ;
. qui
> eventdd asmrs pcinc asmrh cases i.year i.stfips, timevar(timeToTreat) ci(rcap)
> cluster(stfips) graph_op(ytitle("Suicides per 1m Women") xlabel(-20(5)25));//回归及相应设置
. #delimit cr
delimiter now cr
mat list e(lags)
e(lags)[21,4]

由于我们没有指定估计方法,eventdd使用Stata的回归命令通过普通最小二乘回归来估计模型(如果指定了ols选项,则得到相同的结果)。我们还可以为事件研究模型要求其他估计方法;如果我们指定了fe选项,那么该模型会使用固定效应(FE)进行估计。
. #delimit ;
delimiter now ;
. eventdd asmrs pcinc asmrh cases, hdfe absorb(i.stfips i.year) timevar(timeToTreat)//指定感兴趣的固定效应
> ci(rcap) cluster(stfips) graph_op(ytitle("Suicides per 1m Women") xlabel(-20(5)25));




. #delimit ;
delimiter now ;
. test lag21 lag20 lag19 lag18 lag17 lag16 lag15 lag14 lag13 lag12 lag11 lag10
> lag9 lag8 lag7 lag6 lag5 lag4 lag3 lag2; //假设检验
( 1) lag21 = 0
( 2) lag20 = 0
( 3) lag19 = 0
( 4) lag18 = 0
( 5) lag17 = 0
( 6) lag16 = 0
( 7) lag15 = 0
( 8) lag14 = 0
( 9) lag13 = 0
(10) lag12 = 0
(11) lag11 = 0
(12) lag10 = 0
(13) lag9 = 0
(14) lag8 = 0
(15) lag7 = 0
(16) lag6 = 0
(17) lag5 = 0
(18) lag4 = 0
(19) lag3 = 0
(20) lag2 = 0
F( 20, 48) = 32.13
Prob > F = 0.0000
. #delimit cr
delimiter now cr

. #delimit ;
delimiter now ;
. qui
> eventdd asmrs pcinc asmrh cases i.year, fe timevar(timeToTreat) ci(rcap) inrange
> lags(10) leads(10) cluster(stfips) graph_op(ytitle("Suicides per 1m Women"));//限制某些滞后或领先期
Note: with FE option do not include in varlist the categorical variables
that identify the individual fixed effect
. #delimit cr
delimiter now cr

. #delimit ;
delimiter now ;
. qui
> eventdd asmrs pcinc asmrh cases i.year, fe timevar(timeToTreat) ci(rcap)
> balanced cluster(stfips) graph_op(ytitle("Suicides per 1m Women"));//设置平衡选项
Note: with FE option do not include in varlist the categorical variables
that identify the individual fixed effect
. #delimit cr
delimiter now cr

delimiter now ;
. eventdd asmrs pcinc asmrh cases, hdfe timevar(timeToTreat) ci(rcap) cluster(stfips)
> absorb(i.stfips i.year) keepbal(stfips) lags(15) leads(10) graph_op(ytitle("Suicides
> per 1m Women")); //设置keepbal选项
Note: with HDFE option do not include in varlist the categorical variables
that identify the fixed effects to be absorbed in absorb()
(MWFE estimator converged in 5 iterations)
warning: missing F statistic; dropped variables due to collinearity or too few clusters
具体回归结果如下:




. #delimit ;
delimiter now ;
. qui
> eventdd asmrs pcinc asmrh cases i.year, fe timevar(timeToTreat) ci(rcap)
> cluster(stfips) accum lags(15) leads(10) graph_op(ytitle("Suicides per 1m Women"));//设置accum选项
Note: with FE option do not include in varlist the categorical variables
that identify the individual fixed effect
. #delimit cr
delimiter now cr

. #delimit ;
delimiter now ;
. qui
> eventdd asmrs pcinc asmrh cases i.year, fe timevar(timeToTreat) ci(rcap)
> cluster(stfips) accum lags(15) leads(10) noend graph_op( ytitle("Suicides per
> 1m Women")); //设置noend选项
Note: with FE option do not include in varlist the categorical variables that identify the individual fixed effect
. #delimit cr
delimiter now cr

delimiter now ;
. qui
> eventdd asmrs pcinc asmrh cases i.year, fe timevar(timeToTreat) ci(rcap) noline
> baseline(-11) cluster(stfips) graph_op(ytitle("Suicides per 1m Women") //改变基期
> xlabel(-20(5)25));
Note: with FE option do not include in varlist the categorical variables
that identify the individual fixed effect
. #delimit cr
delimiter now cr
4.2 Inference Options 推断选项

. #delimit ;
delimiter now ;
. qui
> eventdd asmrs pcinc asmrh cases i.year, fe timevar(timeToTreat) ci(rcap)
> cluster(stfips) accum lags(10) leads(10) wboot wboot_op(seed(1303)) graph_op(ytitle(
> "Suicides per 1m Women")); //设置野聚类自助法进行推断
Note: with FE option do not include in varlist the categorical variables
that identify the individual fixed effect
. #delimit cr
delimiter now cr

eventdd命令允许使用Stata中的twoway graph来可视化多种形式的置信区间。该命令要求用户通过指定ci(rarea)来指定带有区域阴影的间隔,ci(rcap)表示带有上限峰值的间隔,ci(rline)表示带线的间隔。图5显示了图1中的初始事件研究,但是现在有了三种可选类型的图。用户必须指定一种类型,并且这将适用于所有显示的时间间隔。




. #delimit ;
delimiter now ;
. qui
> eventdd asmrs pcinc asmrh cases i.year, fe timevar(timeToTreat) ci(rarea)//设置图形形状
> cluster(stfips) accum lags(15) leads(20) graph_op(xlabel(-15 "{&le} -15"
> -10 "-10" -5 "-5" 0 "0" 5 "5" 10 "10" 15 "15" 20 "{&ge} 20") scheme(s1mono)
> ytitle("Suicides per 1m Women")) ci_op(fcolor(ltblue%45)) coef_op(msymbol(Oh))
> endpoints_op(msymbol(O));
Note: with FE option do not include in varlist the categorical variables
that identify the individual fixed effect
. #delimit cr
delimiter now cr
5 Conclusions 结论
最新讨论 ( 更多 )
- 政治等级制度的经济后果:公元 1000 年—2000 年中国政权更迭... (苏格拉底大王)
- 从轶事到见解:简化研究想法的生成过程 (苏格拉底大王)
- 史蒂夫·列维特(Steven D. Levitt):我为何决定离开学术界 (苏格拉底大王)
- AI是一个随身的写作伙伴 (苏格拉底大王)
- 我的生产力工具就是个无限延伸的「.txt」文件而已 (苏格拉底大王)