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
Showing posts with label inlist(). Show all posts
Showing posts with label inlist(). Show all posts
Mar 14, 2016
Random graphs (65): Dot plots with confidence intervals
Labels:
foreach,
if,
inlist(),
kountry,
levelsof,
Missing gridlines,
postfile,
proportion,
Random graphs,
twoway rcap,
twoway scatter
Jan 5, 2016
Random graphs (55): Box plots
wbopendata, indicator(NY.GDP.PCAP.KD) clear
// Select countries
#delimit ;
keep if inlist(countrycode, "AUS", "AUT", "BEL", "CAN", "DNK") |
inlist(countrycode, "FIN", "FRA", "DEU", "GRC", "ISL") |
inlist(countrycode, "IRL", "ITA", "JPN", "NLD") |
inlist(countrycode, "NZL", "NOR", "PRT", "ESP") |
inlist(countrycode, "SWE", "CHE", "USA") ;
#delimit cr
// Get rid of unnecessary years
keep countryname countrycode iso2code region regioncode ///
indicatorname indicatorcode ///
yr1960 yr1970 yr1980 yr1990 yr2000 yr2010
// Reshape into long format
reshape long yr, i(countryname) j(year)
// Prepare outcome variable
rename yr gdppc
label var gdppc "GDP per capita (constant 2005 US$)"
// Plot
graph box gdppc, over(year)
Labels:
graph box,
inlist(),
Random graphs,
reshape,
wbopendata
Nov 2, 2014
Random graphs (35): Line plots
clear
import excel "art births across countries.xlsx", sheet("Data") firstrow case(lower) clear
ren a geo // Rename country ID
// Rename variables: perc_YEAR format
foreach v of varlist b-o {
local x : variable label `v'
rename `v' perc`x'
}
// Reshape data set to long format
reshape long perc, i(geo) j(year)
// Cross-sectional bar graph at latest point in time
by geo: egen lastobs = max(cond(!missing(perc), year, .)) // Identify year of latest observation
gen byte last = year == lastobs // ID variable of latest observation
// According to http://www.stata.com/support/faqs/data-management/first-and-last-occurrences/
graph hbar perc if year >= 2007 & last == 1, over(geo, sort(perc) descending) nofill ///
ytitle("% ART live births among all births") ylabel(0(1)6) ///
note("{it:Note:} Data refer to 2010 or latest available (no earlier than 2007)", span) ///
/*title("Country differences in ART birth rates")*/ ///
name(crosssec, replace)
// Create classical twoway line plot
twoway (line perc year if geo == "DK", cmissing(no)) ///
(line perc year if geo == "SI", cmissing(no)) ///
(line perc year if geo == "BE", cmissing(no)) ///
(line perc year if geo == "SE", cmissing(no)) ///
(line perc year if geo == "DE", cmissing(no)) ///
(line perc year if geo == "FR", cmissing(no)) ///
(line perc year if geo == "IT", cmissing(no)) ///
(line perc year if geo == "UK", cmissing(no)) ///
(line perc year if geo == "US", cmissing(no)) ///
, xlabel(1997(1)2010, ang(h) labsize(small)) xtitle("") ///
ylabel(0(1)6) ytitle("% ART live births among all births") ///
legend(label(1 "DK") label(2 "SI") label(3 "BE") ///
label(4 "SE") label(5 "DE") label(6 "FR") ///
label(7 "IT") label(8 "UK") label(9 "US") ///
pos(2))
//cmissing(no) makes sure that gaps are not filled
// Use panel line plot functionality keep if inlist(geo, "DK", "SI", "BE", "SE", "DE", "FR", "IT", "UK", "US") encode geo, gen(country) xtset country year, yearly xtline perc, overlay xlabel(1997(1)2010, ang(h) labsize(small)) xtitle("") /// ylabel(0(1)6) ytitle("% ART live births among all births") ///
legend(pos(2)) // Note that the gaps are much harder to spot in this plot
// Small multiples version
twoway (line perc year, by(geo, note("") /*title("Development of ART birth rate, selected countries")*/) cmissing(no)), ///
xlabel(1998 (2) 2010, ang(h) labsize(small) alternate) xtitle("") ///
ylabel(0(1)6) yscale(r(0)) ytitle("% ART live births among all births") ///
name(longit, replace)
// yscale(r(0)) adds missing gridline according to
// http://www.stata.com/statalist/archive/2013-04/msg00904.html
Jun 6, 2014
Random graphs (25): Line plot
use country refyear coeff age wstator hwactual exist2j hwactua2 if refyear == 2011 using eulfs.dta, clear
// Handle missing values:
replace hwactual = 0 if age < 15
replace hwactual = 0 if inlist(wstator, 3, 4, 5)
replace hwactua2 = 0 if inlist(exist2j, 1, .)
replace hwactua2 = 0 if age < 15
// Sum up working hours from first and second job
gen hours = hwactual + hwactua2
// Drop unnecessary variables
drop refyear wstator exist2j
// Collapse to calculate means
collapse (mean) hours [pweight = coeff], by(country age)
// Label variables
label define age2 02 "0{c 150}4" 07 "5{c 150}9" ///
12 "10{c 150}14" 17 "15{c 150}19" ///
22 "20{c 150}24" 27 "25{c 150}29" ///
32 "30{c 150}34" 37 "35{c 150}39" ///
42 "40{c 150}44" 47 "45{c 150}49" ///
52 "50{c 150}54" 57 "55{c 150}59" ///
62 "60{c 150}64" 67 "65{c 150}69" ///
72 "70{c 150}74" 77 "75{c 150}79" ///
82 "80{c 150}84" 87 "85{c 150}89" ///
92 "90{c 150}94" 97 "95+", modify
label value age age2
label define country 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 country country
// Plot
twoway (line hours age if country == 30) ///
(line hours age if country == 7) ///
(line hours age if country == 22) ///
(line hours age if country == 10) ///
, caption("{it:Source:} EU Labor Force Survey 2011, own calculations", span) ///
ytitle("Hours worked per week per capita") xtitle("Age group") ///
xlabel( 2 (5) 97, val ang(45)) ylabel(0 (5) 30) ///
legend(label(1 "United Kingdom") ///
label(2 "Germany") ///
label(3 "The Netherlands") ///
label(4 "Spain") ring(0) pos(1))
Labels:
collapse,
EU Labor Force Survey,
inlist(),
twoway line
Subscribe to:
Posts (Atom)






