Stata Event Study Graph Code

11,859

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)

output:

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.

Share:
11,859

Related videos on Youtube

bill999
Author by

bill999

Updated on June 04, 2022

Comments

  • bill999
    bill999 about 2 years

    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:

    1. The point estimates and confidence intervals are side-by-side as opposed to on top of each other (which may be fine if this were the only issue).
    2. My time variable, t, appears in each of the x-labels (t=1, t=2, etc.), but I just want it to say (1, 2, etc.) without the t=.
    3. I had to start my t numbering at 1 in this toy example because the factor variables combined with the i operator need to be non-negative. I would like to have my time variable be able to take on negative numbers.
    4. I don't want trunk and weight to appear in the plot. Is it fine to just place these in drop(...)?
    5. I would also like to be able to do all this in regression residuals as opposed to what I have above.
    6. I would like to connect the point estimates with lines.

    I am not at all married to the coefplot command. Other techniques, especially using built-in Stata commands are also perfectly acceptable.