Announcement

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

  • Iteratively Graphing Lines Overlaying Each Other

    I'm doing some placebo testing for a command I've developed. Here's my real data.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year float(relative diff_12 diff_27 diff_16 diff_13 diff_29 diff_23 diff_53 diff_32 diff_52 diff_21 diff_50 diff_35)
    1990 -9  -6.051437   1.988098 -14.028577   .0858941  -3.478288 -1.0756887   -3.94822 -2.8016846   -.4324082  .04457564 -.55204946 -.22909567
    1991 -8  3.3322625   .5506847 -12.236873  1.1179252   -5.92537  -.4865717  -1.760129 -1.1663806    1.280023   .3100856  -.9413556 -.54560375
    1992 -7  2.8650365 -1.5257843  -6.327367 -1.5209053  -3.940223  -1.135563  -6.715644 -4.2100573 -.007741552  .12031516    .656896  -.8922964
    1993 -6    4.25289  -.8487547  -9.155501  -2.145059  1.3797827   .1501558  1.0496625  1.0019472  -.18787855   .1168218  -.4681548   -.714076
    1994 -5  -2.094274  .05727582   8.208552  -.9399192  3.1146345 -1.0425875   .7086133   .7859434    .2254819  -.4653271 -.38832355 -.05880093
    1995 -4   .8204693  1.3913518   8.116424   .0438768    1.23046   .7847378   4.482581 -1.1119622    .2588605   .1849805  -.6451988   .4108817
    1996 -3  -.7121959   .9992239  13.014155   .3504973  4.0575037   .6015267  3.2608175 -.15353885  -.24349357  .04543442  1.0687007    .759743
    1997 -2 -1.8122306  -1.143254   3.882304   .4723949  4.6790657  1.4538778   .5385541  1.2596272    -.238589 -.14569645   .9405996  .04775298
    1998 -1  -.6005207 -1.4688413   8.526881  2.5352955 -1.1175661   .7501129  2.3837652   6.396105   -.6542544  -.2111896   .3288859   1.221495
    1999  0 -12.143868  -2.179568   13.68001  1.4584564 -4.0018454  3.6190116   1.653117 -.03248185    2.779655  -.7936574  -9.150145   6.633817
    2000  1  -2.433019    .761968  2.2873106   .9356676 -1.4637632   2.414775   2.490123 -4.5299172   4.5430307  -1.219032  -7.042178   5.033514
    2001  2 -.56350094   2.034996   6.688071 -2.4138215  1.0830183  1.9068717   1.804408  -6.361621    3.622607  1.7076437  -7.841006   6.318984
    2002  3   3.922118   3.689603   4.837344 -1.3551176  2.2015543  2.2152498  -.3780986  -3.428245    5.217735  1.5421343  -7.435209   2.802115
    2003  4   .6729473  1.6842767   5.320636 .020452386   5.212686   2.818124  4.0406027   -5.70234   2.8474874   4.396373  -8.800531   .6437901
    2004  5 -3.0809226   2.043069    1.07298 -1.1382703   5.817922   1.533093  1.4380864 -11.673812    4.996532   2.802325 -11.323007  -4.567221
    2005  6   -3.14027   4.956491   2.759341   .8993404   9.613034  2.2971523  -3.118226 -12.600416    .7186446   5.380924  -15.33925 -12.507906
    2006  7    .769051  16.608921  2.7640486   3.561272  12.662768  2.0483568  -2.790231  -9.687228   -.3762932   4.184281 -15.271362 -15.240107
    2007  8 -2.8974166   21.73033  -3.346013   3.793796  14.797248      4.027 -1.5663425  -4.483568   -1.467825   5.449445 -17.294516 -19.461214
    2008  9 -2.2475886  17.168425  4.2064047   7.544565  21.931913   4.605433  -.9104053 -3.8688476  -.14387941   7.770937  -19.54023 -21.296967
    2009 10  -.2466331  12.390834  .27635682   9.745594  25.694906   5.353656   3.425185   -3.55805   -1.914415   9.856637 -20.742594  -21.80211
    end
    format %ty year
    What I'm interested in is iteratively graphing these differences on a line graph, like this, where diff_35 is the treated unit and the others a set of placebos.
    Code:
    loc stub = 35
    
    loc placebo_color 178 190 181
    
    rename diff_`stub' treated
    
    order treated, a(year)
    
    lab var relative "Event-Time"
    
    twoway line diff* relative, lcolor("`placebo_color'" ..) lwidth(thin) || ///
             line treated relative, lcol(red) lwidth(thick),, ///
             legend(off) ///
             scheme(black_tableau) ///
             xli(0, lcol(blue) lwidth(thick) lpat(solid)) ///
             yli(0, lcol(blue) lwidth(thick) lpat(solid)) ///
             yti(Gap) //
    So this above approach does exactly what I want. However, I have more than 20 donor units in my real dataset, and Stata doesn't allow for more than 20 variables in this setting. This means what I really have to do is something like
    Code:
    twoway line diff_12-diff_15 relative, lcolor("`placebo_color'" ..) lwidth(thin) || ///
                 line diff_26-diff_17 relative, lcolor("`placebo_color'" ..) || /// Notice this line to graph the rest of the donor units
             line treated relative, lcol(red) lwidth(thick),, ///
             legend(off) ///
             scheme(black_tableau) ///
             xli(0, lcol(blue) lwidth(thick) lpat(solid)) ///
             yli(0, lcol(blue) lwidth(thick) lpat(solid)) ///
             yti(Gap) //
    And this approach is okay I guess, but what if I had 80 donor units? What if I had 300? I know it wouldn't be that big of a deal to make 4 lines of
    Code:
     line diff_26-diff_17 relative, lcolor("`placebo_color'" ..)
    of 20 sets of donor units, but I don't want to do that if I don't have to.

    Is it possible that I could somehow loop over the donor units and have one line of code for the treated unit in a two-way setting?

  • #2
    Code:
    reshape long diff_, i(year) j(group)
    gen byte treated = group == 35
    rename diff_ diff
    sort group year
    twoway line diff relative if !treated,           ///
        connect(L)                                   /// <-- the key option
        lcolor("178 190 181")                        ///
        scheme(black_tableau)                        ///
        lwidth(thin)                                 ///
         ||                                          ///
           line dif relative if treated,             ///
        lcol(red) lwidth(thick) legend(off)          ///         
        xli(0, lcol(blue) lwidth(thick) lpat(solid)) ///
        yli(0, lcol(blue) lwidth(thick) lpat(solid)) ///
        yti(Gap)
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Maarten Buis You're a life-saver, this worked perfectly. Thanks so much.

      Comment

      Working...
      X