Showing posts with label _skip(#). Show all posts
Showing posts with label _skip(#). Show all posts

Dec 17, 2014

Random graphs (39): Plotting on a log axis


Rather than looking at a variable in absolute terms, namely GDP per capita in the upper panel of the Figure, it is also sometimes helpful to look at it in terms of percentage increases, as in the lower panel. Equal distances on the x-axis refer to equal percentage increases in GDP per capita. Four equidistant points on the x-axis are labeled, each indicating a fourfold increase in GDP.

use wvs2005_v20090901a.dta, clear

// Country variable
// 1) Turn into string
decode v2, gen(ctry)

// 2) Abbreviate
kountry ctry, from(other) stuck marker
ren _ISO3N_ cntry
kountry cntry, from(iso3n) to(iso2c)
ren _ISO2C_ country

// Generate outcome: % in good health
generate goodhealth = 100 if v11 <= 2
replace  goodhealth =   0 if v11  > 2
replace  goodhealth =   . if v11 == .

// Collapse data set
collapse (mean) goodhealth [pw = v259], by(country)

// Generate year variable
gen year = 2006

// Preserve collapsed data set
preserve

// Get GDP from World Bank data base
wbopendata, language(en - English) country() topics() indicator(NY.GDP.PCAP.PP.CD) clear long

// Fix obtained data set
ren ny_gdp_pcap_pp_cd gdp
ren iso2code country
keep if year == 2006
keep gdp country
drop if country == ""

// Save obtained GDP data
tempfile gdp
save `gdp'

// Restore 
restore

// Merge GDP with collapsed data set
merge m:1 country using `gdp'
keep if _merge == 3
drop _merge

// Create labels with thousand separator
label define gdp 20000 "20,000" ///
                 40000 "40,000" ///
                 60000 "60,000"
label val gdp gdp

// Plot on unlogged axis
twoway (scatter goodhealth gdp, mlabel(country) mlabpos(0) msymbol(none)) ///
       (lfit goodhealth gdp) ///
      , legend(off) xtitle("GDP per capita, 2006, PPP in current international $") ///    
        ytitle("% in good health") ///
        xlabel(0(20000)60000, valuelabels) ///
        name(unlogged, replace)

// Generate logged variable
gen loggdp = log(gdp) * 1000  // Multiply by 1,000 because only integers can be labeled

// Generate numbers for labeling
//  Round them to three decimal digits, then multiply by 1,000 to get integers
local log1 = round(log(1000), .001) * 1000
local log2 = round(log(1000 * 4), .001) * 1000
local log3 = round(log(1000 * 4 * 4), .001) * 1000
local log4 = round(log(1000 * 4 * 4 * 4), .001) *1000

*di `log1' _skip(2) `log2' _skip(2) `log3' _skip(2) `log4' 

// Create labels for numbers to be labeled
label define loggdp `log1' "1,000" ///
                    `log2' "4,000" ///
                    `log3' "16,000" ///                    
                    `log4' "64,000"
label value loggdp loggdp

// Plot on log axis
twoway (scatter goodhealth loggdp, mlabel(country) mlabpos(0) msymbol(none)) ///
       (lfit goodhealth loggdp) ///
      , legend(off) xtitle("GDP per capita, 2006, PPP in current international $") ///   
        ytitle("% in good health") ///
        xlabel(`log1' `log2' `log3' `log4', valuelabels) ///
        name(logged, replace)

// Combine plots
graph combine unlogged logged, col(1) ysize(8)

Sep 14, 2014

Random graphs (29): Line plots with confidence bands

use mainstat age country refyear coeff if refyear == 2011 using eulfs.dta, replace

drop refyear // Drop unnecessary variable
generate retired = (mainstat == 4) if mainstat != . // Create dummy variable for retired

svyset [pweight = coeff] // Declare weight

// Create temporary files and postfile
tempname foo
tempname retage
postfile `foo' cntry age perc_retired perc_retired_ll perc_retired_ul using `retage' , replace

levelsof country, local(levels1)

foreach country of local levels1 {
  forvalues x = 57 (5) 77 {
      // Calculate proportions
    capture proportion retired if age == `x' & country == `country'
    *matrix list r(table)
    matrix coefs  = r(table)
    local perc    = coefs[1,2] * 100
    local perc_ll = coefs[5,2] * 100
    local perc_ul = coefs[6,2] * 100

    qui di `country' _skip(2) `x' _skip(2) `perc_ll' _skip(2) `perc_' _skip(2) `perc_ul'

    if "`perc'" != "" {    // Make sure that the loop doesn't break if 
    post `foo' (`country') (`x') (`perc') (`perc_ll') (`perc_ul') 
 }
  }
}

postclose `foo'

use `retage', clear         

drop if perc_retired == .   // Drop empty rows

  // Label variables
label define age 57 "55{c 150}59 y" 62 "60{c 150}64 y" 67 "65{c 150}69 y" ///
                 72 "70{c 150}74 y" 77 "75{c 150}79 y", modify
label value age age
label define cntry  1 "AT"  2 "BE"  3 "BG"  4 "CH"  5 "CY" ///
                    6 "CZ"  7 "DE"  8 "DK"  9 "EE" 10 "ES" ///                    
                   11 "FI" 12 "FR" 13 "GR" 14 "HU" 15 "IE" ///
                   16 "IS" 17 "IT" 18 "LT" 19 "LU" 20 "LV" ///
                   21 "MT" 22 "NL" 23 "NO" 24 "PL" 25 "PT" ///
                   26 "RO" 27 "SE" 28 "SI" 29 "SK" 30 "UK" ///
                 , modify
label value cntry cntry

  // Plot plots     
twoway (rarea perc_retired_ll perc_retired_ul age, fcolor(gs14)) ///
       (line perc_retired age) ///   
      , by(cntry, cols(3) legend(off) note("") ///
                  caption("{it:Source:} EU-LFS 2011" ///
                          "{it:Notes:} Gray areas denote 95 % confidence intervals"))) ///
        ytitle("% retired") xtitle("Age group") ///
        xlabel(57 (5) 77, val ang(v)) ///
        ysize(10) 

/* ,by(, note("")) suppresses the default "Graphs by" note) */
/* legend(off) needs to be in by(, legend(off)) to work */