Stata事件研究图代码
Stata Event Study Graph Code
我正在尝试在 Stata 中为事件研究编写代码,但我无法完全得到我想要的。 Jacobson、LaLonde 和 Sullivan (1993),第 698 页图 3 (http://www.princeton.edu/~davidlee/wp/0.pdf),有一个与我想要的非常相似的图,只是我还想添加置信区间。
基于本教程,http://www.stata.com/meeting/germany14/abstracts/materials/de14_jann.pdf,我编写了以下代码:
sysuse auto, clear
egen t = fill(1,2,3,4,1,2,3,4)
quietly regress price ib2.t trunk weight if foreign==0
estimates store domestic
quietly regress price ib2.t trunk weight if foreign==1
estimates store foreign
coefplot (domestic, label(Domestic Cars)) (foreign, label(Foreign Cars)), drop(_cons) xline(0) vertical omitted baselevels
这产生了我想要的东西,但存在以下问题:
- 点估计和置信区间是并排的,而不是彼此重叠的(如果这是唯一的问题,这可能没问题)。
- 我的时间变量 t 出现在每个 x 标签(t=1、t=2 等)中,但我只想说(1、2 等)没有 t =.
- 在这个玩具示例中,我必须从 1 开始我的 t 编号,因为与
i
运算符组合的因子变量必须是非负数。我想让我的时间变量能够取负数。
- 我不想让
trunk
和weight
出现在剧情中。将它们放在 drop(...)
中可以吗?
- 我还希望能够在回归残差中完成所有这些操作,而不是上面的操作。
- 我想用线连接点估计。
我一点也不嫁给coefplot
命令。其他技术,尤其是使用内置 Stata 命令也完全可以接受。
希望我已经正确回答了你的问题,也许我误解了什么,但这是我的回答:
(我没有解决 5,因为我不确定你到底在寻找那个问题,但也许在看到我的解决方案之后就会清楚)
代码:
// load data same as before
sysuse auto, clear
egen t = fill(1,2,3,4,1,2,3,4)
// get coefficients and standard errors of regressions over foreign
statsby _b _se , clear by(foreign): regress price ib2.t trunk weight
// there are some extra variables we don't need/want
drop *_trunk *_weight *_cons
// generate confidence intervals and rename coefficient variables
forvalues i = 1/4 {
local j = `i'+7
gen ci_low`i' = _stat_`i' - 1.96*_stat_`j'
gen ci_high`i' = _stat_`i' + 1.96*_stat_`j'
rename _stat_`i' coef`i'
}
// no longer in need of standard error variables
drop _stat_8 _stat_9 _stat_10 _stat_11
// now, we want our data in long format so we can do a twoway graph
reshape long coef ci_low ci_high, i(foreign) j(t)
// we can label the t values so that they start below 1
lab def timeseries 1 "-1" 2 "0" 3 "1" 4 "2"
lab values t timeseries
// now graph, note each factor has two pieces, a scatter (with connecting lines)
// and an rcap for the confidence intervals
twoway (sc coef t if foreign == 1, mcolor(navy) lcolor(navy) connect(direct)) ///
(rcap ci_low ci_high t if foreign == 1, lcolor(navy)) ///
(sc coef t if foreign == 0, mcolor(maroon) lcolor(maroon) connect(direct)) ///
(rcap ci_low ci_high t if foreign == 0, lcolor(maroon)), ///
legend(lab(1 "Foreign") lab(2 "Foreign CI") lab(3 "Domestic") lab(4 "Domestic CI")) ///
xlab(,val)
人们可能希望改进的一些方法是:
通过标签定义,可以对比这更长的时间序列使用 for 循环,因此并非全部由手工完成
我不是 statsby 方面的专家,所以也许有更简单的方法来获取置信区间并省去主干、重量和常量
至于残差,这个答案的基本直觉是您需要一个包含系数和置信区间的数据集。因此,如果您可以计算残差值及其 CI 并将它们放入数据集中,那么您就可以使用相同类型的双向图。
我正在尝试在 Stata 中为事件研究编写代码,但我无法完全得到我想要的。 Jacobson、LaLonde 和 Sullivan (1993),第 698 页图 3 (http://www.princeton.edu/~davidlee/wp/0.pdf),有一个与我想要的非常相似的图,只是我还想添加置信区间。
基于本教程,http://www.stata.com/meeting/germany14/abstracts/materials/de14_jann.pdf,我编写了以下代码:
sysuse auto, clear
egen t = fill(1,2,3,4,1,2,3,4)
quietly regress price ib2.t trunk weight if foreign==0
estimates store domestic
quietly regress price ib2.t trunk weight if foreign==1
estimates store foreign
coefplot (domestic, label(Domestic Cars)) (foreign, label(Foreign Cars)), drop(_cons) xline(0) vertical omitted baselevels
这产生了我想要的东西,但存在以下问题:
- 点估计和置信区间是并排的,而不是彼此重叠的(如果这是唯一的问题,这可能没问题)。
- 我的时间变量 t 出现在每个 x 标签(t=1、t=2 等)中,但我只想说(1、2 等)没有 t =.
- 在这个玩具示例中,我必须从 1 开始我的 t 编号,因为与
i
运算符组合的因子变量必须是非负数。我想让我的时间变量能够取负数。 - 我不想让
trunk
和weight
出现在剧情中。将它们放在drop(...)
中可以吗? - 我还希望能够在回归残差中完成所有这些操作,而不是上面的操作。
- 我想用线连接点估计。
我一点也不嫁给coefplot
命令。其他技术,尤其是使用内置 Stata 命令也完全可以接受。
希望我已经正确回答了你的问题,也许我误解了什么,但这是我的回答:
(我没有解决 5,因为我不确定你到底在寻找那个问题,但也许在看到我的解决方案之后就会清楚)
代码:
// load data same as before
sysuse auto, clear
egen t = fill(1,2,3,4,1,2,3,4)
// get coefficients and standard errors of regressions over foreign
statsby _b _se , clear by(foreign): regress price ib2.t trunk weight
// there are some extra variables we don't need/want
drop *_trunk *_weight *_cons
// generate confidence intervals and rename coefficient variables
forvalues i = 1/4 {
local j = `i'+7
gen ci_low`i' = _stat_`i' - 1.96*_stat_`j'
gen ci_high`i' = _stat_`i' + 1.96*_stat_`j'
rename _stat_`i' coef`i'
}
// no longer in need of standard error variables
drop _stat_8 _stat_9 _stat_10 _stat_11
// now, we want our data in long format so we can do a twoway graph
reshape long coef ci_low ci_high, i(foreign) j(t)
// we can label the t values so that they start below 1
lab def timeseries 1 "-1" 2 "0" 3 "1" 4 "2"
lab values t timeseries
// now graph, note each factor has two pieces, a scatter (with connecting lines)
// and an rcap for the confidence intervals
twoway (sc coef t if foreign == 1, mcolor(navy) lcolor(navy) connect(direct)) ///
(rcap ci_low ci_high t if foreign == 1, lcolor(navy)) ///
(sc coef t if foreign == 0, mcolor(maroon) lcolor(maroon) connect(direct)) ///
(rcap ci_low ci_high t if foreign == 0, lcolor(maroon)), ///
legend(lab(1 "Foreign") lab(2 "Foreign CI") lab(3 "Domestic") lab(4 "Domestic CI")) ///
xlab(,val)
人们可能希望改进的一些方法是:
通过标签定义,可以对比这更长的时间序列使用 for 循环,因此并非全部由手工完成
我不是 statsby 方面的专家,所以也许有更简单的方法来获取置信区间并省去主干、重量和常量
至于残差,这个答案的基本直觉是您需要一个包含系数和置信区间的数据集。因此,如果您可以计算残差值及其 CI 并将它们放入数据集中,那么您就可以使用相同类型的双向图。