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)
Showing posts with label svyset. Show all posts
Showing posts with label svyset. Show all posts
Apr 27, 2016
Random graphs (76): Line plot with confidence band
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 */
Subscribe to:
Posts (Atom)



