Announcement

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

  • graph for evolution variable in panel form

    hello, I have panel data for 171 countries from 1996-2019 , and want to see the "Evolution of average fertility rates over time ", and also declare some specific years as a recession in my graph. the code that I used was this:

    lgraph fertilityrate year, tline(2003 2009 2012 2014 2016)
    and my graph is :
    Click image for larger version

Name:	Graph1.png
Views:	1
Size:	78.2 KB
ID:	1675381


    if it is correct, I am confused about how can I add two other lines to my graph one "for the adavances economies" and another "for emerging market economies" with a different colors. I tried several ways.


    I hope I receive your assistanse so soon.

    Many thanks in advance for your valuable time.


    regards,
    Khatereh


  • #2
    Blog . . . . . . . . . . . . Adding recession shading to time-series graphs
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D. Schenck
    2/20 http://blog.stata.com/2020/02/13/add...ssion-shading-
    to-time-series-graphs/


    SJ-16-3 gr0067 . Speaking Stata: Shading zones on time series and other plots
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
    Q3/16 SJ 16(3):805--812 (no commands)
    discusses background shading of time series and other plots

    Otherwise you need to calculate those averages before you can plot them. EDIT: Wrong. lgraph has a syntax for specifying group mwans.
    Last edited by Nick Cox; 27 Jul 2022, 12:32.

    Comment


    • #3
      -lgraph- is not part of official Stata, and I'm unfamiliar with it. Suffice it to say that what you want can be accomplished with Stata's official -graph twoway connect-. To get multiple curves on the same plot, you need to get the fertility rates for the advanced economies and emerging market economies into separate variables. As I do not know how your data is organized or what you have, I can't advise you how to extract those as separate variables from what you have, but it is likely to involve -reshape-. Once you do that:
      Code:
      graph twoway connect fertilityrate fertility_advanced fertility_emerging year, xline(2003 2009 2012 2014 2016)
      will put all three curves on the same plot.

      If you need specific advice to transform your data, please post back showing example data, and use the -dataex- command to do that. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

      Added: Crossed with #2.

      Comment


      • #4
        lgraph is a user-written command as the FAQ asks you to explain; one of its options is "addplot(plot)" and that is probably the direction you want to go; if you need more, please supply, as requested in the FAQ, a -dataex- example within CODE blocks

        Comment


        • #5
          hello, many thanks for your reply. @Nick Cox , @Clyde Schechter , and @Rich Goldstein.

          this is part of my data:
          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str4 iso double(year fertilityrate) float(ae em)
          "USA" 1996  1.976 1 0
          "USA" 1997  1.971 1 0
          "USA" 1998  1.999 1 0
          "USA" 1999 2.0075 1 0
          "USA" 2000  2.056 1 0
          "USA" 2001 2.0305 1 0
          "USA" 2002 2.0205 1 0
          "USA" 2003 2.0475 1 0
          "USA" 2004 2.0515 1 0
          "USA" 2005  2.057 1 0
          "USA" 2006  2.108 1 0
          "USA" 2007   2.12 1 0
          "USA" 2008  2.072 1 0
          "USA" 2009  2.002 1 0
          "USA" 2010  1.931 1 0
          "USA" 2011 1.8945 1 0
          "USA" 2012 1.8805 1 0
          "USA" 2013 1.8575 1 0
          "USA" 2014 1.8625 1 0
          "USA" 2015 1.8435 1 0
          "USA" 2016 1.8205 1 0
          "USA" 2017 1.7655 1 0
          "USA" 2018 1.7295 1 0
          "USA" 2019  1.706 1 0
          "GBR" 1996   1.73 1 0
          "GBR" 1997   1.72 1 0
          "GBR" 1998   1.71 1 0
          "GBR" 1999   1.68 1 0
          "GBR" 2000   1.64 1 0
          "GBR" 2001   1.63 1 0
          "GBR" 2002   1.63 1 0
          "GBR" 2003    1.7 1 0
          "GBR" 2004   1.75 1 0
          "GBR" 2005   1.76 1 0
          "GBR" 2006   1.82 1 0
          "GBR" 2007   1.86 1 0
          "GBR" 2008   1.91 1 0
          "GBR" 2009   1.89 1 0
          "GBR" 2010   1.92 1 0
          "GBR" 2011   1.91 1 0
          "GBR" 2012   1.92 1 0
          "GBR" 2013   1.83 1 0
          "GBR" 2014   1.81 1 0
          "GBR" 2015    1.8 1 0
          "GBR" 2016   1.79 1 0
          "GBR" 2017   1.74 1 0
          "GBR" 2018   1.68 1 0
          "GBR" 2019   1.63 1 0
          "AUT" 1996   1.45 1 0
          "AUT" 1997   1.39 1 0
          "AUT" 1998   1.37 1 0
          "AUT" 1999   1.34 1 0
          "AUT" 2000   1.36 1 0
          "AUT" 2001   1.33 1 0
          "AUT" 2002   1.39 1 0
          "AUT" 2003   1.38 1 0
          "AUT" 2004   1.42 1 0
          "AUT" 2005   1.41 1 0
          "AUT" 2006   1.41 1 0
          "AUT" 2007   1.38 1 0
          "AUT" 2008   1.42 1 0
          "AUT" 2009   1.39 1 0
          "AUT" 2010   1.44 1 0
          "AUT" 2011   1.43 1 0
          "AUT" 2012   1.44 1 0
          "AUT" 2013   1.44 1 0
          "AUT" 2014   1.46 1 0
          "AUT" 2015   1.49 1 0
          "AUT" 2016   1.53 1 0
          "AUT" 2017   1.52 1 0
          "AUT" 2018   1.47 1 0
          "AUT" 2019   1.46 1 0
          "BEL" 1996   1.59 1 0
          "BEL" 1997    1.6 1 0
          "BEL" 1998    1.6 1 0
          "BEL" 1999   1.62 1 0
          "BEL" 2000   1.67 1 0
          "BEL" 2001   1.67 1 0
          "BEL" 2002   1.65 1 0
          "BEL" 2003   1.67 1 0
          "BEL" 2004   1.72 1 0
          "BEL" 2005   1.76 1 0
          "BEL" 2006    1.8 1 0
          "BEL" 2007   1.82 1 0
          "BEL" 2008   1.85 1 0
          "BEL" 2009   1.84 1 0
          "BEL" 2010   1.86 1 0
          "BEL" 2011   1.81 1 0
          "BEL" 2012    1.8 1 0
          "BEL" 2013   1.76 1 0
          "BEL" 2014   1.74 1 0
          "BEL" 2015    1.7 1 0
          "BEL" 2016   1.68 1 0
          "BEL" 2017   1.65 1 0
          "BEL" 2018   1.62 1 0
          "BEL" 2019    1.6 1 0
          "DNK" 1996   1.75 1 0
          "DNK" 1997   1.75 1 0
          "DNK" 1998   1.72 1 0
          "DNK" 1999   1.73 1 0
          end
          • that here I create a dummy variable for "adavances economies" and another for "emerging market economies" , name is "ae and em",respectively.
          • if I understood correct based on your advice, with "lgraph" I cannot reach the specific graph that I wanted, so first of all, I generate the mean of fertility: "
            Code:
            egen mean_fertility=mean( fertilityrate ), by(year)
          • but now when I did "
            Code:
            twoway conn mean_fertility year  || conn mean_fertility year if ae==1 || conn mean_fertility year if em==1, xline(2003 2009 2012 2014 2016)
            my graph is this: which is not correct. I apprriciate to recive your advice. many thanks
            Click image for larger version

