Showing posts with label proportion. Show all posts
Showing posts with label proportion. Show all posts

Apr 27, 2016

Random graphs (76): Line plot with confidence band

bhps_ukhls netuse netpuse indpxus_lw strata psu using indresp, ukhlswaves(1/5) ///
           ukhlsdir("C:\data\UKDA-6614-stata11_se\stata11_se\") ///
           vallabcheck
    
// Generate variable of interest
generate internetuse = .
replace  internetuse = netuse if inlist(wave, 19, 20)
label define internetuse 1 "no access at home, at work or elsewhere" ///
                         2 "never use" ///
                         3 "less than once a month" ///
                         4 "once a month" ///
                         5 "several times a month" ///
                         6 "several times a week" ///
                         7 "every day" ///
                        -1 "don't know" ///
                        -2 "refused" ///
                        -7 "proxy" ///
                        -9 "missing", modify
label val internetuse internetuse
replace  internetuse = 8 - netpuse if inlist(wave, 21, 22, 23) & netpuse > 0
replace  internetuse = netpuse if inlist(wave, 21, 22, 23) & netpuse < 0

recode internetuse (-1 = .a) (-2 = .b) (-7 = .c) (-9 = .d)

generate dailyuser = (internetuse == 7) if !missing(internetuse)

// Look at unweighted data
tab dailyuser wave, mis col nof

// Declare weights
svyset psu [pweight=indpxus_lw], strata(strata)

// Calculate weighted results
proportion dailyuser, over(wave)

// Save results in matrix 
matrix coefs  = r(table)

// Make data set of findings
clear
svmat coefs, names(eqcol)

// Get rid of unnecessary columns and rows
drop _prop_1*
keep in 1/6
drop in 2/4

// Get data into proper shape
gen varid = _n
reshape long _prop_2, i(varid) j(wave)
replace _prop_2 = _prop_2 * 100
reshape wide _prop_2, i(wave) j(varid)

rename _prop_21 dailyusers
rename _prop_22 lb
rename _prop_23 ub

label define wave 19 "2009/10" ///
                  20 "2010/11" ///
                  21 "2011/12" ///
                  22 "2012/13" ///
                  23 "2013/14"
label val wave wave

twoway (rarea lb ub wave) ///
       (line dailyusers wave) ///
      , legend(off) xlabel(, val) ///
        ytitle("% daily internet users") ///
        xtitle("") ylabel(, grid) ///
        note(" " ///
             "{it:Note:} Gray area denotes 95% confidence bands. {it:Source:} UKHLS waves 1-5, weighted data.", span)

Apr 4, 2016

Random graphs (71): Proportions with confidence intervals

use sharew5_rel1-0-0_it.dta, clear

// Prepare variables
recode it004_ (1 = 1 "Yes") (5 = 0 "No") (-1 -2 . = .), generate(internetuser)
label var internetuser "Internet use in last 7 days"
decode country, gen(cntry)

// Define stuff
tempname foo
postfile `foo' str25 cntry perc perc_ll perc_ul using "C:\Windows\Temp\test.dta", replace
levelsof cntry, local(levels)

// Calculate proportions by country
foreach country of local levels {

    capture proportion internetuser if cntry == "`country'"
    matrix fcoefs  = r(table)
    local fperc    = fcoefs[1,2] * 100
    local fperc_ll = fcoefs[5,2] * 100
    local fperc_ul = fcoefs[6,2] * 100

    di "`country'"  _skip(2)  `fperc_ll' _skip(2) `fperc' _skip(2) `fperc_ul'

    if "`fperc'" != "" {    // Make sure that the loop doesn't break if empty
    post `foo' ("`country'") (`fperc') (`fperc_ll') (`fperc_ul')
    }
}
postclose `foo'

// Use posted data set
use "C:\Windows\Temp\test.dta", clear

// Sort countries by size
egen order_ = rank(perc), unique
labmask order_, value(cntry) 
 
// Plot
twoway (dot perc order_, horizontal) ///
       (rspike perc_ll perc_ul order_, horizontal), ///
       ylabel(1/15, val) legend(off) xscale(range(20 80) alt) ///
       ytitle("") xtitle("% Internet users (in last 7 days)") ///
       xlabel(20(10)80, grid) ///
       note(" " "{it:Source:} SHARE wave 5, doi:10.6103/SHARE.w5.100", span)

Mar 14, 2016

Random graphs (65): Dot plots with confidence intervals

preserve
// Keep those countries for which three waves are available
keep if inlist(cntry, "AT", "AU", "BG", "CZ", "DEE", "DEW", "ES", "GB", "HU") | ///
        inlist(cntry, "IE", "IL", "JP", "NL", "NO", "PH", "PL", "RU", "SE")   | ///
  inlist(cntry, "SI", "US")

// Define temporary objects
tempname foo
tempname foox
postfile `foo' str3 cntry year perc perc_ll perc_ul using `foox', replace

