Announcement

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

  • Stata extending the graph axis from 2022m1-2023m12 to 1960m1-2023m12

    I run this code. My data goes from 2022m1 to 2023m12. But when I make the graph, the period is extended all the way up to 1960m1. I don't know how to work around this. Any suggestions would be helpful.



    gen treat_dist = month-tm(2022m12)

    gen shifter = treat_dist + 12

    summ shifter if treat_dist == -1

    local true_neg = r(mean)

    disp `true_neg'




    drop if month < tm(2022m1)



    summ shifter if treat_dist == -1

    local true_neg = r(mean)

    disp `true_neg'




    reghdfe dv post ib`true_neg'.shifter##c.treatment_cont if Developing==1 & cat==1, abs(group month) cluster(group)










    gen coef = .

    gen se = .




    levelsof shifter, l(times)

    foreach t in `times' {

    replace coef = _b[`t'.shifter#avg_`sco'] if shifter == `t'

    replace se = _se[`t'.shifter#avg_`sco'] if shifter == `t'

    }

    disp `times'







    * Make confidence intervals

    cap g ci_top = coef+1.96*se

    cap g ci_bottom = coef - 1.96*se




    keep month coef se ci_*

    duplicates drop

    sort month




    summ ci_top

    local top_range = r(max)

    summ ci_bottom

    local bottom_range = r(min)



    twoway (sc coef month) ///

    (rcap ci_top ci_bottom month) ///

    (function y = 0, range(month)) ///

    (function y = -1, range(`bottom_range' `top_range') horiz) , ///

    legend(off)









    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(month coef se ci_top ci_bottom)
    744   -.04560159 .04456954    .0417547 -.13295789
    745   .035214104 .04619185   .12575014 -.05532193
    746   -.05389897 .04303712  .030453784 -.13825172
    747  -.014382817 .04082957   .06564314 -.09440877
    748   -.02137619 .04835052  .073390834 -.11614322
    749   -.06010559 .04499692   .02808838 -.14829956
    750   -.05081084  .0416654  .030853353 -.13247503
    751   .006636559   .040455   .08592836 -.07265524
    752   -.04418491 .04020809  .034622956 -.12299277
    753   -.05142649 .04109575   .02912118 -.13197416
    754            0         0           0          0
    755   -.02560237 .04114302   .05503794 -.10624268
    756 -.0001877204 .04576408   .08950988 -.08988532
    757   -.01596871 .04283699    .0679918 -.09992922
    758   -.04425317 .04183565   .03774472 -.12625106
    759    -.0325988 .04125999   .04827078 -.11346838
    760   -.08916704 .04567079 .0003477098 -.17868178
    761   -.13920917 .04288281  -.05515886  -.2232595
    762   -.06880311 .04861903  .026490185  -.1640964
    763   -.13813807 .04564967  -.04866473 -.22761142
    764   -.20903113 .04899104   -.1130087  -.3050536
    765    -.1458779 .05587654 -.036359884 -.25539592
    766   -.17964283 .04843017  -.08471969 -.27456596
    767   -.08972433 .04444844 -.002605384  -.1768433
    end
    format %tm month

  • #2
    The problem arises from -(function y = -1, range(`bottom_range' `top_range') horiz)-. The local macros `bottom_range' and `top_range' were defined as the minimum and maximum values of ci_top. If you look at those, are -.113 and +.126, respectively (rounded to 3 places). So 0 is included in that. But 0, as a month, corresponds to January 1960 in Stata's date system. So the axis is extended to accommodate it.

    It seems to me that what you actually want, in any case, is a horizontal line at y = -1, just as you want a horizontal line at y = 0. The use of -function- is not necessary for this. I think
    the -yline()- option is a better way to do that. That said, you won't get a horizontal line at -1 because the y-values in your data don't go down that far, the lowest being -.305. This leaves me to suggest
    Code:
    twoway (sc coef month) ///
    (rcap ci_top ci_bottom month) , ///
    yline(0) legend(off)
    as giving you the graph you want (or as close to it as is possible.) If you still want a second horizontal line in negative territory, you will need to pick a value that can be found on the graph. If it were, say, -0.1, you can add another -yline()- option with that value to the command.

    Comment


    • #3
      I get what you are saying. And I tried your code. But I got the error "yline(0) is not a twoway plot type"

      This works for me but of course without the vertical line:

      summ ci_top
      local top_range = r(max)
      summ ci_bottom
      local bottom_range = r(min)

      twoway (sc coef review_time_month) ///
      (rcap ci_top ci_bottom review_time_month) ///
      (function y = 0, range(review_time_month)), ///
      legend(off)

      Comment


      • #4
        Did you put the -yline(0)- after the comma, where it must be? I think not.

        Comment


        • #5
          OH! I hadn't. I used xline(754) and yline(0) just now and it's giving me the graph I want! Thank you so much!

          Comment

          Working...
          X