Name:	Graph12.png
Views:	1
Size:	75.3 KB
ID:	1675453




          many thanks in advance for your valuable time and advice.

          best regards,
          Last edited by Khati Zolfaghari; 28 Jul 2022, 00:35.

          Comment


          • #6
            Code:
             
             egen mean_fertility=mean( fertilityrate ), by(ae em year)
            is closer to what you want, I guess.

            Comment


            • #7
              @Nick Cox thanks for your reply. I tried based on your advice. and I did:
              • egen mean_fertility=mean( fertilityrate ), by(ifscode year)
                that gives me the mean of fertility rate for the whole sample.
              • egen mean_fertility_ae=mean( fertilityrate ), by(ifscode year), if ae==1
              • egen mean_fertility_em=mean( fertilityrate ), by(ifscode year), if em==1
              • but now when I do in the graph the results is not as "
                twoway conn mean_fertility year || conn mean_fertility_ae year|| conn mean_fertility_em year , xline(2003 2009 2012 2014 2016)
              it gives me for each country. I want to get three separate lines just to have a comparison, but when I use the "lgraph" for each I can get seperatily only one line, that can show me the Evolution of average fertility rates over time.

              Click image for larger version

Name:	fertility-p.png
Views:	1
Size:	414.2 KB
ID:	1675486




              many thanks in advance for your valuable time and advice.

              regards,
              Last edited by Khati Zolfaghari; 28 Jul 2022, 05:09.

              Comment


              • #8
                You didn't follow my advice at all. I don't know what ifscode is: it is not mentioned in your data example or in any previous post in this thread.

                This shows that my idea works. You have three groups: ae == 1, em == 1, ae == 0 & em == 0. Naturally, your graph will be different. I can't use your data example as it will lead to one curve only.


                Code:
                webuse grunfeld, clear 
                gen fertility_rate = ln(invest)
                gen ae = company <= 3 
                gen em = inrange(company, 4, 7)
                
                * you start here 
                
                egen mean_fertility = mean(fertility_rate), by(ae em year)
                
                line mean_fertility year if ae == 1, sort || line mean_fertility year if em == 1, sort || line mean_fertility year if em == 0 & ae == 0 , sort legend(order(1 "ae"  2 "em" 3 "others") pos(3) col(1))
                Click image for larger version