levelsof cntry, local(levels1)
levelsof year, local(levels2)

// Calculate proportions by country and year
foreach year of local levels2 {
 foreach country of local levels1 {

    capture proportion separate if cntry == "`country'" & year == `year'
    *matrix list r(table)
    matrix fcoefs  = r(table)
    local fperc    = fcoefs[1,2] * 100
    local fperc_ll = fcoefs[5,2] * 100
    local fperc_ul = fcoefs[6,2] * 100

    di "`country'"  _skip(2) `year' _skip(2)  `fperc_ll' _skip(2) `fperc' _skip(2) `fperc_ul'

    if "`fperc'" != "" {    // Make sure that the loop doesn't break if empty
    post `foo' ("`country'") (`year') (`fperc') (`fperc_ll') (`fperc_ul')
    }
 }
}
postclose `foo'

// Use posted data set
use `foox', clear

// Generate country name variable using -kountry-
kountry cntry, from(iso2c)
rename NAMES_STD country
replace country = "Germany (West)" if country == "dew"
replace country = "Germany (East)" if country == "dee"

// Plot average change over time
twoway (scatter perc year, connect(direct) msymbol(o)) ///
       (rcap perc_ll perc_ul year) ///
      , by(country, ///
           note("{it:Source:} ISSP 'Family and Changing Gender Roles II{c 150}IV. {it:Note:} Error bars denote 95% confidence intervals.", span) ///
           legend(off)) ///
        xlabel(1994 2002 2012) xtitle("") ///
        ytitle("% of couples keeping incomes separate") ///
        ylabel(0 10 20 30 40 50, gstyle(minor)) yscale(r(0))
        // yscale(r(0)) adds missing gridline according to
        // http://www.stata.com/statalist/archive/2013-04/msg00904.html    
restore

Feb 25, 2016

Random graphs (61): Dot plot for two groups

tempname foo
tempname foox
postfile `foo' str2 cntry perc_f perc_f_ll perc_f_ul ///
                          perc_m perc_m_ll perc_m_ul using `foox' , replace

levelsof cntry, local(levels1)

// Calculate proportions by country
foreach country of local levels1 {

    capture proportion nonesep_f if cntry == "`country'"
    *matrix list r(table)
    matrix fcoefs  = r(table)
    local fperc    = fcoefs[1,2] * 100
    local fperc_ll = fcoefs[5,2] * 100
    local fperc_ul = fcoefs[6,2] * 100

    capture proportion nonesep_m if cntry == "`country'"
    *matrix list r(table)
    matrix mcoefs  = r(table)
    local mperc    = mcoefs[1,2] * 100
    local mperc_ll = mcoefs[5,2] * 100
    local mperc_ul = mcoefs[6,2] * 100 
 
    di "`country'"  _skip(2) `fperc_ll' _skip(2) `fperc' _skip(2) `fperc_ul'
    di "`country'"  _skip(2) `mperc_ll' _skip(2) `mperc' _skip(2) `mperc_ul'

    if "`fperc'" != "" {    // Make sure that the loop doesn't break if empty
    post `foo' ("`country'") (`fperc') (`fperc_ll') (`fperc_ul') (`mperc') (`mperc_ll') (`mperc_ul') 
  }
}

postclose `foo'

// Use posted data set
use `foox', clear

// Reverse sign of one variable
replace perc_m    = -perc_m 
replace perc_m_ll = -perc_m_ll
replace perc_m_ul = -perc_m_ul

// Sort countries by size
egen order_ = rank(-perc_f), unique
labmask order_, value(cntry)

// Plot
twoway (dot perc_m order_, horizontal) ///
    (dot perc_f order_, horizontal) ///
      , ylabel(1/31, val) legend(order(1 "Men" 2 "Women") pos(6) row(1)) ///
        ytitle("") xtitle("% of men/women pooling all income") ///
  xline(0) ///
  xlabel(-100 "100" -50 "50" 0 "0" 50 "50" 100 "100") ///
        name(somefigure, replace) ysize(8)

Nov 30, 2014

Random graphs (37): Proportions with confidence intervals

