Announcement

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

  • Aggregating monhly time series data into a yearly time series, and calculating its Growth Rates.

    Hello,

    I was modifying the data in another thread, but the issues below are different from the original question, so I'm posting this in a new thread.
    I have a time series trend for Hosuing Prices interest over time from the states of USA. And I have three different issues, the first two being very related, and the third.

    So from the data i have below, which is monthly:
    a) is it possible to tell stata to aggregate all the monthly data into a yearly data Variable? And is it possible to save this in another file? In the end I need two seperate files, one with the monthly data per state and the other one with the yearly data per state.

    b) How can I tell stata to calculate monthly (and yearly) growth rates of each state into a new variable in a time series? With panel data I’ve been able to do it using the bysort command, but I don’t see how to do it with a time series.

    c) When graphing I do two different types of graphs:
    graph twoway line Ohio month
    twoway (tsrline gdp officials, lcolor(red) lpattern(solid) connect(direct))
    The first graph, for an individual time series is fine. However, the second line of code, once I proceed to graph it, doesn’t allow me to edit it using the graph editor, to chose two different colors for the two different states. How could I create a time series graph over time for two different states with two different colors?

    The code below:
    The first variable is a monthly variable that starts January 2004. In the code I have it's converted into a year monthly structure, so it should be the same.
    The observations in the states, are monthly interest of Housing Prices in google trends.

    input float month byte(Ohio Utah Texas Vermont NorthDakota)
    528 72 0 26 0 0
    529 35 0 25 0 76
    530 90 0 100 0 100
    531 68 0 24 0 0
    532 31 95 11 0 70
    533 100 0 22 0 0
    534 67 98 52 100 0
    535 30 0 26 0 0
    536 42 0 68 0 0
    537 28 83 31 0 0
    538 0 86 33 0 0
    539 27 0 20 64 0
    540 69 0 17 59 0
    541 47 70 49 0 0
    542 52 60 31 0 44
    543 23 68 13 0 95
    544 20 0 38 53 0
    545 46 63 31 0 0
    546 33 63 26 0 0
    547 62 58 21 56 0
    548 0 52 7 0 41
    549 35 96 31 0 0
    550 35 0 10 0 0
    551 85 47 15 0 0
    552 30 0 19 0 0
    553 30 43 22 0 0
    554 67 72 25 0 0
    555 51 40 57 37 0
    556 80 0 51 37 0
    557 15 83 23 41 0
    558 43 60 39 0 0
    559 52 36 29 0 0
    560 32 87 20 0 0
    561 46 47 29 33 0
    562 34 31 23 0 0
    563 23 31 28 0 0
    564 29 68 24 41 0
    565 30 85 17 43 0
    566 56 0 34 0 0
    567 38 52 30 0 20
    568 31 51 28 38 0
    569 34 27 33 0 0
    570 14 39 28 0 0
    571 35 25 23 41 0
    572 22 24 37 37 36
    573 43 43 37 0 16
    574 23 22 24 34 0
    575 49 34 22 0 0
    end
    format %tm month
    [/CODE]

    Thank you!

  • #2
    Regarding a), your question is unclear. If you want to make yearly data out of this, what statistic do you want to use to summarize the months of each year. Should it be the average price for all months of the year? Or the highest? The lowest? The median? The last? What? Once you figure that out, all you need to do is create a year variable and then collapse.
    Code:
    gen year = year(dofm(month))
    collapse (whatever) Ohio-NorthDakota, by(year)
    Regarding b), the answer is that you should convert this to panel data.

    Code:
    rename (Ohio-NorthDakota) price=
    reshape long price, i(month) j(state) string
    Different people have different formulas for calculating growth rates, so all I'll say from here is that whatever you have previously done with -bysort-, do it here.

    Code:
    bysort state month: whatever
    If you need to go back to the wide layout for some reason -reshape wide- will take you there. But it is likely that whatever you are going to do next will also be easier if you keep this panel data layout. The wide layout of the data is only useful in a few circumstances in Stata.

    By the way, -bysort- is a prefix, not a command.

    Regarding c),from reading the manual section on it, it appears that -tsrline- is specifically designed to produce the upper and lower line limits in the same color so that in plots with multiple series, it is clear which upper line goes with which lower line, and also to distinguish them from any associated -tsline- line. In fact, I have the impression that the whole reason for even having a -tsrline- command is to enforce the same color on the upper and lower limit lines: in other -twoway line- type graphs, having two lines with the same color wouldn't even be allowed until you had so many different lines that the pallette was exhausted and had to recycle. So I think you may just have to use plain old -graph twoway line- for the upper and lower limit variables to get different colors.
    Last edited by Clyde Schechter; 11 Nov 2018, 10:45.

    Comment


    • #3
      Clyde,

      From question a)

      I want to create a yearly variable, for all the years in order to be able to compare the interest in housing prices, not per month but per year. I thought that perhaps an a simple average is the most conveniant way ( summation of the 12 months / number o months) for a quick analysis.

      b) I thought as much !

      Regarding c) I just found nick cox's comment on graphing with tsrline, and I see that it might have a different use than my most inmediate ones. However, Later on, I believe what cox's second comment on this thread https://www.statalist.org/forums/for...trend-in-stata will be very useful to compare trends between states.

      Thank you as always for your help in everything Clyde, I appreciate very much!

      Falco

      Comment


      • #4
        I want to create a yearly variable, for all the years in order to be able to compare the interest in housing prices, not per month but per year. I thought that perhaps an a simple average is the most conveniant way ( summation of the 12 months / number o months) for a quick analysis.
        Well, if your focus is on the growth in housing prices, using the mean will give you some puzzling results. In particular, the annual growth of the mean will not be equal to the compounded effect of the monthly growth over 12 monthly periods. In order to get that kind of consistency, you would have to use either the first or the last month's price as the price for the year.

        Comment


        • #5
          Clyde,

          The observations on are a the google trends measure for housing prices. That is, how popular "Housing Prices" are in a particular state for a given month. That's why I thought using the average would work.

          However reagarding a monthly growth rate, from the last post to this one, for all my data I did:

          rename (Alabama-Wyoming) interest=
          reshape long interest, i(month) j(state) string
          sort state month
          egen statepanel = group(state)
          xtset statepanel month

          bysort state month: gen growth=(interest[_n]-interest[_n-1])/interest[_n-1]

          And the data I have produced is the one underneath.

          Is it necessary (as I did) to create an xtset?
          And what other way's are there of calculating the growth rates? Or the changes from month to month percentually?
          And why does stata produce som missing values with the formula?



          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float month str18 state byte interest float(statepanel growth)
          528 "Alabama"  77 1           .
          529 "Alabama"  74 1  -.03896104
          530 "Alabama" 100 1   .35135135
          531 "Alabama"   0 1          -1
          532 "Alabama"   0 1           .
          533 "Alabama"   0 1           .
          534 "Alabama"  68 1           .
          535 "Alabama"   0 1          -1
          536 "Alabama"   0 1           .
          537 "Alabama"  61 1           .
          538 "Alabama"  63 1  .032786883
          539 "Alabama"   0 1          -1
          540 "Alabama"   0 1           .
          541 "Alabama"   0 1           .
          542 "Alabama"  45 1           .
          543 "Alabama"  50 1   .11111111
          544 "Alabama"   0 1          -1
          545 "Alabama"   0 1           .
          546 "Alabama"   0 1           .
          547 "Alabama"  85 1           .
          548 "Alabama"  39 1   -.5411765
          549 "Alabama"  74 1    .8974359
          550 "Alabama"  37 1         -.5
          551 "Alabama"  54 1   .45945945
          552 "Alabama"   0 1          -1
          553 "Alabama"   0 1           .
          554 "Alabama"   0 1           .
          555 "Alabama"  31 1           .
          556 "Alabama"  29 1  -.06451613
          557 "Alabama"  31 1   .06896552
          558 "Alabama"  44 1    .4193548
          559 "Alabama"  26 1   -.4090909
          560 "Alabama"  26 1           0
          561 "Alabama"  25 1  -.03846154
          562 "Alabama"  25 1           0
          563 "Alabama"   0 1          -1
          564 "Alabama"  21 1           .
          565 "Alabama"   0 1          -1
          566 "Alabama"  20 1           .
          567 "Alabama"  40 1           1
          568 "Alabama"  19 1       -.525
          569 "Alabama"  20 1   .05263158
          570 "Alabama"  18 1         -.1
          571 "Alabama"  17 1  -.05555556
          572 "Alabama"  53 1   2.1176472
          573 "Alabama"  16 1   -.6981132
          574 "Alabama"  33 1      1.0625
          575 "Alabama"  44 1    .3333333
          576 "Alabama"  15 1   -.6590909
          577 "Alabama"  32 1   1.1333333
          578 "Alabama"  31 1     -.03125
          579 "Alabama"  30 1 -.032258064
          580 "Alabama"  15 1         -.5
          581 "Alabama"  32 1   1.1333333
          582 "Alabama"  23 1     -.28125
          583 "Alabama"  15 1   -.3478261
          584 "Alabama"  14 1  -.06666667
          585 "Alabama"  19 1    .3571429
          586 "Alabama"  21 1   .10526316
          587 "Alabama"  14 1   -.3333333
          588 "Alabama"  39 1   1.7857143
          589 "Alabama"  14 1   -.6410257
          590 "Alabama"  57 1   3.0714285
          591 "Alabama"  19 1   -.6666667
          592 "Alabama"  33 1    .7368421
          593 "Alabama"  41 1   .24242425
          594 "Alabama"  26 1   -.3658537
          595 "Alabama"  50 1    .9230769
          596 "Alabama"   0 1          -1
          597 "Alabama"  52 1           .
          598 "Alabama"  30 1   -.4230769
          599 "Alabama"  12 1         -.6
          600 "Alabama"  22 1    .8333333
          601 "Alabama"  34 1   .54545456
          602 "Alabama"  33 1 -.029411765
          603 "Alabama"  23 1   -.3030303
          604 "Alabama"  12 1   -.4782609
          605 "Alabama"  25 1   1.0833334
          606 "Alabama"  30 1          .2
          607 "Alabama"  28 1  -.06666667
          608 "Alabama"  11 1   -.6071429
          609 "Alabama"  38 1   2.4545455
          610 "Alabama"  27 1  -.28947368
          611 "Alabama"  34 1   .25925925
          612 "Alabama"  17 1         -.5
          613 "Alabama"  24 1    .4117647
          614 "Alabama"  17 1  -.29166666
          615 "Alabama"  41 1   1.4117647
          616 "Alabama"  21 1   -.4878049
          617 "Alabama"  24 1   .14285715
          618 "Alabama"  15 1       -.375
          619 "Alabama"  34 1   1.2666667
          620 "Alabama"  19 1   -.4411765
          621 "Alabama"  37 1    .9473684
          622 "Alabama"  16 1   -.5675676
          623 "Alabama"  18 1        .125
          624 "Alabama"  30 1    .6666667
          625 "Alabama"  19 1   -.3666667
          626 "Alabama"  27 1    .4210526
          627 "Alabama"  32 1    .1851852
          end
          format %tm month

          Comment


          • #6
            If you are 100% certain that there are no time gaps in the data, you do not have to use -xtset- and can proceed in the way you show. The reason I recommend using -xtset- is that if there is a gap in your data, then your formula (interest[_n]-interest[_n-1])/interest[_n-1], will be calculating "growth" incorrectly using a baseline value of interest[_n-1] that is not actually from the preceding month but from some earlier month. If you use -xtset- and gen growth = (interest - L1.interest)/L1.interest, Stata will check for that problem and will not make that mistake. The lag operator is "smart" in that sense. If there are no gaps in the time series, then you will get the same results either way.

            As for why you get some missing values, there are two separate phenomena. For the very first observation in each state, there is no previous value of interest to use in the formula, hence a missing result. The other missing values for growth all arise when the preceding month's value of interest is 0: you can't divide by 0.

            And what other way's are there of calculating the growth rates? Or the changes from month to month percentually?
            Well, I have seen a few things used. One is (interest[_n+1]-interest)/interest. This will produce the same results as yours, except that everything will be displaced by one month from where you have them. So this is a "prospective" growth, as opposed to your formula for "retrospective" growth.

            Another formula is (interest/interest[_n-1] - 1). Algebraically this is equivalent to your formula. There are some far-fetched circumstances under which floating-point calculations could lead to different results with the two (with your formula getting it wrong and this one getting it right), but as a practical matter those situations don't arise with real-world financial phenomena.

            Another formula I have seen is ln(interest)-ln(interest[_n-1]). This one has never made any sense to me. It is a formula that is only approximately correct, and the approximation starts getting bad at growth rates of 0.1 and keeps getting worse with larger growth rates. So it is only usable when you are sure the growth rates are low. Even though it is a passable approximation in that setting, I still don't understand why anybody uses it since it is by no means a "short cut" calculation: it is far more computationally intensive than your formula. So why go to all the extra trouble to get a lousy approximation when it's so easy to get the exact result? Nevertheless, you will find this formula in publications, and I've seen it used in posts here on Statalist as well.

            By the way, both of the last two formulas can also be done "prospectively."

            If you want the results in percents, just multiply your results by 100.
            Last edited by Clyde Schechter; 13 Nov 2018, 08:06.

            Comment


            • #7
              I went back to time series data, since the people for whom I am doing the data want to see this in time series format.

              I tried to create a change for the month of Alabama:
              generate changeAlabama = 100*(Alabama[_n] - Alabama[_n-1]) / Alabama[_n-1]

              Why would you consider that changing it to panel data is better to calculate growth changes?

              Also, is if there is a better way to calculate growth, do try it out, but having read the different types and for what I need (and have been told to do) I believe this is the correct form. However, when I then graph it with: twoway line changeAlabama month the changes are a bit hard to read. Is there a better way to do it?

              I also was trying to create a loop to be able to do this with the rest of the states, but I'm not sure how.


              I created a loop for graphing:

              foreach var of local varlist {
              gen = trendchange
              graph export `var'_By_Year.png, as(png) replace
              }

              foreach var of local varlist {
              twoway (line `var' month), title(`var' by Monthly)
              graph export `var'_By_Year.png, as(png) replace
              }


              But for growth rates, taking states as variable and putting in the equation for growth rates is not possible?
              Any help with this would be really helpful.

              Here is a sample code:

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input float month byte(Alabama Utah Wyoming Texas) float changeAlabama
              528  77  0   0  26         .
              529  74  0   0  25 -3.896104
              530 100  0   0 100 35.135136
              531   0  0   0  24      -100
              532   0 95   0  11         .
              533   0  0   0  22         .
              534  68 98   0  52         .
              535   0  0   0  26      -100
              536   0  0   0  68         .
              537  61 83 100  31         .
              538  63 86   0  33 3.2786884
              539   0  0   0  20      -100
              540   0  0   0  17         .
              541   0 70   0  49         .
              542  45 60   0  31         .
              543  50 68   0  13  11.11111
              544   0  0   0  38      -100
              545   0 63  78  31         .
              546   0 63   0  26         .
              547  85 58   0  21         .
              548  39 52  63   7 -54.11765
              549  74 96   0  31  89.74359
              550  37  0   0  10       -50
              551  54 47   0  15  45.94595
              552   0  0   0  19      -100
              553   0 43  52  22         .
              554   0 72  47  25         .
              555  31 40   0  57         .
              556  29  0   0  51 -6.451613
              557  31 83   0  23  6.896552
              558  44 60   0  39  41.93548
              559  26 36   0  29 -40.90909
              560  26 87  45  20         0
              561  25 47   0  29 -3.846154
              562  25 31   0  23         0
              563   0 31  40  28      -100
              564  21 68   0  24         .
              565   0 85  36  17      -100
              566  20  0   0  34         .
              567  40 52   0  30       100
              568  19 51  32  28     -52.5
              569  20 27  36  33  5.263158
              570  18 39   0  28       -10
              571  17 25   0  23 -5.555555
              572  53 24   0  37  211.7647
              573  16 43   0  37 -69.81132
              574  33 22  28  24    106.25
              575  44 34  28  22 33.333332
              end
              format %tm month

              Comment


              • #8
                You can do it in wide layout, but it's much less efficient, and it will just make it harder for you to do other analyses with the results, since most Stata commands work best (or only) with long layout data. If the reason for going wide is to create a display of the states data side-by-side for human eyes to look at, then you can do it in long layout (as per #2) and then at the end you can -reshape wide- to create the visual display:

                Code:
                reshape wide interest growth, i(month) j(state) string
                I think this is better than writing messy loops over states to do the calculation in wide layout at the start. Also it is best to get in the habit of working with data in long layout in Stata.


                To clarify a different point, I wasn't advocating that you use one of the other formulas for growth. In your original post you did not specify which formula you wanted to use, and rather than pick one for you, I chose to simply point out that there are alternatives and leave it to you to pick the one you want. You then asked what the alternatives were, so I listed some, but that was just informational. By all means stick with the one you're using if that's the one that suits your needs and you like it. Really the only change in what you're doing that I would push you to adopt is using -xtset- and the lag operator rather than using the [_n-1] approach. The reason is that you really can never trust a data set to be complete, and even if you think it is and the data comes from usually reliable sources, data sets often can surprise and disappoint, and it is better to use calculations that are robust to data errors (or call attention to them) than to use a formula that will just produce incorrect results without warning.

                Comment


                • #9
                  Clyde,

                  I -xtset- my data, and then I wanted to try using the lag operator rather than the [_n-1] approach.

                  I tried using the following line of code: generate growthAlabama = 100*(Alabama[_n] - L1.Alabama[_n-1]) / L1.Alabama[_n-1]

                  However, it gives back that L1.Alabama invalid name, r(198).
                  Why is that and how can I proceed in using the lag operator?


                  Code:
                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input float month byte Alabama float changeAlabama
                  528  77         .
                  529  74 -3.896104
                  530 100 35.135136
                  531   0      -100
                  532   0         .
                  533   0         .
                  534  68         .
                  535   0      -100
                  536   0         .
                  537  61         .
                  538  63 3.2786884
                  539   0      -100
                  540   0         .
                  541   0         .
                  542  45         .
                  543  50  11.11111
                  544   0      -100
                  545   0         .
                  546   0         .
                  547  85         .
                  548  39 -54.11765
                  549  74  89.74359
                  550  37       -50
                  551  54  45.94595
                  552   0      -100
                  553   0         .
                  554   0         .
                  555  31         .
                  556  29 -6.451613
                  557  31  6.896552
                  558  44  41.93548
                  559  26 -40.90909
                  560  26         0
                  561  25 -3.846154
                  562  25         0
                  563   0      -100
                  564  21         .
                  565   0      -100
                  566  20         .
                  567  40       100
                  568  19     -52.5
                  569  20  5.263158
                  570  18       -10
                  571  17 -5.555555
                  572  53  211.7647
                  573  16 -69.81132
                  574  33    106.25
                  575  44 33.333332
                  end
                  format %tm month

                  Comment


                  • #10
                    You cannot use the L1 operator in combination with [_n-1] subscripting. That is not valid syntax. You can use either one separately (and if there are no gaps in the time series, they will give you the same results). Try this:

                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input float month byte Alabama float changeAlabama
                    528  77         .
                    529  74 -3.896104
                    530 100 35.135136
                    531   0      -100
                    532   0         .
                    533   0         .
                    534  68         .
                    535   0      -100
                    536   0         .
                    537  61         .
                    538  63 3.2786884
                    539   0      -100
                    540   0         .
                    541   0         .
                    542  45         .
                    543  50  11.11111
                    544   0      -100
                    545   0         .
                    546   0         .
                    547  85         .
                    548  39 -54.11765
                    549  74  89.74359
                    550  37       -50
                    551  54  45.94595
                    552   0      -100
                    553   0         .
                    554   0         .
                    555  31         .
                    556  29 -6.451613
                    557  31  6.896552
                    558  44  41.93548
                    559  26 -40.90909
                    560  26         0
                    561  25 -3.846154
                    562  25         0
                    563   0      -100
                    564  21         .
                    565   0      -100
                    566  20         .
                    567  40       100
                    568  19     -52.5
                    569  20  5.263158
                    570  18       -10
                    571  17 -5.555555
                    572  53  211.7647
                    573  16 -69.81132
                    574  33    106.25
                    575  44 33.333332
                    end
                    format %tm month
                    
                    tsset month // THIS IS CRITICAL
                    
                    generate growthAlabama1 = 100*(Alabama[_n] - Alabama[_n-1]) / Alabama[_n-1]
                    generate growthAlabama2 = 100*(Alabama[_n] - L1.Alabama) / L1.Alabama
                    assert growthAlabama1 == growthAlabama2
                    generate growthAlabama3 = 100*(Alabama[_n] - L1.Alabama[_n-1]) / L1.Alabama[_n-1]
                    You will see that the first two work fine, and give identical results. The third, which is yours from above, generates an error.

                    Comment


                    • #11
                      Thanks Clyde,

                      I finally will give the two different type of data. I finally did a panel:

                      Using the commands for growth, and yes they yield the same result and a different result from the the ones I was using before.
                      bysort statepanel (year): gen growth3 = 100*(price[_n] - price[_n-1]) / price[_n-1]
                      bysort statepanel (year): gen growth2= 100*(price[_n] - L1.price) / L1.price

                      The other way I believe it will be a headache for anyone to go through all the resulting data, as you mentioned.

                      I have two more questions:

                      1- The first one is how could I create twoway graphs in panel data for the different states?
                      2- How can I use: xtline price, overlay but taking into account just a few states in a panel?

                      Thanks for all your help

                      Code:
                      * Example generated by -dataex-. To install: ssc install dataex
                      clear
                      input float year str18 state float(price statepanel growth3 growth2)
                      2004 "Alabama"  36.916668 1          .          .
                      2005 "Alabama"         32 1 -13.318288 -13.318288
                      2006 "Alabama"      19.75 1  -38.28125  -38.28125
                      2007 "Alabama"  25.083334 1  27.004223  27.004223
                      2008 "Alabama"      21.75 1  -13.28904  -13.28904
                      2009 "Alabama"  31.083334 1   42.91188   42.91188
                      2010 "Alabama"  26.416666 1  -15.01341  -15.01341
                      2011 "Alabama"  23.583334 1 -10.725548 -10.725548
                      2012 "Alabama"  22.333334 1  -5.300353  -5.300353
                      2013 "Alabama"  22.916666 1  2.6119344  2.6119344
                      2014 "Alabama"      24.75 1   8.000003   8.000003
                      2015 "Alabama"         28 1  13.131313  13.131313
                      2016 "Alabama"      36.75 1      31.25      31.25
                      2017 "Alabama"  33.583332 1  -8.616783  -8.616783
                      2018 "Alabama"  34.636364 1   3.135579   3.135579
                      2004 "Alaska"   23.083334 2          .          .
                      2005 "Alaska"   24.833334 2   7.581227   7.581227
                      2006 "Alaska"   13.833333 2   -44.2953   -44.2953
                      2007 "Alaska"       16.25 2  17.469883  17.469883
                      2008 "Alaska"   12.333333 2 -24.102566 -24.102566
                      2009 "Alaska"   20.583334 2    66.8919    66.8919
                      2010 "Alaska"       15.75 2 -23.481783 -23.481783
                      2011 "Alaska"       14.25 2  -9.523809  -9.523809
                      2012 "Alaska"   17.083334 2  19.883045  19.883045
                      2013 "Alaska"   10.083333 2  -40.97561  -40.97561
                      2014 "Alaska"   13.166667 2   30.57852   30.57852
                      2015 "Alaska"        11.5 2  -12.65823  -12.65823
                      2016 "Alaska"        12.5 2   8.695652   8.695652
                      2017 "Alaska"    9.833333 2 -21.333336 -21.333336
                      2018 "Alaska"   12.181818 2    23.8829    23.8829
                      2004 "Arizona"         50 3          .          .
                      2005 "Arizona"  30.166666 3  -39.66667  -39.66667
                      2006 "Arizona"  38.083332 3   26.24309   26.24309
                      2007 "Arizona"  32.083332 3 -15.754924 -15.754924
                      2008 "Arizona"      27.25 3 -15.064932 -15.064932
                      2009 "Arizona"  27.166666 3  -.3058127  -.3058127
                      2010 "Arizona"  23.416666 3  -13.80368  -13.80368
                      2011 "Arizona"      21.75 3  -7.117435  -7.117435
                      2012 "Arizona"  20.916666 3  -3.831421  -3.831421
                      2013 "Arizona"  23.083334 3  10.358572  10.358572
                      2014 "Arizona"  19.583334 3 -15.162455 -15.162455
                      2015 "Arizona"         24 3   22.55319   22.55319
                      2016 "Arizona"  24.666666 3   2.777775   2.777775
                      2017 "Arizona"  24.416666 3 -1.0135136 -1.0135136
                      2018 "Arizona"   25.27273 3   3.506056   3.506056
                      2004 "Arkansas" 18.666666 4          .          .
                      2005 "Arkansas" 11.333333 4  -39.28571  -39.28571
                      2006 "Arkansas" 15.583333 4       37.5       37.5
                      2007 "Arkansas"     15.25 4 -2.1390355 -2.1390355
                      2008 "Arkansas" 12.916667 4 -15.300545 -15.300545
                      2009 "Arkansas" 11.666667 4  -9.677419  -9.677419
                      2010 "Arkansas"        10 4 -14.285717 -14.285717
                      2011 "Arkansas"      8.75 4      -12.5      -12.5
                      2012 "Arkansas"      9.25 4   5.714286   5.714286
                      2013 "Arkansas"  7.583333 4 -18.018017 -18.018017
                      2014 "Arkansas"  8.916667 4  17.582418  17.582418
                      2015 "Arkansas"      7.75 4 -13.084115 -13.084115
                      2016 "Arkansas"       8.5 4    9.67742    9.67742
                      2017 "Arkansas" 11.916667 4   40.19608   40.19608
                      2018 "Arkansas"  9.363636 4 -21.424036 -21.424036
                      end

                      Comment


                      • #12
                        Code:
                        * Example generated by -dataex-. To install: ssc install dataex
                        clear
                        input float year str18 state float(price statepanel growth3 growth2)
                        2004 "Alabama"  36.916668 1          .          .
                        2005 "Alabama"         32 1 -13.318288 -13.318288
                        2006 "Alabama"      19.75 1  -38.28125  -38.28125
                        2007 "Alabama"  25.083334 1  27.004223  27.004223
                        2008 "Alabama"      21.75 1  -13.28904  -13.28904
                        2009 "Alabama"  31.083334 1   42.91188   42.91188
                        2010 "Alabama"  26.416666 1  -15.01341  -15.01341
                        2011 "Alabama"  23.583334 1 -10.725548 -10.725548
                        2012 "Alabama"  22.333334 1  -5.300353  -5.300353
                        2013 "Alabama"  22.916666 1  2.6119344  2.6119344
                        2014 "Alabama"      24.75 1   8.000003   8.000003
                        2015 "Alabama"         28 1  13.131313  13.131313
                        2016 "Alabama"      36.75 1      31.25      31.25
                        2017 "Alabama"  33.583332 1  -8.616783  -8.616783
                        2018 "Alabama"  34.636364 1   3.135579   3.135579
                        2004 "Alaska"   23.083334 2          .          .
                        2005 "Alaska"   24.833334 2   7.581227   7.581227
                        2006 "Alaska"   13.833333 2   -44.2953   -44.2953
                        2007 "Alaska"       16.25 2  17.469883  17.469883
                        2008 "Alaska"   12.333333 2 -24.102566 -24.102566
                        2009 "Alaska"   20.583334 2    66.8919    66.8919
                        2010 "Alaska"       15.75 2 -23.481783 -23.481783
                        2011 "Alaska"       14.25 2  -9.523809  -9.523809
                        2012 "Alaska"   17.083334 2  19.883045  19.883045
                        2013 "Alaska"   10.083333 2  -40.97561  -40.97561
                        2014 "Alaska"   13.166667 2   30.57852   30.57852
                        2015 "Alaska"        11.5 2  -12.65823  -12.65823
                        2016 "Alaska"        12.5 2   8.695652   8.695652
                        2017 "Alaska"    9.833333 2 -21.333336 -21.333336
                        2018 "Alaska"   12.181818 2    23.8829    23.8829
                        2004 "Arizona"         50 3          .          .
                        2005 "Arizona"  30.166666 3  -39.66667  -39.66667
                        2006 "Arizona"  38.083332 3   26.24309   26.24309
                        2007 "Arizona"  32.083332 3 -15.754924 -15.754924
                        2008 "Arizona"      27.25 3 -15.064932 -15.064932
                        2009 "Arizona"  27.166666 3  -.3058127  -.3058127
                        2010 "Arizona"  23.416666 3  -13.80368  -13.80368
                        2011 "Arizona"      21.75 3  -7.117435  -7.117435
                        2012 "Arizona"  20.916666 3  -3.831421  -3.831421
                        2013 "Arizona"  23.083334 3  10.358572  10.358572
                        2014 "Arizona"  19.583334 3 -15.162455 -15.162455
                        2015 "Arizona"         24 3   22.55319   22.55319
                        2016 "Arizona"  24.666666 3   2.777775   2.777775
                        2017 "Arizona"  24.416666 3 -1.0135136 -1.0135136
                        2018 "Arizona"   25.27273 3   3.506056   3.506056
                        2004 "Arkansas" 18.666666 4          .          .
                        2005 "Arkansas" 11.333333 4  -39.28571  -39.28571
                        2006 "Arkansas" 15.583333 4       37.5       37.5
                        2007 "Arkansas"     15.25 4 -2.1390355 -2.1390355
                        2008 "Arkansas" 12.916667 4 -15.300545 -15.300545
                        2009 "Arkansas" 11.666667 4  -9.677419  -9.677419
                        2010 "Arkansas"        10 4 -14.285717 -14.285717
                        2011 "Arkansas"      8.75 4      -12.5      -12.5
                        2012 "Arkansas"      9.25 4   5.714286   5.714286
                        2013 "Arkansas"  7.583333 4 -18.018017 -18.018017
                        2014 "Arkansas"  8.916667 4  17.582418  17.582418
                        2015 "Arkansas"      7.75 4 -13.084115 -13.084115
                        2016 "Arkansas"       8.5 4    9.67742    9.67742
                        2017 "Arkansas" 11.916667 4   40.19608   40.19608
                        2018 "Arkansas"  9.363636 4 -21.424036 -21.424036
                        end
                        
                        encode state, gen(nstate)
                        xtset nstate year
                        
                        graph twoway line growth3 year, sort by(state) name(question1, replace)
                        
                        xtline growth3 if inlist(state, "Alabama", "Arkansas"), overlay name(question2, replace)

                        Comment


                        • #13
                          Whenever I use:

                          xtline growth3 if inlist(state, "Alabama", "Arkansas"), overlay name(question2, replace) It gives me an overlay of all the graphs instead of only Alabama and Arkansas. Is there a way to get an overlay of only two graphs? Is there a way to tell,
                          graph twoway line growth3 year, sort by(state) name(question1, replace) to graph me only three states instead of all the 52 states?

                          Comment


                          • #14
                            Whenever I use:

                            xtline growth3 if inlist(state, "Alabama", "Arkansas"), overlay name(question2, replace) It gives me an overlay of all the graphs instead of only Alabama and Arkansas. Is there a way to get an overlay of only two graphs?
                            I can't reproduce your difficulty here. When I run this command, I get only two overlaid graphs, one for Alabama and the other for Arkansas. Are you sure this is the exact syntax you are using? I can't explain it if you are.

                            Is there a way to tell,
                            graph twoway line growth3 year, sort by(state) name(question1, replace) to graph me only three states instead of all the 52 states?
                            Sure. Just add -if inlist(state, names_of_the_three_states_separated_by_commas_here )-, before the comma.
                            ​​​​​​​

                            Comment

                            Working...
                            X