Announcement

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

  • Panel Data - Graph

    Hello, I'm new on Statalist forum!
    I have a couple of questions relating how to graph time series panel data.

    My database have quarterly data for 35 countries spanning the period 1969q1--2017q2, and the structure (data below is just to illustrate, not the real) looks like this, id-->35:

    id |quarter | country | Prices | GDP PC
    1 19694 USA
    1 19701 USA 12.745783 128.15845
    1 19702 USA 12.834085 126.52808
    ...

    I would like to graph Prices (Y axis) and GDP PC (X axis) for all countries or for at least 10 of them, over the period, and analyse how the data distribute across countries. I did some scatter, but it looks quite "noisy" and its very difficult to spot any pattern. So, if you have any advice, please share it with me!

    Thanks you very much.

  • #2
    This example requires the labmask command. The easiest way to find and install it is to type in Stata search labmask and follow the links. Also see https://www.statalist.org/forums/for...abeling-legend for a related discussion.

    Code:
    // open some example data
    clear all
    use "https://www.rug.nl/ggdc/docs/pwt90.dta", clear
    
    gen gdppc = rgdpe/ pop
    keep country year gdppc pl_con
    
    // keep the first 35 countries
    bys country (year) : gen mark =  _n == 1
    replace mark = sum(mark)
    keep if mark <= 35
    drop mark
    
    // sort countries by their average price level
    bys country : egen mean = mean(pl_con)
    egen order = rank(mean), track
    labmask order, values(country)
    
    // add background lines
    tempfile temp
    save `temp'
    
    separate pl_con, by(country) veryshortlabel
    
    forvalues i = 1/35 {
        bys year (pl_con`i') : replace pl_con`i' = pl_con`i'[1]
        by year : gen gdppc`i' = gdppc[1]
    }
    keep if country == "Albania"
    keep year pl_con? pl_con?? gdppc? gdppc??
    merge 1:m year using `temp'
    
    local gr ""
    forvalues i = 1/35 {
        local gr `gr' line pl_con`i' gdppc`i', lpattern(solid) lcolor(gs12) ||
    }
    
    // final graph
    sort order year
    twoway `gr' ///
        line pl_con gdppc , by(order, legend(off) compact note("")) ///
        lpattern(solid) lwidth(*1.2) ///
        xscale(log) xlab(1000 "10{sup:3}" 10000 "10{sup:4}" 100000 "10{sup:5}") ///
        xtitle("GDP per capita (log scale)") ytitle("price level (USA 2011 = 1)")
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	582.6 KB
ID:	1425923
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Since there is a time element to the graph you can make that explicit by labeling the beginning and end with the year. In my example that is particularly useful as not all countries start at the same point in time.

      Code:
      // open some example data
      clear all
      use "https://www.rug.nl/ggdc/docs/pwt90.dta", clear
      
      gen gdppc = rgdpe/ pop
      keep country year gdppc pl_con
      
      // keep the first 35 countries
      bys country (year) : gen mark =  _n == 1
      replace mark = sum(mark)
      keep if mark <= 35
      drop mark
      
      // sort countries by their average price level
      bys country : egen mean = mean(pl_con)
      egen order = rank(mean), track
      labmask order, values(country)
      
      // add background lines
      tempfile temp
      save `temp'
      
      separate pl_con, by(country) veryshortlabel
      
      forvalues i = 1/35 {
          bys year (pl_con`i') : replace pl_con`i' = pl_con`i'[1]
          by year : gen gdppc`i' = gdppc[1]
      }
      keep if country == "Albania"
      keep year pl_con? pl_con?? gdppc? gdppc??
      merge 1:m year using `temp'
      
      local gr ""
      forvalues i = 1/35 {
          local gr `gr' line pl_con`i' gdppc`i', lpattern(solid) lcolor(gs12) ||
      }
      
      // begin and end year labels
      gen byte miss = missing(gdppc)
      bys country miss (year) : gen ystart = pl_con if _n == 1
      bys country miss (year) : gen xstart = gdppc  if _n == 1
      bys country miss (year) : gen yend = pl_con if _n == _N
      bys country miss (year) : gen xendt = gdppc  if _n == _N
      gen startlabpos = 9
      replace startlabpos = 3 if inlist(country, "Bangladesh", "Cambodia", ///
          "Belarus", "Burundi", "Armenia", "Azerbaijan", "Angola")
      replace startlabpos = 10 if inlist(country, "Algeria")
      replace startlabpos = 11 if inlist(country, "Burkina Faso")
      replace startlabpos = 12 if inlist(country, "Botswana")
      // final graph
      sort order year
      twoway `gr' ///
          line pl_con gdppc , by(order, legend(off) compact note("")) ///
          lpattern(solid) lwidth(*1.2) ///
          xscale(log) xlab(1000 "10{sup:3}" 10000 "10{sup:4}" 100000 "10{sup:5}") ///
          xtitle("GDP per capita (log scale)") ///
          ytitle("price level (USA 2011 = 1)") || ///
          scatter ystart xstart, msymbol(i) mlabel(year) mlabvpos(startlabpos) || ///
          scatter yend xend, msymbol(i) mlabel(year)
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	590.6 KB
ID:	1425932
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        The strategy of showing each subset as foreground and the rest as background could be very old, except that in practice it seems to hinge on being able to show background data in a light grey (gray) colour (color).

        Some references at https://stats.stackexchange.com/ques.../190328#190328

        I would be delighted to learn of other (especially ancient or excellent) examples.

        Comment


        • #5
          Thanks you very much for your detailed answer, Maarten. I will play around with the code and its application.

          Nick, thanks you for the extra example.

          Cheers,

          Comment


          • #6
            Maarten Buis I followed your code for this example and it worked. Is there a way to adjust the code to add two lines (like parallel trends)?

            I would appreciate any assistance.

            Thanks,
            Anoush K.

            Comment

            Working...
            X