// Open General Social Survey data
use wtssall year health1 parsol ///
if inlist(year, 2002, 2004, 2006, 2010, 2014, 2018) ///
using "data/GSS7218_R2.DTA", clear
// Listwise deletion
keep if !missing(health1, parsol)
// Fix health variable
recode health1 (4 5 = 1 "Poor health") ///
(3 2 1 = 0 "Good health"), gen(poorhealth)
label var poorhealth "Poor health"
// Collapse data set
collapse poorhealth [pw = wtssall], by(year parsol)
replace poorhealth = poorhealth * 100
// Create figure
foreach x of numlist 2002 2006 2010 2014 2018 {
local 2002 gs11
local 2006 gs11
local 2010 gs11
local 2014 gs11
local 2018 gs11
local `x' red
twoway (line poorhealth parsol if year == 2002, lcolor(`2002') lstyle(solid)) ///
(line poorhealth parsol if year == 2006, lcolor(`2006') lstyle(solid)) ///
(line poorhealth parsol if year == 2010, lcolor(`2010') lstyle(solid)) ///
(line poorhealth parsol if year == 2014, lcolor(`2014') lstyle(solid)) ///
(line poorhealth parsol if year == 2018, lcolor(`2018') lstyle(solid)) ///
, title({bf:`x'}, justification(left) bexpand span) ///
ytitle("% poor health") ///
xtitle("Living standard compared to parents'") ///
xlabel(1 `""Much" "better""' ///
2 `""Somewhat" "better""' ///
3 `""About" "the same""' ///
4 `""Somewhat" "worse""' ///
5 `""Much" "worse""' ///
, val) ///
name(year`x', replace) legend(off) nodraw
}
graph combine year2002 ///
year2006 year2010 ///
year2014 year2018, ///
col(2) ysize(8) altshrink ///
note("{it: Note:} Weighted with WTSSALL", size(vsmall)) ///
name(figure, replace)
Showing posts with label twoway line. Show all posts
Showing posts with label twoway line. Show all posts
Apr 24, 2020
Random graphs (145): Uncluttered line graphs
Labels:
collapse,
General Social Survey,
Random graphs,
twoway line
Feb 27, 2020
Random graphs (143): Scatterplot
// Open Allbus 1980-2016:
use id03 eastwest year using "allbus\ZA4586_v1-0-0.dta", clear
recode id03 (-1 -7 -8 -9 -13 = .), gen(topbot)
recode eastwest (2 = 1) (1 = 0), gen(east)
table year, c(mean topbot)
collapse topbot, by(year east)
twoway (scatter topbot year if east == 0, connect(L)) ///
(scatter topbot year if east == 1, connect(L)) ///
, ylabel(4 (.5) 7, format(%6.1f)) ///
xlabel(1980 1982 1986 1988 1990 1991 1992 2000 (2) 2016, alt) ///
xtitle("Survey year") ///
ytitle("Average ladder ranking") ///
legend(order(1 "West Germany" 2 "East Germany") pos(11) ring(0)) ///
note("{it: Source:} German General Social Survey 1980-2016, doi:10.4232/1.13029", justification(left) bexpand span)
Labels:
Allbus,
Random graphs,
twoway line
Sep 26, 2018
Random graphs (140): Line plot
sdmxuse data OECD, clear dataset(FAMILY) dimensions(GBR.TOTAL.FAM3) start(1960) end(2018)
destring time, gen(year)
twoway line value year, xtitle("") ytitle("% of births outside of marriage")
Labels:
OECD data,
Random graphs,
sdmxuse,
twoway line
Jul 24, 2018
Random graphs (137): Logistic regression
clear
// Generate data
set seed 1
set obs 50
gen hours = rnormal(3, 1) // Number of hours studied
gen e = rnormal(1,1)
gen questions = 2 + 2*hours + 1*e // Questions answered correctly
qui sum questions, detail
generate pass = (questions >= r(p75)) // Passing the exam
// 1) Histogram of outcome variable
twoway (histogram pass, discrete percent), ///
xlabel(0 "[0] Failed" 1 "[1] Passed") xtitle("") ///
ytitle("Percent of students") xsize(4) ysize(4) name(figure5, replace)
// 2) Scatterplot
twoway (scatter pass hours), ///
xlabel(0 (1) 5) xtitle("Hours studied for exam") ///
ytitle("Exam success") ///
ylabel(0 "[0] Failed" 1 "[1] Passed") legend(off) ///
xsize(4) ysize(4) name(figure6, replace)
// 3) Scatterplot with regression line
regress pass hours
local intercept = round(_b[_cons], .01)
local x = round(_b[hours], .01)
twoway (scatter pass hours) ///
(lfit pass hours, lpattern(solid) range(1 5)), ///
xlabel(0 (1) 5) xtitle("Hours studied for exam") ///
text(.8 2 "y = `intercept' + `x' x + e", size(large)) ///
ytitle("Exam success") ///
ylabel(0 "[0] Failed" 1 "[1] Passed") legend(off) ///
xsize(4) ysize(4) name(figure7, replace)
// 4) Logit curve
logit pass hours
predict yhat
twoway (scatter pass hours) ///
(line yhat hours, lpattern(solid) sort), ///
xlabel(0 (1) 5) xtitle("Hours studied for exam") ///
ytitle("Exam success") ///
ylabel(0 "[0] Failed" 1 "[1] Passed") legend(off) ///
xsize(4) ysize(4) name(figure8, replace)
graph combine figure5 figure6 figure7 figure8, ///
col(2) xsize(8) ysize(8) altshrink name(figures58, replace)
Labels:
logit,
predict,
Random graphs,
Simulation,
twoway histogram,
twoway lfit,
twoway line,
twoway scatter
Jul 12, 2018
Random graphs (133): Line plot
// Get divorce rate
sdmxuse data ESTAT, dataset(demo_ndivind) dimensions(A.DIVMARPCT.EU28) clear
drop freq
rename value divorce
tempname divorce
save `divorce', replace
// Get out-of-wedlock births
sdmxuse data ESTAT, dataset(demo_find) dimensions(A.NMARPCT.EFTA) clear
rename value emb
// Merge both data sets
merge 1:1 time using `divorce', nogenerate
destring time, replace
// Standardize
generate emb100 = (emb/5.6) * 100 // EMB in 1970
generate divorce100 = (divorce/11.3) * 100 // Divorce in 1970
// Plot
drop if time < 1970
twoway (line emb100 time) ///
(line divorce100 time) ///
(scatteri 410 2011 "Increase in divorce rate in Europe", msymbol(i) mlabpos(9)) ///
(scatteri 679 2016 "Increase in extramarital birth rate in Europe", msymbol(i) mlabpos(9)) ///
, ytitle("1970 = 100") xlabel(1970 (10) 2010) ylabel(100 (100) 700) xtitle("") legend(off) xsize(7)
Labels:
Random graphs,
sdmxuse,
twoway line,
twoway scatteri
Feb 9, 2018
Analysis of incomplete data using multiple imputation in Stata
This replicates MI Example 1 of Allison (2002, pp. 41-50) using Stata 14.
version 14 use "https://statisticalhorizons.com/wp-content/uploads/college.dta", clear mi set mlong mi register imputed gradrat csat private lenroll stufac rmbrd act mi impute mvn gradrat csat private lenroll stufac rmbrd act, /// add(5) burnin(500) burnbetween(200) emlog emoutput /// saveptrace(trace, replace) preserve mi ptrace describe trace mi ptrace use trace, clear // Generate regression coefficient generate b = v_y2y1 / v_y2y2
// Figure 5.1
twoway line b iter if inrange(iter, 1, 100), ///
xtitle(Iteration) ytitle(b(csat)) ///
ylabel(, format(%6.3f)) name(figure51, replace)
// Figure 5.2
tsset iter
ac v_y2y1, lags(100) ciopts(color(white)) note("") name(figure52, replace)
restore
// Table 5.3
eststo clear
foreach i of numlist 1/5 {
qui eststo: regress gradrat csat lenroll private stufac rmbrd if _mi_m == `i'
}
esttab, not se wide nostar noobs order(_cons) varlabel(_cons "Intercept") nomtitle
// Figure 5.3
mi estimate: regress gradrat csat lenroll private stufac rmbrd
Reference
Allison, Paul D. 2002. Missing Data. Sage. doi: 10.4135/9781412985079
Labels:
ac,
esttab,
mi,
Missing values,
Textbooks,
tsset,
twoway line
Dec 2, 2017
Random graphs (120): Uncluttered line plots
// Use OECD data
sdmxuse data OECD, dataset(IDD) clear attributes
// Generate country variable
kountry location, from(iso3c)
rename NAMES_STD country
// Generate year variable
destring time, gen(year)
// Keep relevant data
keep if measure == "GINI"
keep if age == "TOT"
keep if definition == "CURRENT"
keep if inlist(country, "Netherlands", "Germany", "United Kingdom", "United States")
drop if methodo == "METH2012"
sort country year
twoway (connected value year if country == "Germany") ///
(line value year if country == "Netherlands", lcolor(gs10) lpattern(solid)) ///
(line value year if country == "United Kingdom", lcolor(gs10) lpattern(solid)) ///
(line value year if country == "United States", lcolor(gs10) lpattern(solid)) ///
, legend(off) name(pa, replace) title(Germany) xtitle("") ytitle("Income inequality") ///
ylabel(, format(%6.2f))
twoway (connected value year if country == "Netherlands") ///
(line value year if country == "Germany", lcolor(gs10) lpattern(solid)) ///
(line value year if country == "United Kingdom", lcolor(gs10) lpattern(solid)) ///
(line value year if country == "United States", lcolor(gs10) lpattern(solid)) ///
, legend(off) name(pb, replace) title(Netherlands) xtitle("") ytitle("Income inequality") ///
ylabel(, format(%6.2f))
twoway (connected value year if country == "United Kingdom") ///
(line value year if country == "Germany", lcolor(gs10) lpattern(solid)) ///
(line value year if country == "Netherlands", lcolor(gs10) lpattern(solid)) ///
(line value year if country == "United States", lcolor(gs10) lpattern(solid)) ///
, legend(off) name(pc, replace) title(United Kingdom) xtitle("") ytitle("Income inequality") ///
ylabel(, format(%6.2f))
twoway (connected value year if country == "United States") ///
(line value year if country == "Germany", lcolor(gs10) lpattern(solid)) ///
(line value year if country == "Netherlands", lcolor(gs10) lpattern(solid)) ///
(line value year if country == "United Kingdom", lcolor(gs10) lpattern(solid)) ///
, legend(off) name(pd, replace) title(United States) xtitle("") ytitle("Income inequality") ///
ylabel(, format(%6.2f))
graph combine pa pb pc pd, col(2) name(fig1, replace)
Labels:
kountry,
OECD data,
Random graphs,
sdmxuse,
twoway connected,
twoway line
Oct 11, 2017
Random graphs (114): Line plot and choropleth
use ESS1-7e01, clear
// Country name variable
kountry cntry, from(iso2c) marker
rename NAMES_STD country
// Create No contract variable
gen nocontract = (wrkctra == 3) if !missing(wrkctra)
// Calculate per year and country
preserve
statsby cont = _b[_cons] contse = _se[_cons], by(country essround) clear: regress nocontract
replace cont = cont * 100 // Convert proportion into precentage
generate lb = cont - (contse * 100)
generate ub = cont + (contse * 100)
label define essround 1 "2002" 2 "2004" 3 "2006" 4 "2008" 5 "2010" 6 "2012" 7 "2014", modify
sort essround country
twoway (rarea lb ub essround) ///
(connected cont essround), ///+
by(country, legend(off) note(" " "{it:Source:} ESS 2002-2014", span) ///
title("{bf:A}", justification(left) bexpand span)) ///
xtitle("") xlabel(1/7, val ang(45)) ytitle("% no contract") ///
xsize(6) ysize(6) ///
name(figurea, replace)
restore
// Calculate average % no contracts per country
collapse nocontract, by(cntry)
replace nocontract = nocontract * 100
format nocontract %6.1f
saveold cont, replace
// Read in map data
// Source: shapefile from http://www.naturalearthdata.com/downloads/10m-cultural-vectors/
shp2dta using "maps\ne_10m_admin_0_countries", database(database) coordinates(coordinates) genid(id) replace
// Restrict coordinates to Europe
// Source: https://en.wikipedia.org/wiki/Extreme_points_of_Europe
use coordinates, clear
replace _Y = . if _Y < 36
replace _Y = . if _Y > 71 & !missing(_Y)
replace _X = . if _X < -28
replace _X = . if _X > 33 & !missing(_X)
saveold europecoordinates, replace
// Create data set with Europe internal borders
clonevar id = _ID
merge m:1 id using database, nogenerate
keep if CONTINENT == "Europe"
saveold europe, replace
// Merge map data with set with data to be plotted
use ISO_A2 NAME id using database, clear
rename ISO_A2 cntry
replace cntry = "FR" if NAME == "France" // Somehow not correct in map
replace cntry = "NO" if NAME == "Norway" // Somehow not correct in map
drop if cntry == "-99"
merge 1:1 cntry using cont, keep(matched) nogenerate
spmap nocontract using europecoordinates, id(id) xsize(6) ysize(6) ///
polygon(data(europe)) legstyle(2) clnumber(9) ///
legend(position (9) ring(0)) fcolor(Oranges) ///
legorder(hilo) ocolor(none ..) ///
title("{bf:B}", justification(left) bexpand span) ///
legtitle("{bf:% no contract, 2002-2014 averages}") legjunction({c 150}) ///
note(" " "{it:Source:} ESS 2002-2014", span) name(figureb, replace)
graph combine figurea figureb, row(1) xsize(12)
Sep 20, 2017
Random graphs (113): Cumulative line plot
clear
input year yes no
2000 3 4
2001 0 0
2002 0 0
2003 0 0
2004 0 0
2005 1 0
2006 0 0
2007 1 1
2008 0 1
2009 1 0
2010 0 1
2011 3 0
2012 1 1
2013 1 1
2014 1 2
2015 4 0
2016 8 2
2017 2 1
end
sort year
gen yes_cum = sum(yes)
gen no_cum = sum(no)
label define year 2000 `""2000" "and before""'
label val year year
twoway line yes_cum year || line no_cum year || ///
scatteri 15 2017 "No grandparent effect found", msymbol(i) mlabpos(8) || ///
scatteri 27 2017 "Grandparent effect found", msymbol(i) mlabpos(8) ||, ///
xtitle("") xlabel(2000 (5) 2015 2017, val) ///
ytitle("Number of studies published") ///
legend(off)
Labels:
Random graphs,
twoway line
Sep 7, 2017
Random graphs (112): Line plot
clear
// Open Eurostat data
unzipfile "macrodata\lfsi_pt_a.zip"
insheet using lfsi_pt_a_1_Data.csv
// Prepare variables
replace value = "." if value == ":"
destring value, gen(fixedterm)
rename time year
replace geo = "France" if geo == "France (metropolitan)"
kountry geo, from(other) stuck
rename _ISO3N_ country
kountry country, from(iso3n) to(iso2c)
rename _ISO2C_ cntry
// Select data and save
keep if year >= 2004
keep fixedterm geo year cntry
save lfs_fixedterm, replace
// Open ESS data
use essround agea mnact wrkctra pspwght cntry using ESS1-7e01, clear
// Select data
keep if essround >= 2
keep if inrange(agea, 20, 64)
keep if mnact == 1
// Prepare variables
generate ess_fixedterm = (wrkctra == 2)
replace ess_fixedterm = (ess_fixedterm * 100)
generate year = 2004 if essround == 2
replace year = 2006 if essround == 3
replace year = 2008 if essround == 4
replace year = 2010 if essround == 5
replace year = 2012 if essround == 6
replace year = 2014 if essround == 7
// Point estimates and standard errors
statsby ess_fixedterm = _b[_cons] se = _se[_cons], clear by(cntry year): regress ess_fixedterm
// Merge Eurostat data with ESS data
merge 1:1 cntry year using lfs_fixedterm
// Select countries
drop if inlist(cntry, "RU", "IL", "UA", "LV", "MK", "MT", "RO")
// Generate country name variable
kountry cntry, from(iso2c)
ren NAMES_STD country
// Calculate confidence intervals
generate lb = ess_fixedterm - 1.96 * se
generate ub = ess_fixedterm + 1.96 * se
// Plot figure
sort year
twoway (rarea ub lb year, lcolor(white)) ///
(connected ess_fixedterm year) ///
(line fixedterm year), by(country, note("")) ///
xtitle("") ytitle("Percentage of total employed (20{c 150}64 y)" "on temporary contract") ///
legend(order(2 "ESS" 1 "95% CI" 3 "Eurostat") row(1)) xlabel(2004 (2) 2014)
Random graphs (111): Line plot
clear
import excel ccf.xlsx, sheet("Completed cohort fertility") cellrange(A2:AH39) firstrow
xpose, clear varname
local y = 1935
foreach x of numlist 1/37 {
rename v`x' ccf`y'
local y = `y' + 1
}
drop in 1
reshape long ccf, i(_varname) j(cohort)
replace ccf = . if ccf == 0
rename _varname country
twoway (line ccf cohort if country == "USA") ///
(line ccf cohort if country == "Italy") ///
(line ccf cohort if country == "GermanyWest") ///
(line ccf cohort if country == "GermanyEast") ///
, xtitle(Birth cohort) ytitle("Completed cohort fertility") ///
ylabel(, format(%6.1f)) ///
legend(order(1 "USA" 2 "Italy" 3 "Germany (West)" 4 "Germany (East)"))
Labels:
Human Fertility Database,
Random graphs,
twoway line,
xpose
Jul 28, 2017
Random graphs (107): Line plot using -sdmxuse-
// Download data
sdmxuse data ESTAT, dataset(lfsi_grt_a) clear attributes
// Select desired timelines
keep if indic_em == "SERV_RT"
keep if sex == "T"
destring time, gen(year)
rename value percserv
// Plot
twoway ///
(line percserv year if geo == "UK") ///
(line percserv year if geo == "NL") ///
(line percserv year if geo == "SE") ///
(line percserv year if geo == "DE") ///
(line percserv year if geo == "FI") ///
(line percserv year if geo == "PT") ///
(line percserv year if geo == "HU") ///
(line percserv year if geo == "BG"), ///
legend(order(1 "United Kingdom" 2 "The Netherlands" ///
3 "Sweden" 4 "Germany" 5 "Finland" ///
6 "Portugal" 7 "Hungary" 8 "Bulgaria") pos(2)) ///
xtitle("") ytitle("Percentage of workforce in the service sector") ///
xlabel(1992 1995(5) 2010 2014) ///
note(" " "{it:Source:} Eurostat, lfsi_grt_a, date of extraction: 2017-07-27", span)
Labels:
Eurostat data,
Random graphs,
sdmxuse,
twoway line
Jul 10, 2017
Random graphs (101): Sparklines
import delimited une_rt_a_1_Data.csv, clear
/*
DATASET: Unemployment by sex and age - annual average [une_rt_a]
LAST UPDATE: 03.07.17 07:40:41
EXTRACTION DATE: 09.07.17 23:38:36
SOURCE OF DATA: Eurostat
*/
drop if geo == "United States"
keep if unit == "Percentage of active population"
drop sex age unit flagandfootnotes
// Generate country variable
kountry geo, from(other) stuck
ren _ISO3N_ country
kountry country, from(iso3n) to(iso2c)
ren _ISO2C_ cntry
replace cntry = "UK" if cntry == "GB"
// Fix unemployment rate
replace value = "" if value == ":" // Fix missing data indicator
destring value, replace // Convert to numeric
// Line plot
twoway (line value time), by(geo, ///
note(" " "{it: Source:} Eurostat, une_rt_a, date of extraction: 2017-07-09", span)) ///
xlabel(1990 (10) 2010) xtick(1987 (1) 2016) xmtick(1990 (5) 2015) ///
xtitle("") ytitle("Male unemployment rate, 25-74 y.") ///
name(byplot, replace)
twoway (line value time if cntry == "ES") ///
(line value time if cntry == "FR") ///
(line value time if cntry == "IE") ///
(line value time if cntry == "BE") ///
(line value time if cntry == "NL") ///
(line value time if cntry == "UK") ///
, legend(order(1 "Spain" 2 "France" 3 "Ireland" ///
4 "Belgium" 5 "Netherlands" 6 "UK")) ///
note(" " "{it: Source:} Eurostat, une_rt_a, date of extraction: 2017-07-09", span) ///
xlabel(1990 (5) 2015) xmtick(1987 (1) 2016) ///
xtitle("") ytitle("Male unemployment rate, 25-74 y.") ///
name(lineplot, replace)
// Sparklines
sparkline value time, over(geo) xlabel(1990 (5) 2015) xmtick(1987 (1) 2016) ///
ytitle("") xtitle("") title("Male unemployment rate, 25-74 y.") ///
subtitle("") ///
note(" " "{it: Source:} Eurostat, une_rt_a, date of extraction: 2017-07-09", span) ///
name(sparklines, replace)
graph combine sparklines byplot lineplot, col(1) ysize(15) xsize(6)
Labels:
Eurostat data,
kountry,
Random graphs,
sparkline,
twoway line
Jun 18, 2017
Random graphs (98): Shaded areas
import delimited une_rt_a_1_Data.csv, clear
/*
DATASET: Unemployment by sex and age - annual average [une_rt_a]
LAST UPDATE: 14.06.17 13:10:19
EXTRACTION DATE: 18.06.17 11:35:33
SOURCE OF DATA: Eurostat
*/
drop sex age unit flagandfootnotes
// Generate country variable
kountryadd "Germany (until 1990 former territory of the FRG)" to "Germany" add
kountry geo, from(other) stuck marker
ren _ISO3N_ country
kountry country, from(iso3n) to(iso2c)
ren _ISO2C_ country_str
replace country_str = "UK" if country_str == "GB"
list country_str geo
// Fix unemployment rate
replace value = "" if value == ":" // Fix missing data indicator
destring value, replace // Convert to numeric
// Plot
twoway (scatteri 12 2008 12 2012, recast(area) bcolor(gs14)) ///
(line value time if country_str == "UK") ///
(line value time if country_str == "DE") ///
(line value time if country_str == "US") ///
, legend(order(2 "UK" 3 "Germany" 4 "US") ///
pos(7) ring(0)) ///
xtitle("") ytitle("Male unemployment rate, 25-74 y.") xtick(1983(1)2016) ///
xlabel(1985(5)2015) ///
note(" " "{it: Source:} Eurostat, une_rt_a, date of extraction: 2017-06-18", span)
Labels:
Eurostat data,
kountry,
Random graphs,
twoway line,
twoway scatteri
Feb 11, 2017
Random graphs (93): Bold letters to label panels of a figure
// Read in data for Panel A
clear
input str30 time str30 sex swlb
"Marginal part-time" "Men" 7.0600364
"Marginal part-time" "Women" 7.25796249
"Substantial part-time" "Men" 6.870697962
"Substantial part-time" "Women" 6.885557977
"Full-time" "Men" 6.46086536
"Full-time" "Women" 6.20409318
end
// Create Panel A
graph bar swlb, over(sex) over(time, sort(1)) ///
ytitle("Predicted Satisfaction with" "the Work-Life Balance") ///
xsize(6) exclude0 ///
yscale(range(6 7.4)) ylabel(6 (.2) 7.4, format(%6.1f)) ///
title("{bf:A}", justification(left) bexpand span) ///
name(A, replace)
// Read in data for Panel B
clear
input str30 time str30 giis swlb
"Substantial part-time" "High gender equality" 7.172238806
"Substantial part-time" "Low gender equality" 6.292988109
"Marginal part-time" "High gender equality" 7.357329866
"Marginal part-time" "Low gender equality" 6.843323499
"Full-time" "High gender equality" 6.435347496
"Full-time" "Low gender equality" 6.143352232
end
// Recode one variable so that categories are in right order
generate gii = 0 if giis == "Low gender equality"
replace gii = 1 if giis == "High gender equality"
label define gii 0 `""Low" "gender" "equality""' ///
1 `""High" "gender" "equality""'
label val gii gii
drop giis
// Create Panel B
twoway (line swlb gii if time == "Marginal part-time") ///
(line swlb gii if time == "Substantial part-time") ///
(line swlb gii if time == "Full-time") ///
, legend(order(1 "Marginal part-time" 2 "Substantial part-time" 3 "Full-time") pos(2)) ///
ytitle("Predicted Satisfaction with" "the Work-Life Balance") ///
xlabel(0 1, val) xtitle("") ///
ylabel(6 (.2) 7.4, format(%6.1f)) ///
name(B, replace) ///
title("{bf:B}", justification(left) bexpand span)
// Combine Panel A and Panel B
graph combine A B, row(2) ysize(8) xsize(6) ycommon
Labels:
graph bar,
input,
Random graphs,
twoway line
Dec 1, 2016
Random graphs (92): Line plot
import delimited "lfsi_grt_a_1_Data.csv", clear
replace value = "" if value == ":"
destring value, gen(percserv)
twoway ///
(line percserv time if geo == "United Kingdom") ///
(line percserv time if geo == "Netherlands") ///
(line percserv time if geo == "Sweden") ///
(line percserv time if geo == "Germany (until 1990 former territory of the FRG)") ///
(line percserv time if geo == "Finland") ///
(line percserv time if geo == "Portugal") ///
(line percserv time if geo == "Hungary") ///
(line percserv time if geo == "Bulgaria"), ///
legend(order(1 "United Kingdom" 2 "The Netherlands" ///
3 "Sweden" 4 "Germany" 5 "Finland" ///
6 "Portugal" 7 "Hungary" 8 "Bulgaria") pos(2)) ///
xtitle("") ytitle("Percentage of workforce in the service sector") ///
xlabel(1992 1995(5) 2010 2014) ///
note(" " "{it:Source:} Eurostat, lfsi_grt_a, date of extraction: 2016-12-01", span)
Labels:
destring,
Eurostat data,
Random graphs,
twoway line
Apr 29, 2016
Random graphs (78): Line graphs using the -by- options
// Download data
wbopendata, clear long year(1990:2014) indicator(sh.xpd.publ.zs)
rename sh_xpd_publ_zs spending
rename iso2code country
label var spending "Health expenditure, public (% of GDP)"
// Group countries
gen lac = (inlist(country, "AR", "BS", "BB", "BZ", "BO", "BR", "CL") ///
| inlist(country, "CO", "CR", "CU", "DO", "EC", "SV", "GT") ///
| inlist(country, "GY", "HT", "HN", "JM", "MX", "NI", "PA") ///
| inlist(country, "PY", "PE", "SR", "TT", "UY", "VE"))
gen oecd = (inlist(country, "AT", "AU", "BE", "CA", "CZ", "DK", "EE") ///
| inlist(country, "FI", "FR", "DE", "HU", "IS", "IE", "IT") ///
| inlist(country, "JP", "LU", "NL", "NZ", "NO", "PL", "PT") ///
| inlist(country, "SK", "SI", "ES", "SE", "CH", "GB", "US"))
generate group = ""
replace group = "OECD" if oecd
replace group = "LAC" if lac
// Drop other countries and old data
drop if group == ""
drop if year < 1995
// Drop unnecessary variables
drop countrycode region regioncode
drop lac oecd
// Calculate average spending
sort group year country
*list, sepby(year)
bysort group year: egen avgspending = mean(spending)
*list, sepby(year)
// Calculate coefficient of variation
sort group year country
*list, sepby(year)
bysort group year: egen sdspending = sd(spending)
*list, sepby(year)
gen cvspending = (sdspending/avgspending) * 100
*list, sepby(year) abb(20)
// Calculate delta with respect to the country group
generate delta_group = (spending - avgspending) / sdspending
label var delta_group "Delta country group"
// Calculate delta with respect to the Netherlands
// Dutch spending per year
gen spending_nl = spending if country == "NL"
bysort year (spending_nl): replace spending_nl = spending_nl[_n-1] if missing(spending_nl)
// SD of Dutch spending
egen sdspending_nl = sd(spending) if country == "NL"
replace sdspending_nl = sdspending_nl[_n-1] if missing(sdspending_nl)
// Delta
generate delta_nl = (spending - spending_nl) / sdspending_nl
// Figure 5
capture drop figure5
generate figure5 = .
replace figure5 = 1 if country == "DK"
replace figure5 = 2 if country == "NL"
replace figure5 = 3 if country == "ES"
label define figure5 1 "Denmark" 2 "The Netherlands" 3 "Spain"
label value figure5 figure5
twoway line delta_nl year, by(figure5, col(1) note("") ixaxes iytitle l1title("") ///
title("Selected countries" "compared to The Netherlands")) ///
yscale(range(-3 3)) ylabel(-3 (1) 3, grid) xlabel(1995 2000 2005 2010 2014, tick) ///
l1title("") xsize(3) ysize(8) xtitle("") xmtick(1995/2014) name(figure5A, replace) ///
yline(0) nodraw
twoway line delta_group year, by(figure5, col(1) note("") ixaxes iytitle ///
title("Selected countries" "compared to OECD average") l1title("")) ///
yscale(range(-3 3)) ylabel(-3 (1) 3, grid) xlabel(1995 2000 2005 2010 2014, tick) ///
l1title("") xsize(3) ysize(8) xtitle("") xmtick(1995/2014) name(figure5B, replace) ///
yline(0) nodraw
graph combine figure5A figure5B, col(2) name(figure5, replace)
Labels:
bysort,
Random graphs,
Subscripting,
twoway line,
wbopendata
Apr 28, 2016
Random graphs (77): Line plots
// Download data
wbopendata, clear long year(1990:2014) indicator(sh.xpd.publ.zs)
rename sh_xpd_publ_zs spending
rename iso2code country
label var spending "Health expenditure, public (% of GDP)"
// Group countries
gen lac = (inlist(country, "AR", "BS", "BB", "BZ", "BO", "BR", "CL") ///
| inlist(country, "CO", "CR", "CU", "DO", "EC", "SV", "GT") ///
| inlist(country, "GY", "HT", "HN", "JM", "MX", "NI", "PA") ///
| inlist(country, "PY", "PE", "SR", "TT", "UY", "VE"))
gen oecd = (inlist(country, "AT", "AU", "BE", "CA", "CZ", "DK", "EE") ///
| inlist(country, "FI", "FR", "DE", "HU", "IS", "IE", "IT") ///
| inlist(country, "JP", "LU", "NL", "NZ", "NO", "PL", "PT") ///
| inlist(country, "SK", "SI", "ES", "SE", "CH", "GB", "US"))
generate group = ""
replace group = "OECD" if oecd
replace group = "LAC" if lac
// Drop other countries and old data
drop if group == ""
drop if year < 1995
// Drop unnecessary variables
drop countrycode region regioncode
drop lac oecd
// Calculate average spending
sort group year country
*list, sepby(year)
bysort group year: egen avgspending = mean(spending)
*list, sepby(year)
// Calculate coefficient of variation
sort group year country
*list, sepby(year)
bysort group year: egen sdspending = sd(spending)
*list, sepby(year)
gen cvspending = (sdspending/avgspending) * 100
*list, sepby(year) abb(20)
// Figure 1a
sort group year
twoway (line avgspending year if group == "OECD") ///
(line avgspending year if group == "LAC") ///
(line spending year if country == "NL"), ///
legend(order(1 "OECD regional average" 3 "The Netherlands" 2 "LAC regional average") title("") ring(0)) ///
yscale(range(0 10)) ylabel(0 (1) 10, grid) ytitle("Public health expenditure (% of GDP)") ///
xlabel(1995 2000 2005 2010 2014, tick) xmtick(1995/2014) xtitle("") name(figure1a, replace) ysize(4) nodraw
// Figure 1b
sort group year
twoway (line cvspending year if group == "OECD") ///
(line cvspending year if group == "LAC"), ///
legend(order(2 "LAC" 1 "OECD") title("Coefficient of Variation (CV)") ring(0)) ///
yscale(range(0 50)) ylabel(0 (5) 50, grid) ytitle("CV of public health expenditure") ///
xlabel(1995 2000 2005 2010 2014, tick) xmtick(1995/2014) xtitle("") name(figure2a, replace) ysize(4) nodraw
// Combined Figures
graph combine figure1a figure2a, col(1) ysize(8) name(figure1, replace)
Labels:
bysort,
egen,
Random graphs,
twoway line,
wbopendata
Apr 21, 2016
Random graphs (73): Density functions
use happy eduyrs using ESS1-6e01_0_F1.dta, clear
// Pull in outliers
qui sum eduyrs, detail
replace eduyrs = r(p99) if eduyrs >= r(p99) & !missing(eduyrs)
// Calculate density functions at different years of education
kdensity happy if eduyrs == 4, n(5000) k(gauss) bw(.8) gen(h0 d0) nograph
kdensity happy if eduyrs == 8, n(5000) k(gauss) bw(.8) gen(h1 d1) nograph
kdensity happy if eduyrs == 12, n(5000) k(gauss) bw(.8) gen(h2 d2) nograph
kdensity happy if eduyrs == 16, n(5000) k(gauss) bw(.8) gen(h3 d3) nograph
// Scale and place density functions
replace d0 = (d0*18) + 4
replace d1 = (d1*18) + 8
replace d2 = (d2*18) + 12
replace d3 = (d3*18) + 16
// Plot
twoway (line h0 d0 if inrange(h0,3,10)) ///
(line h1 d1 if inrange(h1,3,10)) ///
(line h2 d2 if inrange(h2,3,10)) ///
(line h3 d3 if inrange(h3,3,10)) ///
(lfit happy eduyrs) ///
, legend(off) xline(4 8 12 16) xlabel(0 (1) 22) ///
xtitle("Years of education") ytitle("Happiness") ///
yscale(range(3 10)) ylabel(3(1)10)
Labels:
European Social Survey,
kdensity,
Random graphs,
Textbooks,
twoway line
Mar 11, 2016
Random graphs (64): Line plots
label define wavex 1 "1991" 2 "1995" 3 "2000" 4 "2005" 5 "2010"
label val wave wavex
// Working to tight deadlines
fre y10_q4_2
generate tight = .
replace tight = 1 if y10_q4_2 == 1 | y10_q4_2 == 2
replace tight = 0 if y10_q4_2 == 3 | y10_q4_2 == 4 | y10_q4_2 == 5 | y10_q4_2 == 6 | y10_q4_2 == 7
preserve
collapse (mean) tight, by(countid wave)
replace tight = tight * 100
twoway connected tight wave, ///
by(countid, note("") title("Working to tight deadlines")) ///
ytitle("% (Almost) all of the time") ///
xtitle("") xlabel(, val) name(deadlines, replace)
restore
// Time to get job done
fre y10_q_27
generate notenoughtime = .
replace notenoughtime = 1 if y10_q_27 == 1
replace notenoughtime = 0 if y10_q_27 == 2
preserve
collapse (mean) notenoughtime, by(countid wave)
replace notenoughtime = notenoughtime * 100
twoway connected notenoughtime wave, ///
by(countid, note("{it:Source:} European Working Conditions Survey 1991{c 150}2010", span) ///
title("Not enough time to get job done")) ///
ytitle("% Yes") xtitle("") xlabel(, val) name(notenoughtime, replace)
restore
graph combine deadlines notenoughtime, col(1) ysize(10)
Subscribe to:
Posts (Atom)






















