use ZA5900_v3-0-0.dta, replace
renvars, lower // Switch variable names to lower case
// Fix country variable
generate cntry = c_alphan
replace cntry = "GB" if cntry == "GB-GBN"
replace cntry = "DE" if cntry == "DE-E" | cntry == "DE-W"
replace cntry = "BE" if cntry == "BE-BRU" | cntry == "BE-WAL" | cntry == "BE-FLA"
// Generate country name variable
kountry cntry, from(iso2c)
encode NAMES_STD, gen(country)
drop NAMES_STD
// Happiness variable
recode v55 (1 = 6) (2 = 5) (3 = 4) (4 = 3) (5 = 2) (6 = 1) (7 = 0) (0 8 9 = .), gen(lsat)
label define lsat 0 "Completely unhappy" ///
1 "Very unhappy" ///
2 "Fairly unhappy" ///
3 "Neither happy nor unhappy" ///
4 "Fairly happy" ///
5 "Very happy" ///
6 "Completely happy"
label val lsat lsat
label var lsat "Happiness"
// Plot means by country
preserve
statsby mean_ = _b[_cons] ///
loci = (_b[_cons] - 1.96 * _se[_cons]) ///
hici = (_b[_cons] + 1.96 * _se[_cons]) ///
, by(country) total clear: ///
regress lsat
replace country = 1000 if country == .
label define country 1000 "{bf: Total}", modify
egen order_ = rank(mean_), unique
labmask order_, value(country) decode
twoway (rcap mean_ mean_ order_, horizontal) ///
(rspike loci hici order_, horizontal) ///
, legend(off) ylabel(1/41, valuelabels ang(h) labsize(*.8)) ///
xlabel(3.5 (.5) 5.0, grid format(%6.1f)) name(lsat, replace) ///
xmtick(3.5 (.25) 5.0, grid) ///
ytitle("") xtitle("Average happiness") xscale(alt) ysize(8) ///
note(" " ///
"{it:Note:} Happiness ranges from 0 ('Completely unhappy') to 6 ('Completely happy')" ///
"{it:Source:} ISSP 2012, doi:10.4232/1.12339" , span size(*.8))
restore
Showing posts with label preserve. Show all posts
Showing posts with label preserve. Show all posts
Apr 30, 2016
Random graphs (79): Means with confidence intervals
Dec 18, 2014
Random graphs (40): Line plot
clear
// Read in Table
input str4 cntry P1970 P1991 P1998 P2008 S1970 S1991 S1998 S2008 T1970 T1991 T1998 T2008
EU15 16.2 7.6 4.5 3.4 37.4 28.6 26.6 23.2 54 73 68.9 73.5
DE 8 3.4 2.5 2.1 48 35 30.4 25.3 44 62 67.1 72.5
FI 20 8 6.3 4.8 34 29 27.8 25.6 46 63 65.9 69.6
NL 6 4 3.6 3 36 24 19.9 16.7 58 72 76.5 80.3
SE 8 4 3.1 2.2 39 28 25.1 22.7 53 68 71.8 75.1
UK 3 2.1 1.9 1.5 42 25 . 17.7 55 73 75.3 80.7
CZ 13.5 10 5.6 3.5 49.4 45.9 41.4 38 37 44 53 58.6
end
// Crazy stuff to bring it into shape
preserve
drop S1970-T2008
reshape long P, i(cntry) j(year)
tempfile p
save `p', replace
restore
preserve
drop P1970-P2008 T1970-T2008
reshape long S, i(cntry) j(year)
tempfile s
save `s', replace
restore
drop P1970-S2008
reshape long T, i(cntry) j(year)
merge 1:1 cntry year using `p'
drop _merge
merge 1:1 cntry year using `s'
// Plot
twoway (scatter P year if cntry == "EU15", connect(L)) ///
(scatter S year if cntry == "EU15", connect(L)) ///
(scatter T year if cntry == "EU15", connect(L)) ///
, ytitle("Percentage of EU-15 workforce") ///
xtitle("") ///
note(" " "{it: Source:} Mau and Verwiebe 2010, pp. 153{c 150}4", span) ///
legend(label(1 "{bf:Primary sector:}" "Agriculture") ///
label(2 "{bf:Secondary sector:}" "Industry and" "construction") ///
label(3 "{bf:Tertiary sector:}" "Services")) ///
xlabel(1970 1991 1998 2008)
Labels:
input,
preserve,
Random graphs,
reshape,
tempfile,
twoway scatter
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)
Labels:
_skip(#),
collapse,
decode,
graph combine,
kountry,
log(),
mlabel,
preserve,
Random graphs,
round(),
tempfile,
twoway lfit,
twoway scatter,
wbopendata,
World Values Survey,
ysize
Subscribe to:
Posts (Atom)