use acountry asex aage ayear a602 a612 aweight using GGS_Wave1_V.4.2.dta, clear // Create outcome variable recode a612 (1 2 1601 = 1 "(Likely) infertile") /// (3 4 1501 1602 = 0 "(Likely) fertile") /// (2001 . .a .b .c = .x "Don't know"), /// gen(infertility) replace infertility = 0 if a602 == 1 // Also fertile when currently pregnant // Create country identifier gen cntry = "" replace cntry = "BG" if acountry == 11 replace cntry = "RU" if acountry == 12 replace cntry = "GE" if acountry == 13 replace cntry = "DE" if acountry == 14 replace cntry = "FR" if acountry == 15 replace cntry = "HU" if acountry == 16 replace cntry = "IT" if acountry == 17 replace cntry = "NL" if acountry == 18 replace cntry = "RO" if acountry == 19 replace cntry = "NO" if acountry == 20 replace cntry = "AT" if acountry == 21 replace cntry = "EE" if acountry == 22 replace cntry = "BE" if acountry == 23 replace cntry = "AU" if acountry == 24 replace cntry = "LT" if acountry == 25 replace cntry = "PL" if acountry == 26 replace cntry = "CZ" if acountry == 28 // Recode gender variable gen sex = "" replace sex = "Men" if asex == 1 replace sex = "Women" if asex == 2 // Drop unnecessary countries drop if inlist(cntry, "IT", "AU", "NL") // Prevalence plot by country preserve // Drop unnecesary age groups keep if aage <= 39 keep if aage >= 34 svyset [pweight = aweight] // Declare weight // Create temporary files and postfile tempname foo tempname infert postfile `foo' str2 cntry str5 sex perc_infert perc_infert_ll perc_infert_ul using `infert' , replace levelsof cntry, local(levels1) levelsof sex, local(sex1) // Calculate proportions by country and sex foreach sex of local sex1 { foreach country of local levels1 { // Calculate proportions capture proportion infertility if cntry == "`country'" & sex == "`sex'" *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 di "`country'" _skip(2) `perc_ll' _skip(2) `perc' _skip(2) `perc_ul' if "`perc'" != "" { // Make sure that the loop doesn't break if empty post `foo' ("`country'") ("`sex'") (`perc') (`perc_ll') (`perc_ul') } } } postclose `foo' // Use posted data set use `infert', clear // Sort countries by size for women egen order_ = rank(-perc_infert) if sex == "Women", unique labmask order_, value(cntry) // Plot for women twoway (dot perc_infert order_ if sex == "Women", horizontal) /// (rcap perc_infert_ll perc_infert_ul order_ if sex == "Women", horizontal) /// , ylabel(1/14, val) legend(off) /// ytitle("") xtitle("% of {bf:women} aged 35{c 0150}39 reporting (suspected) infertility") /// /*caption("{it:Note:} Error bars denote 95 % confidence intervals", size(small) span)*/ /// name(infert3539women, replace) // Drop women's order drop order_ // Sort countries by size for men egen order_ = rank(-perc_infert) if sex == "Men", unique labmask order_, value(cntry) // Plot for men twoway (dot perc_infert order_ if sex == "Men", horizontal) /// (rcap perc_infert_ll perc_infert_ul order_ if sex == "Men", horizontal) /// , ylabel(1/13, val) legend(off) /// ytitle("") xtitle("% of {bf:men} aged 35{c 0150}39 reporting (suspected) infertility") /// /*caption("{it:Note:} Error bars denote 95 % confidence intervals", size(small) span)*/ /// name(infert3539men, replace) // Combine graphs graph combine infert3539women infert3539men /// , ysize(8) col(1) xcommon /// caption("{it:Source:} Generations and Gender Survey Wave 1 (2005{c 0150}2011}" /// "{it:Note:} Error bars denote 95 % confidence intervals", size(vsmall) span) restore
// Plot infertility by age // Recode age into groups recode aage (20/24 = 1 "20–24") /// (25/29 = 2 "25–29") /// (30/34 = 3 "30–34") /// (35/39 = 4 "35–39") /// (40/45 = 5 "40–45") /// (1/19 = .a "Below 20 y") /// (46/85 = .b "46+ y") /// , gen(age) // Save valuelabels in temporary do-file tempfile valuelabels label save age using `valuelabels' svyset [pweight = aweight] // Declare weight // Create temporary files and postfile tempname foo tempname infertage postfile `foo' str2 cntry str5 sex age perc_infert perc_infert_ll perc_infert_ul using `infertage' , replace levelsof cntry, local(levels1) levelsof sex, local(sex1) // Calculate proportion by age group, country, and sex foreach sex of local sex1 { foreach country of local levels1 { forvalues x = 1/5 { // Calculate proportions capture proportion infertility if age == `x' & cntry == "`country'" & sex == "`sex'" *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 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'") ("`sex'") (`x') (`perc') (`perc_ll') (`perc_ul') } } } } postclose `foo' // Use posted values use `infertage', clear drop if perc_infert == . // Drop empty rows // Define and set saved value labels again do `valuelabels' label val age age // Plot for women by country and age twoway (rarea perc_infert_ll perc_infert_ul age if sex == "Women", fcolor(gs15) cmissing(no)) /// (line perc_infert age if sex == "Women", cmissing(no)) /// , by(cntry, cols(3) legend(off) note("") /// caption("{it:Source:} Generations and Gender Survey Wave 1 (2005{c 0150}2011)" /// "{it:Note:} Gray areas denote 95 % confidence intervals", size(vsmall) span)) /// ytitle("% of {bf:women} reporting (suspected) infertility") xtitle("Age group") /// xlabel(1/5, val alternate) /// ysize(8) name(infertage, replace)

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 */