Announcement

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

  • #16
    Dear teacher Nick Cox , I have managed to do the exercise. The code generates the graphs I need. But, I am not sure whether it is the most efficient one (lack of confidence on the graph export mechanism; have very little experience with graph ). Kindly, check the Graph section code.
    Code:
    *-----------------------------------------------------------------------------
    *                                Data
    *-----------------------------------------------------------------------------
    clear
    input str32 country int year double(NGDPRPC NGDPDPC NGSD_NGDP PCPIPCH)
    "Cyprus"  2010 20490.637 31261.236 12.558  2.558
    "Cyprus"  2011  20051.42 32692.698 14.706  3.481
    "Cyprus"  2012 18936.417 29066.268  10.16  3.089
    "Cyprus"  2013 17732.983  27825.87  8.246   .381
    "Cyprus"  2014 17644.755 27267.428  8.022  -.268
    "Estonia" 2010  11052.81 14672.328 23.075  2.741
    "Estonia" 2011 11928.681 17470.842 26.442   5.08
    "Estonia" 2012 12487.099  17431.54 27.143  4.219
    "Estonia" 2013  12774.29 19078.213 27.426  3.247
    "Estonia" 2014 13170.071 19969.128 26.937   .476
    "Greece"  2010 20327.829 26972.873  5.664  4.704
    "Greece"  2011 18464.601 25896.934  5.099  3.118
    "Greece"  2012  17173.71 22171.911  8.972  1.035
    "Greece"  2013  16742.04 21805.257   9.56  -.854
    "Greece"  2014 16984.502  21726.89  10.28 -1.394
    "Malta"   2010 15939.801 21150.423 18.954  2.041
    "Malta"   2011 16113.851 22933.749 19.254  2.513
    "Malta"   2012 16450.882  22069.52 19.718  3.227
    "Malta"   2013 17005.342 24021.743 21.781   .979
    "Malta"   2014 18097.074 26202.597 26.674   .771
    end
    *label variables
    label    variable    NGDPRPC    "Gross domestic product per capita, constant prices"
    label    variable    NGDPDPC    "Gross domestic product per capita, current prices"
    label    variable    NGSD_NGDP  "Gross national savings"
    label    variable    PCPIPCH    "Inflation, average consumer prices"
    
    *-----------------------------------------------------------------------------
    *                            Generate Rank Variables
    *-----------------------------------------------------------------------------
    
    *generate rank variabels : largest value gets rank 1
    sort year
    foreach v in NGDPRPC NGDPDPC NGSD_NGDP PCPIPCH {
        by year: egen rank_`v' = rank(-`v')
    }
    
    *-----------------------------------------------------------------------------
    *                                    Graph
    *-----------------------------------------------------------------------------
    local names 
    gen tosort = . 
    
    quietly foreach var in NGDPRPC NGDPDPC NGSD PCPIPCH {
       separate rank_`var', by(country == "Cyprus") veryshortlabel gen(toplot) 
       forval year= 2010/2014 {
           replace tosort = cond(year == `year', rank_`var', .) 
           graph hbar (asis) toplot* if year==`year', nofill over(country, sort(tosort)) ///
           title("`: var label `var''" `year') b1title(Rank) legend(off) name(gr_`var'_`year', replace) saving(gr_`var'_`year', replace)
           local names `names' gr_`var'_`year'
           graph export gr_`var'_`year'.wmf, replace
       } 
       drop toplot* 
       
    }
    
    drop tosort 
    graph close gr*

    Comment


    • #17
      OK, except that

      1. Now that one variable is revealed as inflation, I suggest that you invert its ranking.

      2. The variable labels are helpful, but too long if you want to combine the graphs.

      3. Do you really want 20 separate graphs?

      Code:
      *-----------------------------------------------------------------------------
      *                                Data
      *-----------------------------------------------------------------------------
      clear
      input str32 country int year double(NGDPRPC NGDPDPC NGSD_NGDP PCPIPCH)
      "Cyprus"  2010 20490.637 31261.236 12.558  2.558
      "Cyprus"  2011  20051.42 32692.698 14.706  3.481
      "Cyprus"  2012 18936.417 29066.268  10.16  3.089
      "Cyprus"  2013 17732.983  27825.87  8.246   .381
      "Cyprus"  2014 17644.755 27267.428  8.022  -.268
      "Estonia" 2010  11052.81 14672.328 23.075  2.741
      "Estonia" 2011 11928.681 17470.842 26.442   5.08
      "Estonia" 2012 12487.099  17431.54 27.143  4.219
      "Estonia" 2013  12774.29 19078.213 27.426  3.247
      "Estonia" 2014 13170.071 19969.128 26.937   .476
      "Greece"  2010 20327.829 26972.873  5.664  4.704
      "Greece"  2011 18464.601 25896.934  5.099  3.118
      "Greece"  2012  17173.71 22171.911  8.972  1.035
      "Greece"  2013  16742.04 21805.257   9.56  -.854
      "Greece"  2014 16984.502  21726.89  10.28 -1.394
      "Malta"   2010 15939.801 21150.423 18.954  2.041
      "Malta"   2011 16113.851 22933.749 19.254  2.513
      "Malta"   2012 16450.882  22069.52 19.718  3.227
      "Malta"   2013 17005.342 24021.743 21.781   .979
      "Malta"   2014 18097.074 26202.597 26.674   .771
      end
      *label variables
      label    variable    NGDPRPC    "GDP pc, constant prices"
      label    variable    NGDPDPC    "GDP pc, current prices"
      label    variable    NGSD_NGDP  "Gross national savings"
      label    variable    PCPIPCH    "Inflation, consumer prices"
      
      *-----------------------------------------------------------------------------
      *                            Generate Rank Variables
      *-----------------------------------------------------------------------------
      
      *generate rank variabels : largest value gets rank 1
      sort year
      foreach v in NGDPRPC NGDPDPC NGSD_NGDP {
          by year: egen rank_`v' = rank(-`v')
      }
      
      by year: egen rank_PCPIPCH = rank(PCPIPCH)
      
      *-----------------------------------------------------------------------------
      *                                    Graph
      *-----------------------------------------------------------------------------
      local names 
      gen tosort = . 
      
      quietly foreach var in NGDPRPC NGDPDPC NGSD PCPIPCH {
         separate rank_`var', by(country == "Cyprus") veryshortlabel gen(toplot) 
         forval year= 2010/2014 {
             replace tosort = cond(year == `year', rank_`var', .) 
             graph hbar (asis) toplot* if year==`year', nofill over(country, sort(tosort)) ///
             title("`: var label `var''" `year') b1title(Rank) legend(off) name(gr_`var'_`year', replace) saving(gr_`var'_`year', replace)
             local names `names' gr_`var'_`year'
         } 
         drop toplot* 
         
      }
      
      drop tosort 
      graph combine `names'
      Click image for larger version

Name:	budu3.png
Views:	1
Size:	45.1 KB
ID:	1491316


      Comment


      • #18
        I return to #9 with some second thoughts.

        Click image for larger version

Name:	budu4.png
Views:	1
Size:	37.9 KB
ID:	1491333


        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str32 country int year float(rank_NGDPRPC rank_NGDPDPC rank_NGSD_NGDP rank_PCPIPCH)
        "Greece"  2010 2 2 4 1
        "Estonia" 2010 4 4 1 2
        "Cyprus"  2010 1 1 3 3
        "Malta"   2010 3 3 2 4
        "Greece"  2011 2 2 4 3
        "Malta"   2011 3 3 2 4
        "Estonia" 2011 4 4 1 1
        "Cyprus"  2011 1 1 3 2
        "Malta"   2012 3 3 2 2
        "Greece"  2012 2 2 4 4
        "Cyprus"  2012 1 1 3 3
        "Estonia" 2012 4 4 1 1
        "Estonia" 2013 4 4 1 1
        "Greece"  2013 3 3 3 4
        "Malta"   2013 2 2 2 2
        "Cyprus"  2013 1 1 4 3
        "Cyprus"  2014 2 1 4 3
        "Estonia" 2014 4 4 1 2
        "Malta"   2014 1 2 2 1
        "Greece"  2014 3 3 3 4
        end
        
        reshape long rank_, i(country year) j(measure) string
        replace rank_ = 5 - rank_ if measure == "PCPIPCH"
        
        replace measure = "GDP pc, current prices" if measure == "NGDPDPC"
        replace measure = "GDP pc, constant prices" if measure == "NGDPRPC"
        replace measure = "Gross national savings" if measure == "NGSD_NGDP" 
        replace measure = "Inflation, consumer prices" if measure == "PCPIPCH"     
        
        separate rank_, by(country) veryshortlabel
        
        scatter rank_? year, by(measure, note("") legend(off)) ///
        ms(none ..) mlabpos(0 ..) mlabsize(medlarge medsmall ..) mlabcolor(red black ..)  mla(country country country country) ///
        xsc(r(2009.5 2014.5)) ysc(r(0.8 4.2)) yla(, ang(h)) ysc(reverse) xla(2010/2014) subtitle(, fcolor(eltgreen*0.25)) xtitle("")

        Comment


        • #19
          Nick Cox: Many thanks for your feedback! In the actual data set, I am dealing with 12 variables and 28 countries; I suppose it would not be possible to combine the graphs. Regarding the ranking of inflation, I agree with your suggestion -- inverting the ranking would be a better choice. Thanks again!

          Comment


          • #20
            I doubt that any of these approaches will survive transfer to that size of dataset.

            Comment

            Working...
            X