Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • xline shall only cover a certain range in event plot

    Dear community,

    I recently started exploring the fascinating package did_imputation by Borusyak et al. (2021) for a diff-in-diff imputation design (here is a reference). Now, I want to add a horizontal line to my event plot that only starts at the treatment time. Unfortunately, I haven't found a way to implement this, yet. I checked yline but this covers the whole range of the plot. I also checked addplot but this does not seem to be allowed when using the event_plot function. I also tried this option using pipes and scatteri but these commands also seem not to be allowed.
    I come from R and I am quite unsure why there isn't an easy option to add a custom line/plot to an existing one. Can someone please help me?

    I am using the example code by Borusyak et al. (2021):

    Code:
    // from https://github.com/borusyak/did_imputation/blob/main/five_estimators_example.do
    
    clear all
    timer clear
    set seed 10
    global T = 15
    global I = 300
    
    set obs `=$I*$T'
    gen i = int((_n-1)/$T )+1                     // unit id
    gen t = mod((_n-1),$T )+1                    // calendar period
    tsset i t
    
    // Randomly generate treatment rollout years uniformly across Ei=10..16 (note that periods t>=16 would not be useful since all units are treated by then)
    gen Ei = ceil(runiform()*7)+$T -6 if t==1    // year when unit is first treated
    bys i (t): replace Ei = Ei[1]
    gen K = t-Ei                                 // "relative time", i.e. the number periods since treated (could be missing if never-treated)
    gen D = K>=0 & Ei!=.                         // treatment indicator
    
    // Generate the outcome with parallel trends and heterogeneous treatment effects
    gen tau = cond(D==1, (t-12.5), 0)             // heterogeneous treatment effects (in this case vary over calendar periods)
    gen eps = rnormal()                            // error term
    gen Y = i + 3*t + tau*D + eps                 // the outcome (FEs play no role since all methods control for them)
    //save five_estimators_data, replace
    
    // Estimation with did_imputation of Borusyak et al. (2021)
    did_imputation Y i t Ei,allhorizons pretrend(5) shift(1) autosample
    ereturn list
    event_plot, default_look graph_opt(xtitle("Periods since the event") ytitle("Average causal effect") ///
        title("Borusyak et al. (2021) imputation estimator") xlabel(-5(1)5))
    
    estimates store bjs // storing the estimates for later
    This is what I get:
    Click image for larger version

Name:	Screenshot 2023-10-29 004811.png
Views:	1
Size:	39.5 KB
ID:	1731893


    And this is what I want to have:

    Click image for larger version

Name:	Screenshot 2023-10-29 004811_with_line.png
Views:	1
Size:	39.4 KB
ID:	1731894


    Thank you in advance!

  • #2
    You may be able to work around this apparent limitation by adding details to the existing option

    Code:
    graph_opt(@@@ || scatteri !!!, recast(line))
    where the @@@ are to be replaced by your existing call and !!! are to be replaced by the coordinates you want.

    Comment


    • #3
      Thank you, Nick! That did the trick! I somehow was confused using the pipes and scatteri and put it at the wrong end of the command.

      This is the code now:
      Code:
      event_plot, default_look graph_opt(xtitle("Periods since the event") ytitle("Average causal effect") title("Borusyak et al. (2021) imputation estimator") xlabel(-5(1)5) || scatteri 1 0 1 6, recast(line) lpattern(dash) lwidth(0.75) lcolor(black))
      And this is the result:
      Click image for larger version

Name:	Screenshot 2023-10-29 142814.png
Views:	1
Size:	45.0 KB
ID:	1731934

      Comment

      Working...
      X