Showing posts with label World Values Survey. Show all posts
Showing posts with label World Values Survey. Show all posts

May 25, 2017

Random graphs (96): Categorical variables

use S003A S002 X001 C041 C039 C038 C037 if inlist(S002, 4, 5) ///
    using WVS_Longitudinal_1981-2014_stata_v_2014_11_25.dta, clear

// Declare missing values
mvdecode C041 C039 C038 C037, mv(-5 -4 -3 -2 -1)

// Factor analysis
factor C041 C039 C038 C037, pcf
alpha C041 C039 C038 C037, item
local alph = round(`r(alpha)', .01)

// Prepare variables
scores nonworkethic = mean(C041 C039 C038 C037), nv(3) 
generate workethic = 5 - nonworkethic 
gen male = (X001 == 1) if X001 != -2
drop nonworkethic X001
// Create descriptive plot foreach var of varlist C041 C039 C038 C037 { recode `var' (1 = 4 "Strongly agree") /// (2 = 3 "Agree") /// (3 = 2 "Neither agree nor disagree") /// (4 = 1 "Disagree") /// (5 = 0 "Strongly disagree"), gen(`var'_rec) numlabel `var'_rec, add mask("# ") twoway histogram `var'_rec, discrete horizontal yla(0/4, valuelabel) percent /// title("`: variable label `var''") /// ytitle("") /// name(`var', replace) nodraw drop `var'_rec } graph combine C041 C039 C038 C037, col(2) row(2) altshrink title("Work ethic is measured as the average of four items:") /// caption("In a PCA, all items load on one dimension; explained variance = 49%, Cronbach's alpha = `alph'", span) note("{it:Source:} WVS 1999-2009, pooled", span) name(figure1, replace) drop C041 C039 C038 C037
// Calculate country differences preserve statsby mean_ = _b[_cons] /// loci = (_b[_cons] - 1.96 * _se[_cons]) /// hici = (_b[_cons] + 1.96 * _se[_cons]) /// , by(S003A) clear: /// regress workethic // Sort coefficients by size egen order_ = rank(mean_), unique labmask order_, value(S003A) decode // Plot twoway (rcap mean_ mean_ order_, horizontal) /// (rspike loci hici order_, horizontal) /// , legend(off) ylabel(1/63, valuelabels ang(h) labsize(*.8)) /// xlabel(0 (1) 4, grid format(%6.1f)) name(all, replace) /// xmtick(0 (.5) 4) /// ytitle("") xtitle("Work ethic across countries") /// title("{bf:A}", justification(left) bexpand span) /// xscale(alt) ysize(10) nodraw restore // Country differences--men only preserve statsby mean_ = _b[_cons] /// loci = (_b[_cons] - 1.96 * _se[_cons]) /// hici = (_b[_cons] + 1.96 * _se[_cons]) /// , by(S003A) clear: /// regress workethic if male == 1 // Sort coefficients by size egen order_ = rank(mean_), unique labmask order_, value(S003A) decode // Plot twoway (rcap mean_ mean_ order_, horizontal) /// (rspike loci hici order_, horizontal) /// , legend(off) ylabel(1/63, valuelabels ang(h) labsize(*.8)) /// xlabel(0 (1) 4, grid format(%6.1f)) name(men, replace) /// xmtick(0 (.5) 4) /// ytitle("") xtitle("Men's work ethic across countries") /// title("{bf:B}", justification(left) bexpand span) /// xscale(alt) ysize(10) nodraw restore // Calculate gender gap preserve statsby mean_ = _b[male] /// loci = (_b[male] - 1.96 * _se[male]) /// hici = (_b[male] + 1.96 * _se[male]) /// , by(S003A) clear: /// regress workethic male // Sort coefficients by size egen order_ = rank(mean_), unique labmask order_, value(S003A) decode // Plot twoway (rcap mean_ mean_ order_, horizontal) /// (rspike loci hici order_, horizontal) /// , legend(off) ylabel(1/63, valuelabels ang(h) labsize(*.8)) /// xlabel(-.25 (.25) .5, grid format(%6.2f)) name(gendergap, replace) /// xmtick(-.25 (.1) .5) /// text(63 .5 "Men" "higher", place(sw)) /// text( 1 -.25 "Women" "higher", place(ne)) /// ytitle("") xtitle("Gender gap in work ethic") /// title("{bf:C}", justification(left) bexpand span) /// xscale(alt) ysize(10) nodraw restore graph combine all men gendergap, note(" " "{it:Source:} WVS 1999-2009, pooled", span) col(3) ysize(12) xsize(18) altshrink /// title("Work ethic in cross-national comparison", span) name(figure2, replace)

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)