Name:	fertility.png
Views:	1
Size:	31.8 KB
ID:	1675492




                Comment


                • #9
                  @Nick Cox thanks for your reply, and I really appologize if I bother you and I did not explain good in the past.
                  • " ifscode" in my data the id code that I get to all of my data, so it is for whole sample. then I create base on the dummy variable=1 if my countries were advanced economy and zero otherwise.
                  • I did the same for the emerging market.
                  • I try to follow your advice , and the graph in the panel data set that I have it seems is not correct. now I did again:
                  • [/QUOTE]egen mean_fertility = mean(fertilityrate), by(ae em year)
                    [/QUOTE]
                  • line mean_fertility year if ae == 1, sort || line mean_fertility year if em == 1, sort || line mean_fertility year if em == 0 & ae == 0 , sort legend(order(1 "ae" 2 "em" 3 "others") pos(3) col(1)) , tline(2003 2009 2012 2014 2016)
                  and I attached the graph.
                  • but when I did based on the lgraph and seperately:
                    lgraph fertility year if em==1, tline(2003 2009 2012 2014 2016)
                  • lgraph fertility year if em==1, tline(2003 2009 2012 2014 2016)
                  • lgraph fertility year , tline(2003 2009 2012 2014 2016)
                  my graph it seems make sense.

                  Click image for larger version

Name:	em&amp;ae.png
Views:	1
Size:	80.0 KB
ID:	1675502
                  Click image for larger version

Name:	merg-lgraph.png
Views:	1
Size:	84.6 KB
ID:	1675503



                  again I apologize for my simple problem , many thanks in advance or your valuable time and advice.

                  best regards,

                  Comment


                  • #10
                    Clearly #8 worked for me. There is some other story behind why it didn't work for you, possibly missing values, You don't give a data example that allows your latest to be checked. However,

                    Code:
                    tab ae em, missing
                    may help illumine the problem, as might

                    1. explaining exactly how you calculated ae and em.

                    2. trying

                    Code:
                     
                     egen mean_fertility = mean(fertility_rate) if !missing(ae, em), by(ae em year)
                    I really won't try to advise on use of lgraph, which I don't use myself.

                    Comment


                    • #11
                      @Nick Cox may thanks for your reply.
                      1)
                      gen ae=0
                      replace ae=1 if ifscode == 111
                      replace ae=1 if ifscode == 112
                      replace ae=1 if ifscode == 122
                      replace ae=1 if ifscode == 124
                      replace ae=1 if ifscode == 128
                      replace ae=1 if ifscode == 132
                      replace ae=1 if ifscode == 134
                      replace ae=1 if ifscode == 136
                      replace ae=1 if ifscode == 138
                      replace ae=1 if ifscode == 144
                      replace ae=1 if ifscode == 146
                      replace ae=1 if ifscode == 156
                      replace ae=1 if ifscode == 158
                      replace ae=1 if ifscode == 172
                      replace ae=1 if ifscode == 174
                      replace ae=1 if ifscode == 178
                      replace ae=1 if ifscode == 182
                      replace ae=1 if ifscode == 184
                      replace ae=1 if ifscode == 193
                      replace ae=1 if ifscode == 196
                      replace ae=1 if ifscode == 436
                      replace ae=1 if ifscode == 528
                      replace ae=1 if ifscode == 532
                      replace ae=1 if ifscode == 542
                      replace ae=1 if ifscode == 576
                      replace ae=1 if ifscode == 142
                      1-1)
                      gen em=0
                      replace em=1 if ifscode == 233
                      replace em=1 if ifscode == 228
                      replace em=1 if ifscode ==964
                      replace em=1 if ifscode == 469
                      replace em=1 if ifscode ==944
                      replace em=1 if ifscode == 935
                      replace em=1 if ifscode == 199
                      replace em=1 if ifscode == 223
                      replace em=1 if ifscode == 566
                      replace em=1 if ifscode ==273
                      replace em=1 if ifscode ==542
                      replace em=1 if ifscode ==922
                      replace em=1 if ifscode ==186
                      replace em=1 if ifscode ==293
                      replace em=1 if ifscode ==578
                      replace em=1 if ifscode ==548
                      replace em=1 if ifscode ==536
                      replace em=1 if ifscode ==686
                      replace em=1 if ifscode ==924

                      2)
                      | em
                      ae | 0 1 | Total
                      -----------+----------------------+----------
                      0 | 3,576 432 | 4,008
                      1 | 600 24 | 624
                      -----------+----------------------+----------
                      Total | 4,176 456 | 4,632
                      but the graph did not change.




                      I apologized to take your time.
                      Many thanks in advance for your valuable time and advice.


                      best regards,




                      Comment


                      • #12
                        Your code and results do show the problem. Look again: you have some observations with ae == 1 andem == 1, namely ifscode 542, Hence fix ae and em accordingly and re-calculate the means.

                        Comment


                        • #13
                          @Nick Cox Many thanks. rally thanks and I apologize to take your time.
                          yes, it works,

                          best regards,
                          Click image for larger version

Name:	correctgraph.png
Views:	1
Size:	62.8 KB
ID:	1675532

                          Comment


                          • #14
                            The legend would be better reversed, but you're on a better road now.

                            Comment


                            • #15
                              Nick Cox many thanks.


                              Best regards,

                              Comment

                              Working...
                              X