clear // Install rcall command *github install haghish/rcall, stable // Clear memory of R session rcall clear //#install.packages("essurvey"); rcall: library(essurvey) // Download data rcall: download_rounds(c(9), /// ess_email = "REGISTERED E-MAIL ADDRESS HERE", /// output_dir = "data", /// format = 'stata') // Open data set local vars cntry gndr polintr dweight use `vars' using "data/ESS9/ESS9e01_2.dta", clear // Variables generate interest = (inlist(polintr, 1, 2)) replace interest = . if missing(polintr) generate female = (gndr == 2) // Calculate gender gap per country tempname foo postfile `foo' str50 cntry men women gap gap_se using deleteme.dta, replace levelsof cntry, local(cntry) foreach i of local cntry { qui regress interest i.female [pw = dweight] if cntry == `"`i'"' qui margins i.female, post local men = _b[0.female] * 100 local women = _b[1.female] * 100 qui regress interest i.female [pw = dweight] if cntry == `"`i'"' qui margins r.female, post local gap = _b[r1vs0.female] * -100 local gap_se = _se[r1vs0.female] * -100 post `foo' ("`i'") (`men') (`women') (`gap') (`gap_se') } postclose `foo' // Open data set with gender gap per country use deleteme.dta, clear kountry cntry, from(iso2c) egen country = rank(gap), unique labmask country, value(NAMES_STD) generate ub = gap + 1.96 * gap_se generate lb = gap - 1.96 * gap_se generate y1 = -women generate y2 = 0 generate y3 = -women + men generate x1 = country - 0.2 generate x2 = country + 0.2 mylabels 0(20)60, myscale(-@) local(show) local spaces = 30 * " " twoway (scatter y1 country, msymbol(p) yaxis(1 2)) /// (rbar y1 y2 x1, blcolor(gs10%40) bfcolor(gs10%40) barwidth(0.4)) /// (rbar y1 y3 x2, blcolor(gs5%40) bfcolor(gs5%40) barwidth(0.4)) /// (rbar gap y2 x2, blcolor(gs3%40) bfcolor(gs3%40) barwidth(0.4)) /// (rspike ub lb x2) /// , ylabel(`show', grid ang(h)) /// ylabel(0(10)40, grid ang(h) axis(2)) /// ytitle("% interested in politics") /// ytitle("`spaces' Gender gap", axis(2)) /// xlabel(1/19, noticks ang(45) valuelabel labsize(small)) /// legend(order(2 "Men" 3 "Women" 4 "Gender gap" 5 "Gender gap 95% CI") /// col(4) pos(12) ring(0)) name(gendergap, replace)
Showing posts with label Random graphs. Show all posts
Showing posts with label Random graphs. Show all posts
May 19, 2020
Random graphs (146): Bar graph
Apr 24, 2020
Random graphs (145): Uncluttered line graphs
// 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)
Labels:
collapse,
General Social Survey,
Random graphs,
twoway line
Apr 23, 2020
Random graphs (144): Automatically labeling figures with letters
// Open ESS round 5
use agea stflife cntry if inlist(cntry, "DE", "GB", "NL", "SE") using "ESS5e03_4.dta", clear
// Country variable
kountry cntry, from(iso2c)
rename NAMES_STD country
// Restrict to complete cases
keep if !missing(agea, stflife, cntry)
// Center age
qui sum agea, detail
replace agea = `r(p99)' if agea > `r(p99)' & !missing(agea)
bys country: center agea, gen(age)
drop agea
label var age "Age (centered)"
// Letters
tokenize "`c(ALPHA)'"
local i 1
levelsof country, local(country)
foreach z of local country {
qui sum age if country == `"`z'"', detail
foreach x of numlist 10 25 50 75 90 {
local x`x': di %9.1f r(p`x')
di `x`x''
}
qui regress stflife c.age##c.age if country == `"`z'"'
qui margins, at(age = (`x10' `x25' `x50' `x75' `x90'))
marginsplot, title(`"{bf:``i''} `z'"') ///
ytitle("Life satisfaction" "(predicted)") ///
xtitle("Age (percentiles)") ///
xlabel(`x10' "10th" ///
`x25' "25th" ///
`x50' "50th" ///
`x75' "75th" ///
`x90' "90th") ///
ylabel(, format(%6.1f)) ///
recastci(rarea) ciopts(color(gs10)) ///
name("``i''", replace) nodraw
local ++i
}
graph combine A B C D, col(2) ycommon
Labels:
center,
European Social Survey,
levelsof,
marginsplot,
Random graphs,
recastci,
tokenize
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
Nov 2, 2018
Random graphs (141): Heat map
matrix al = (.27, .09 ,-.01 \ .10, -.06, -.13 \ -.04 ,-0.17, -0.30)
plotmatrix, mat(al) split(-.4(0.05).4) ///
freq formatcells(%6.2f) ///
legend(title("Wear and tear" "(allostatic load)") ///
col(1) ///
order(1 "Less wear and tear" 5 "Average" 8 "More wear and tear") ) ///
xlabel(1 "Working" 2 "Intermediate" 3 "Higher") ///
ylabel(0 "Working" -1 "Intermediate" -2 "Higher") ///
xscale(alt) yscale(rev) ///
xtitle(Participant's social class) ///
ytitle(Parental class) ///
note(" " "Numbers denote the average allostatic load in that group. The overall average across all classes is 0 (SD = 1)", span)
Labels:
plotmatrix,
Random graphs
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
Aug 3, 2018
Random graphs (139): Forest plot
clear
input str50 study year r n
"Duncan et al." 1972 .573 14347
"Chamberlain & Griliches" 1977 .508 584
"Hauser et al." 1982
"Hauser" 1984 .404 518
"Hauser & Mossel" 1985 .404 518
"Hauser & Sewell" 1986 .459 532
"Hauser & Wong" 1989 .57 164
"Dronkers" 1993 .6 244
"Scarr & Weinberg" 1994 .32 105
"Hauser et al." 1999 .421 1100
"Ashenfelter & Zimmerman" 1997 .51 143
"Sieben & de Graaf" 2001 .515 2200
"Sieben & de Graaf" 2003 .342 152618
"Conley & Glauber" 2007 .576 1777
"Beenstock" 2008 .435 6926
"Conley" 2008 .578 1777
"Conley & Glauber" 2008 .627 1127
"Mazumder" 2008 .602 6097
"Olneck" 1977 .549 346
"Lee" 2009 .478 4561
"Ermisch & Pronzanto" 2010 .37 68957
"Bjorklund & Salvanes" 2011 .39 34476
"Lindahl" 2011 .411 9864
"Mazumder" 2011 .665 671
"Adermon" 2012 .424 338222
"Schnitzlein" 2014 .656 1480
"Andrade" 2016 .122 1632366
"Bredtman & Smith" 2016 .327 3087
"Pfeffer et al." 2016 .46 5215
end
generate z = atanh(r) // r-to-z = inverse hyperbolic tangent
generate sez = sqrt(1/(n - 3))
admetan z sez
display _newline ///
" Pooled estimate of r = " tanh(r(eff)) _newline ///
"Lower Limit of 95% CI = " tanh(r(eff) - (1.96 * r(se_eff))) _newline ///
"Upper Limit of 95% CI = " tanh(r(eff) + (1.96 * r(se_eff)))
// Prepare data for -forestplot-
generate _USE = 1
// Generate CI's for r
generate lb = tanh(_LCI)
generate ub = tanh(_UCI)
// Generate study labels for -forestplot-
generate _LABELS = study + " (" + string(year, "%02.0f") + ")"
label var n "Sample size"
// Add effect size to data set
local new = _N + 1
set obs `new'
replace _LABELS = "{bf:Overall}" if _n == _N
replace r = tanh(r(eff)) if _LABELS == "{bf:Overall}"
replace lb = tanh(r(eff) - (1.96 * r(se_eff))) if _LABELS == "{bf:Overall}"
replace ub = tanh(r(eff) + (1.96 * r(se_eff))) if _LABELS == "{bf:Overall}"
replace _USE = 5 if _LABELS == "{bf:Overall}"
// Forest plot
forestplot r lb ub, effect("Correlation") rcol(n) leftjustify nowt nonull xlabel(0 .1 .2 .3 .4 .5 .6 .7)
Labels:
admetan,
forestplot,
Meta-analysis,
Random graphs
Jul 24, 2018
Random graphs (138): Functions
twoway (function y = 1 + 1*x, range(0 5)), ///
ylabel("") xlabel("") xsize(4) ysize(4) ///
title(Linear function) name(figure9a, replace)
twoway (function y = exp(x) / (1 + exp(x)), range(-5 5)), ///
ylabel("") xlabel("") xsize(4) ysize(4) ///
title(Logistic function) name(figure9b, replace)
graph combine figure9a figure9b, col(2) xsize(8) ysize(4) ///
altshrink name(figure9, replace)
Labels:
Random graphs,
twoway function
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
Random graphs (136): Linear probability model
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) Heteroskedastic residuals
regress pass hours
predict resid, resid
twoway (scatter resid hours), ///
xlabel(0 (1) 5) xtitle("Hours studied for exam") ///
yline(0) name(figure7a, replace)
graph combine figure5 figure6 figure7 figure7a, ///
col(2) xsize(8) ysize(8) altshrink name(figures57, replace)
Labels:
predict,
Random graphs,
Simulation,
twoway histogram,
twoway scatter
Random graphs (135): Scatterplot with OLS 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
// 1) Basic scatterplot
twoway (scatter questions hours), ///
xlabel(0 (1) 5) xtitle("Hours studied for exam") ///
ytitle("Number of questions answered correctly") ///
name(figure1, replace) ylabel(0 (5) 15) xsize(4) ysize(4)
// 2) Scatterplot with regression line
twoway (scatter questions hours) ///
(lfit questions hours), ///
xlabel(0 (1) 5) xtitle("Hours studied for exam") ///
ytitle("Number of questions answered correctly") ///
ylabel(0 (5) 15) legend(off) name(figure2, replace) xsize(4) ysize(4)
// 3) Scatterplot with regression line and equation
regress questions hours
local intercept = round(_b[_cons], .1)
local x = round(_b[hours], .1)
twoway (scatter questions hours) ///
(lfit questions hours), ///
xlabel(0 (1) 5) xtitle("Hours studied for exam") ///
text(14 2 "y = `intercept' + `x' + e", size(large)) ///
ytitle("Number of questions answered correctly") ///
ylabel(0 (5) 15) legend(off) name(figure3, replace) xsize(4) ysize(4)
// 4) Scatterplot with regression line and equation and labels for components
twoway (scatter questions hours) ///
(lfit questions hours, lpattern(solid) range(0 5)) ///
(function y = 1.6 + 2.5, range(1 2)) ///
(function y = 2, range(4.1 6.6) horizontal lpattern(solid) lcolor(red)) ///
(scatteri 1.6 0 (3) "Intercept", msymbol(o) mlabcolor(red)) ///
(scatteri 5 2 "Slope", msymbol(i) mlabcolor(red)), ///
xlabel(0 (1) 5) xtitle("Hours studied for exam") ///
text(14 2 "y = `intercept' + `x' x + e", size(large)) ///
ytitle("Number of questions answered correctly") ///
ylabel(0 1.6 5 10 15) legend(off) name(figure4, replace) xsize(4) ysize(4)
graph combine figure1 figure2 figure3 figure4, col(2) xsize(8) ysize(8) altshrink name(figures14, replace)
Jul 22, 2018
Random graphs (134): Chi-squared function
twoway (function y = (x^(1 -1)*exp(-1*x/2))/((2^(1.0))*exp(lngamma(1.0))), range(0 20)) ///
(function y = (x^(1.5-1)*exp(-1*x/2))/((2^(1.5))*exp(lngamma(1.5))), range(0 20)) ///
(function y = (x^(2.5-1)*exp(-1*x/2))/((2^(2.5))*exp(lngamma(2.5))), range(0 20)) ///
(function y = (x^(5-1)*exp(-1*x/2))/((2^(5))*exp(lngamma(5))), range(0 20)) ///
(scatteri .5 0.1 "{it:df} = 2", msymbol(i) mlabpos(3)) ///
(scatteri .24 1.7 "{it:df} = 3", msymbol(i) mlabpos(3)) ///
(scatteri .15 5.0 "{it:df} = 5", msymbol(i) mlabpos(3)) ///
(scatteri .10 10.0 "{it:df} = 10", msymbol(i) mlabpos(3)) ///
, xtitle(Chi-squared) ytitle("") ylabel(, format(%6.1f)) legend(off) name(figu, replace) //ysize(6) xsize(6)
// Also search chidemo
Labels:
Random graphs,
twoway function,
twoway scatteri
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
May 16, 2018
Random graphs (132): Distributions
// Open ESS 2006 data
use ESS3e03_6.dta, clear
// Prepare variables
kountry cntry, from(iso2c)
rename NAMES_STD country
recode agea (999 = .)
recode ygcdbyr (6666/9999 = .)
recode inwyye (9999 = .)
recode gndr (9 = .)
// Generate relevant variables
generate yearborn = inwyye - agea
gen grandparent = ygcdbyr - yearborn
//Plot figure
twoway kdensity grandparent if gndr == 1, by(country, title(Men , justification(left) span) note("")) xline(50) ylabel(0 .05 .10, format(%6.2f)) ytitle("Density") xtitle("") /*xtitle("Age at grandparenthood")*/ name(men, replace) nodraw
twoway kdensity grandparent if gndr == 2, by(country, title(Women, justification(left) span) note("")) xline(50) ylabel(0 .05 .10, format(%6.2f)) ytitle("Density") xtitle("Age at grandparenthood") name(women, replace) nodraw
graph combine men women, col(1) ysize(8) note("{it:Note:} Vertical line indicates age 50", size(vsmall))
Apr 19, 2018
Random graphs (131): Scatterplot
sdmxuse data ESTAT, clear dataset(crim_pris_cap) dimensions(A.PRIS_ACT_CAP.P_HTHAB.)
drop unit indic_cr freq
rename value prison
tempfile x
replace geo = "UK" if geo == "UKC-L"
save `x', replace
sdmxuse data ESTAT, clear dataset(spr_exp_sum) start(2008) end(2015) dimensions(A.TOTALNOREROUTE.PC_GDP..)
drop spdeps unit freq
rename value spending
merge 1:1 geo time using `x', keep(match) nogenerate
replace geo = "GR" if geo == "EL"
kountry geo, from(iso2c)
rename NAMES_STD country
regress prison spending if time == "2015"
local r2 = round(`e(r2)', .01) * 100
twoway (scatter prison spending if time == "2015", mlabel(country)) ///
(lfit prison spending if time == "2015"), ///
ytitle("Prison population per 100,000 inhabitants") ///
xtitle("Total social spending as % of GDP") ///
legend(order(2 "Linear fit, explained variance = `r2'%") pos(1) ring(0)) ///
name(f2015, replace)
Labels:
Eurostat data,
kountry,
Random graphs,
sdmxuse,
twoway scatter
Mar 28, 2018
Random graphs (130): Bar graph
clear
input str30 journal swb psw
"{it:ASR}" 31 3
"{it:AJS}" 13 1
"{it:ESR}" 55 1
`"{it:"Demography"}"' 62 7
`"{it:"Population Studies"}"' 35 3
`"{it:"Demographic Research"}"' 42 2
"{it:JMF}" 154 39
end
graph hbar swb psw, over(journal, sort(1)) ///
legend(order(1 "Subjective wellbeing" 2 "Psychosocial wellbeing") ring(0) pos(2)) ///
ytitle("Number of articles on Google Scholar using the term") name(swbvspsw, replace) ///
note(" " "{it:Source:} Google Scholar, March 28, 2018", span)
Labels:
graph bar,
Random graphs
Mar 23, 2018
Random graphs (129): Offsetting markers
// Open Allbus 2016
use eastwest hs01 id02 using ZA5250_v2-0-0.dta, clear
// Region
recode eastwest (1 = 0 "West Germany") (2 = 1 "East Germany"), gen(east)
label var east "East Germany"
// Subjective social class
recode id02 (-50/-7 = .) ///
( 1 = 0 "Lower class") ///
( 2 = 1 "Working class") ///
( 3 = 2 "Middle class") ///
( 4 5 = 3 `" "Upper" "(middle)" "class" "'), gen(class)
label var class "Subjective social class"
// Health
recode hs01 (-9 = .) (5 = 0 "Bad") (4 = 1 "Less good") (3 = 2 "Satisfactory") ///
(2 = 3 "Good") (1 = 4 "Very good"), gen(health)
label var health "Self-rated health"
// Model
regress health i.class##i.east //[pw = wghtpew]
// Plot using marginsplot
margins class#east, saving(myfile, replace)
marginsplot, legend(pos(4) ring(0)) name(marginsplot, replace) title("") ///
ytitle("Predicted self-rated health") ylabel(, format(%6.1f))
// Plot using twoway with overlay
use myfile, clear
// Create offset
clonevar _mx = _m1
replace _mx = cond(_m2 == 2, _mx - 0.1, _mx + 0.1)
// Plot using marginsplot
twoway (rcap _ci_lb _ci_ub _m1 if _m2==0, sort) ///
(rcap _ci_lb _ci_ub _mx if _m2==1, sort) ///
(connected _margin _m1 if _m2==0, msymbol(oh)) ///
(connected _margin _mx if _m2==1, msymbol(o)) ///
, title("") ///
ytitle("Predicted self-rated health") xla(, valuelabel) ///
legend(pos(4) ring(0) col(1) order(3 "West Germany" 4 "East Germany")) ///
name(twoway, replace) ylabel(, format(%6.1f))
Labels:
Allbus,
margins,
marginsplot,
Random graphs,
twoway connected,
twoway rcap
Mar 22, 2018
Random graphs (128): Comparing distributions
// Open cumulated Allbus 1980-2014
use ost_west v152 year using ZA4584_v1-0-0.dta, clear
// West Germany only
keep if ost_west == 1
// Subjective SES
recode v152 (0 = .) (97 98 99 = .), gen(topbot)
label var topbot "Subjective SES"
// Drop years with missing data
drop if inlist(year, 1984, 1994, 1996, 1998)
// Drop early years
drop if year < 1998
label drop year // Drop label
// Generate average subjective SES per year
bysort year: egen avg = mean(topbot)
// Figure
tabplot topbot year, yasis xasis horizontal percent barw(1) bfcolor(none) //
xtitle("") subtitle("") note("") //
addplot(connect avg year, sort msymbol(O) mcolor(black) msize(large))
Labels:
Allbus,
Random graphs,
tabplot
Mar 9, 2018
Random graphs (127): Density plots
clear
input str25 inout days
"Decision" 15
"Decision" 15
"Decision" 25
"Decision" 1
"Decision" 135
"Decision" 12
"Decision" 184
"Decision" 40
"Decision" 12
"Decision" 11
"Decision" 44
"Decision" 35
"Decision" 87
"Decision" 101
"Decision" 196
"Decision" 51
"Decision" 120
"Decision" 7
"Decision" 84
"Decision" 103
"Decision" 2
"Decision" 175
"Decision" 29
"Decision" 384
"Decision" 56
"Decision" 49
"Decision" 28
"Decision" 40
"Decision" 140
"Decision" 3
"Decision" 50
"Decision" 5
"Decision" 85
"Decision" 43
"Decision" 152
"Decision" 19
"Decision" 11
"Decision" 103
"Decision" 27
"Decision" 133
"Decision" 64
"Decision" 68
"Decision" 113
"Decision" 159
"Decision" 19
"Decision" 82
"Decision" 44
"Decision" 2
"Decision" 75
"Decision" 1
"Decision" 90
"Decision" 10
"Decision" 110
"Reviewed" 21
"Reviewed" 1
"Declined" 0
"Reviewed" 30
"Reviewed" 0
"Declined" 0
"Reviewed" 0
"Declined" 0
"Declined" 1
"Reviewed" 6
"Declined" 0
"Declined" 0
"Declined" 1
"Declined" 1
"Reviewed" 47
"Declined" 0
"Declined" 0
"Declined" 1
"Reviewed" 1
"Declined" 0
"Reviewed" 2
"Reviewed" 9
"Declined" 2
"Reviewed" 1
"Reviewed" 7
"Reviewed" 28
"Reviewed" 2
"Declined" 0
"Reviewed" 16
"Reviewed" 89
"Reviewed" 4
"Declined" 1
"Reviewed" 3
"Declined" 4
"Reviewed" 38
"Reviewed" 1
"Reviewed" 41
"Reviewed" 28
"Reviewed" 51
"Declined" 0
"Reviewed" 10
"Reviewed" 59
"Declined" 1
"Reviewed" 13
"Declined" 0
"Reviewed" 6
"Reviewed" 12
"Reviewed" 45
"Reviewed" 25
"Reviewed" 4
"Reviewed" 8
"Reviewed" 0
"Reviewed" 3
"Reviewed" 7
"Reviewed" 64
"Reviewed" 49
"Reviewed" 4
"Reviewed" 57
"Reviewed" 1
"Reviewed" 1
"Reviewed" 0
"Reviewed" 1
"Reviewed" 42
"Reviewed" 10
"Declined" 0
"Reviewed" 49
"Reviewed" 4
"Reviewed" 3
"Declined" 0
"Reviewed" 16
"Reviewed" 46
"Reviewed" 37
"Reviewed" 2
"Reviewed" 14
"Reviewed" 7
"Reviewed" 21
"Reviewed" 3
"Reviewed" 34
"Reviewed" 14
"Reviewed" 4
"Reviewed" 29
"Reviewed" 20
end
// Using label rather than order for the legend labels allows using a line break in the legend
twoway (kdensity days if inout == "Decision", lcolor(red)) ///
(kdensity days if inout == "Reviewed" | inout == "Declined", lcolor(green) lpattern(solid)) ///
(kdensity days if inout == "Reviewed", lcolor(midgreen) lpattern(solid)), ///
legend(label(1 "Waiting times to receive journal decisions") ///
label(2 "Waiting times to respond to review request") ///
label(3 "Waiting times to respond to review request" "(excluding when I declined to review)") ///
pos(2) ring(0)) ///
xlabel(0 25 50 75 100 200 300 400) xtitle(Days) ///
ytitle("Kernel density estimate") ///
title("How long journals wait for my reviews of their manuscripts" "{it:vs.} how long I wait for journals' decisions on my manuscripts", span)
Labels:
Random graphs,
twoway kdensity
Feb 19, 2018
Random graphs (126): Turning tables into figures
sdmxuse data ESTAT, dataset(lfsa_epgar) start(2010) end(2010) clear
keep if inlist(geo, "AT", "BE", "BG", "CY", "CZ", "DK", "EE", "FI") ///
| inlist(geo, "FR", "DE", "EL", "HU", "IE", "LT", "NL", "NO") ///
| inlist(geo, "PL", "PT", "ES", "SE", "CH", "UK")
keep if age == "Y_GE15"
replace geo = "GR" if geo == "EL"
kountry geo, from(iso2c)
rename NAMES_STD country
drop if sex == "T"
replace sex = "Men" if sex == "M"
replace sex = "Women" if sex == "F"
encode reason, gen(reasonno)
label var reasonno "Reasons for part-time work"
label define reasonno 1 "Care activities" ///
2 "Other personal reasons" ///
3 "Own illness, disability" 4 "Education, training" ///
5 "Could not find full-time job" 6 "Other", modify
replace value = value * 10
expand value
drop if missing(value)
tabplot reasonno country, by(sex, ///
note("{it:Note:} Bars and numbers indicate percentage of part-time workers per country" ///
"{it:Source:} Eurostat, lfsa_egpar, data refer to 2010.")) ///
percent(sex country) ///
showval(mlabsize(tiny) format(%6.0f)) xtitle("") ///
xlabel(, angle(vertical) labsize(small)) name(figure2, replace)
Labels:
Eurostat data,
expand,
kountry,
Random graphs,
sdmxuse,
tabplot
Subscribe to:
Posts (Atom)




















