Announcement

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

  • custom xline value per group in xtline

    Hi. I'm trying to produce a graph for each US state using -xtline- and the -xline- option (Stata 15.1). The first xline is when a policy started and the second is when it stopped. However, I would like the xlines in the code below to vary for each state.

    Code:
    xtline ratio, i(stateid) t(year) xline(2012) xline(2016)
    For instance, the code below sets the first year of the policy. How do I use this in an -xtline- command? Ideally, I'd like all the states I'm exploring to show on one graph.
    Code:
    gen xline1=0
        replace xline1=year if policy==1 & statefip == statefip[_n-1] & policy[_n-1]==0
    bys statefip: egen line1=sum(xline1)
    I tried using a -local- macro in a loop, but that gives only one graph per state. More importantly, the macro gives the wrong value for the xline. In the code below, the macro assigns 2016 for NM, but it should be 2010 (the value in the dataset).
    Code:
    foreach s in NM {
        local line=line 
        di "`line'"
        xtline ratio if statea=="`s'", i(statea) t(year) xline(`line')
        }
    Thanks.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte statefip str2 stateabbr int year float ratio byte policy
     1 "AL" 2008  .008560429 0
     1 "AL" 2010 .0037396036 0
     1 "AL" 2012  .006015119 0
     1 "AL" 2014  .002584282 0
     1 "AL" 2016   .10050032 1
     1 "AL" 2018   .26768586 1
     1 "AL" 2020    .1992347 0
     4 "AZ" 2008   .09503522 0
     4 "AZ" 2010    .1584363 0
     4 "AZ" 2012   .08396793 0
     4 "AZ" 2014   .13108851 0
     4 "AZ" 2016   .15130498 0
     4 "AZ" 2018    .1680617 0
     4 "AZ" 2020   .19041963 1
     6 "CA" 2008   .06721185 0
     6 "CA" 2010    .0256285 0
     6 "CA" 2012   .02907942 0
     6 "CA" 2014   .03441821 0
     6 "CA" 2016    .0264971 0
     6 "CA" 2018   .07148014 1
     6 "CA" 2020    .1101405 1
     8 "CO" 2008     .179553 0
     8 "CO" 2010    .1701319 0
     8 "CO" 2012   .12337877 0
     8 "CO" 2014   .08391105 0
     8 "CO" 2016   .11530598 0
     8 "CO" 2018   .18432954 0
     8 "CO" 2020    .3747691 0
     9 "CT" 2008  .011593563 0
     9 "CT" 2010  .006923317 0
     9 "CT" 2012  .008262033 0
     9 "CT" 2014   .01044251 0
     9 "CT" 2016  .069030836 0
     9 "CT" 2018   .10884544 1
     9 "CT" 2020    .1515628 1
    29 "MO" 2008   .04155307 0
    29 "MO" 2010   .05756563 0
    29 "MO" 2012  .062537424 0
    29 "MO" 2014   .05891614 0
    29 "MO" 2016   .06787449 0
    29 "MO" 2018   .05674638 0
    29 "MO" 2020   .05663429 1
    30 "MT" 2008   .03477622 0
    30 "MT" 2010   .03792036 0
    30 "MT" 2012   .04820985 0
    30 "MT" 2014  .034932867 0
    30 "MT" 2016    .0709369 0
    30 "MT" 2018    .0877433 0
    30 "MT" 2020   .10608228 0
    32 "NV" 2008  .070089065 0
    32 "NV" 2010  .023095006 0
    32 "NV" 2012    .0800713 0
    32 "NV" 2014   .04005749 0
    32 "NV" 2016   .07264246 0
    32 "NV" 2018   .07254117 1
    32 "NV" 2020    .1245699 0
    34 "NJ" 2008   .03385814 0
    34 "NJ" 2010   .02223438 0
    34 "NJ" 2012    .0861322 0
    34 "NJ" 2014           . 0
    34 "NJ" 2016    .1390661 0
    34 "NJ" 2018   .18166585 0
    34 "NJ" 2020   .27149034 0
    35 "NM" 2008 .0020252715 0
    35 "NM" 2010  .002406243 1
    35 "NM" 2012  .017177511 1
    35 "NM" 2014   .02589253 1
    35 "NM" 2016   .03279964 0
    35 "NM" 2018   .06400568 0
    35 "NM" 2020   .11118354 0
    36 "NY" 2008           . 0
    36 "NY" 2010   .05124302 0
    36 "NY" 2012   .05672386 0
    36 "NY" 2014  .036251973 0
    36 "NY" 2016  .069052294 0
    36 "NY" 2018   .10105893 1
    36 "NY" 2020   .09506477 1
    37 "NC" 2008    .1176946 0
    37 "NC" 2010   .07750328 0
    37 "NC" 2012   .09227828 0
    37 "NC" 2014   .07645018 0
    37 "NC" 2016    .1525962 0
    37 "NC" 2018    .1684355 1
    37 "NC" 2020   .21032202 1
    40 "OK" 2008   .08180905 0
    40 "OK" 2010   .04021045 0
    40 "OK" 2012   .06006729 0
    40 "OK" 2014   .03444615 0
    40 "OK" 2016   .09257267 0
    40 "OK" 2018    .0998082 0
    40 "OK" 2020   .11799998 0
    end

  • #2
    To the best of my knowledge, -xtline- cannot do that. You will need to do this from lower-level commands. (If I'm wrong about that, I hope somebody will correct me.)

    Code:
    by stateabbr (year), sort: egen xline1 = min(cond(policy, year, .))
    
    levelsof stateabbr, local(states) clean
    foreach s of local states {
        summ xline1 if stateabbr == `"`s'"', meanonly
        local location = r(mean)
        local second_xline
        if !missing(`location') {
            local second_xline xline(`location')
        }
        graph twoway line ratio year if stateabbr == `"`s'"', sort ///
            xline(2012) `second_xline' name(`"`s'"', replace) title(`"`s'"')
    }
    
    graph combine `states', nocopies

    Comment


    • #3
      I think it may help to shake off the idea of using xline() and consider plotting the information in some other ways. For various trickery see https://www.stata-journal.com/articl...article=gr0067 or https://blog.stata.com/2020/02/13/ad...#disqus_thread


      With the example dataset I go

      Code:
      su ratio, meanonly
      gen max = r(max)
      gen zero = 0
      
      twoway rbar max zero year if policy, barw(2) xtitle("") color(red*0.2) || connected ratio year, by(stateabbr, note("")) legend(order(1 "policy in effect"))

      All sorts of stylistic variations are possible. Here is one: ytitle(something better)
      Click image for larger version

Name:	policyineffect.png
Views:	1
Size:	48.6 KB
ID:	1648275

      Last edited by Nick Cox; 03 Feb 2022, 18:44.

      Comment


      • #4
        Wow. Very nice, Nick. Thanks!

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          To the best of my knowledge, -xtline- cannot do that. You will need to do this from lower-level commands. (If I'm wrong about that, I hope somebody will correct me.)
          This also worked well. Thanks, Clyde.

          Comment

          Working...
          X