Reputation: 2527
I am trying to write code for an event study in Stata, but I can't quite get what I want. Jacobson, LaLonde, and Sullivan (1993), page 698 Figure 3 (http://www.princeton.edu/~davidlee/wp/0.pdf), have a plot that is very similar to what I want, except that I also want to add confidence intervals.
Based on this tutorial, http://www.stata.com/meeting/germany14/abstracts/materials/de14_jann.pdf, I have written the following code:
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
This produces something in the ballpark of what I want, but there are the following problems:
i
operator need to be non-negative. I would like to have my time variable be able to take on negative numbers.trunk
and weight
to appear in the plot. Is it fine to just place these in drop(...)
?I am not at all married to the coefplot
command. Other techniques, especially using built-in Stata commands are also perfectly acceptable.
Upvotes: 3
Views: 14381
Reputation: 887
Hopefully I have answered your question correctly, maybe I have misinterpreted something, but here is my answer:
(I did not solve 5 because I wasn't sure exactly what you were looking for with that question, but maybe after seeing my solution it will be clear)
Code:
// 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)
Some ways that one might wish to improve this are:
with the label defining, one could use a for loop for time series longer than this so it is not all done by hand
I am not an expert on statsby, so maybe there is an easier way to get the confidence intervals and leave out the trunk, weight, and constant
As for the residuals, the basic intuition of this answer is that you want a dataset that includes the coefficients and confidence intervals. So if you can calculate the values for the residuals and their CI and put those in a dataset, then you can use the same type of twoway graph.
Upvotes: 6