Showing posts with label egen. Show all posts
Showing posts with label egen. Show all posts

Sep 18, 2019

Random graphs (142): Boxplot using twoway commands with overlaid data

use v358 v392 v585 v829 if inrange(v829, 1, 5) using "data allbus\ZA4612_v1-0-1.dta", clear

// Subjective social mobility
recode v829 (5 = 1 "Much lower status") ///
            (4 = 2 "Lower status") ///
            (3 = 3 "About equal") ///
            (2 = 4 "Higher status") ///
            (1 = 5 "Much higher status") ///
            (95 96 98 99 = .), gen(ssm)
label var ssm "Status of own job compared to father's"

// Respondent's social status ISEI
clonevar isei = v358
replace  isei =    . if inlist(isei, 0, 99)
replace  isei = v392 if missing(isei)
replace  isei =    . if inlist(isei, 0, 99)
label var isei "Respondent's ISEI"

// Father's social status ISEI
clonevar fisei = v585 
replace  fisei =    . if inlist(fisei, 0, 99)
label var fisei "Father's ISEI"

// Objective status mobility
gen osm = isei - fisei
label var osm "Objective status mobility"


// Boxplot using -graph- command graph box osm, over(ssm, descending) horizontal /// yline(0) l1title("Status of own job compared to father's" "(self-assessment)") /// name(figure1, replace)


// Boxplot using -twoway- command

// Calculate stuff for boxplot
qui bys ssm: sum osm, detail
qui bys ssm: egen min = pctile(osm), p(25) // Interquartile range
qui bys ssm: egen max = pctile(osm), p(75) // Interquartile range
qui bys ssm: egen med = median(osm)

// Plot
twoway (scatter ssm osm, msymb(o) jitter(2) mcolor(red%10)) ///
       (scatter ssm med, msymb(oh) mcolor(black)) ///
       (rspike  min max ssm, horizontal lcolor(black)), ///
        ylabel(1/5, valuelabel) xline(0) ///
        ytitle("Status of own job compared to father's" "(self-assessment)") ///
        legend(order(2 "Median" 3 "Interquartile range") ring(0) pos(11)) ///
        xtitle(Objective social mobility) name(figure2, replace)
drop min max med // Drop stuff for boxplot

Dec 27, 2016

Generating a country–year variable

use "C:\ess 1-7\ESS1-7e01.dta", clear

// Generate country-year variable
egen cyear = group(cntry essround)

// Fit three-level model
mixed happy || cntry: || cyear:, variance
estat icc

capture drop u1* u0*
predict u1 u0, reffects 
predict u1se u0se, reses 
// Plot country-level variation preserve egen pickone = tag(cntry) keep if pickone egen order_ = rank(-u1), unique labmask order_, value(cntry) gen high = u1 + (1.96 * u1se) gen low = u1 - (1.96 * u1se) twoway (rcap u1 u1 order_, dsymbol(x)) /// (rspike high low order_) , /// yline(0) xlabel(1/32, val ang(v)) /// xtitle("") /// ytitle("Country-level residuals") /// legend(off) /// name(cntry, replace) restore
// Plot country-year level variation preserve egen pickone = tag(cyear) keep if pickone egen order_ = rank(-u0), unique labmask order_, value(cyear) gen high = u0 + (1.96 * u0se) gen low = u0 - (1.96 * u0se) twoway (rcap u0 u0 order_, dsymbol(x) horizontal) /// (rspike high low order_, horizontal) , /// xline(0) ylabel(none) /// xscale(alt) /// ytitle("") /// xtitle("Country{c 150}year-level residuals") /// legend(off) /// name(cyear, replace) ysize(8) restore

May 4, 2016

Random graphs (80): Line plots using the -by- option

unzipfile output961465243413697143.zip, replace

use ESS1-6e01_1_F1.dta, clear

// Label waves
label define essround 1 "2002" 2 "2004" 3 "2006" 4 "2008" 5 "2010" 6 "2012"
label val essround essround 

// Keep only countries that contribute six observations
egen pickone = tag(cntry essround)
tab cntry essround if pickone // Look at no. of waves per country
egen numrounds = total(pickone), by(cntry) // Calculate no. of waves per country
keep if numrounds == 6

// Identify migrants
generate migrant = 1 if (brncntr == 2) & !missing(brncntr)
replace  migrant = 2 if (brncntr == 1 & (facntr == 2 | mocntr == 2)) & !missing(brncntr, facntr, mocntr)
replace  migrant = 0 if (brncntr == 1 &  facntr == 1 & mocntr == 1) & !missing(brncntr, facntr, mocntr)

label define migrant 0 "Native" 1 "First-generation migrant" 2 "Second-generation migrant"
label value migrant migrant

// Generate country identifier
kountry cntry, from(iso2c)
rename NAMES_STD country
replace country = "Slovenia" if country == "si"

// By country, year, and migrant status
collapse (mean) imbgeco imueclt imwbcnt [pweight = dweight], by(country essround migrant)

drop if migrant == .

sort country migrant essround
twoway (connected imbgeco essround if migrant == 0, ///
         by(country, note(" " "{it:Source:} European Social Survey, rounds 1{c 150}6, weighted data", span size(*.8)))) ///
       (connected imbgeco essround if migrant == 1) ///
       (connected imbgeco essround if migrant == 2) ///
      , xlabel(1/6, val ang(v)) ytitle("Immigration good for economy") ysize(8) xtitle("") name(imbgecocym, replace) ///
        legend(order(1 "Natives" ///
                     2 "First-gen." ///
                     3 "Second-gen.") ring(0) row(1)) 

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)