// 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)
Showing posts with label bysort. Show all posts
Showing posts with label bysort. Show all posts
Apr 29, 2016
Random graphs (78): Line graphs using the -by- options
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 13, 2016
Random graphs (72): Dot plots with the by() option
clear
input str20 cntry bindirect blbindirect ulbindirect bdirect blbdirect ulbdirect
Austria 0.002 -0.002 0.005 0.008 -0.016 0.032
Germany 0.006 0.000 0.012 0.040 0.017 0.063
Sweden 0.001 -0.005 0.007 0.054 0.031 0.076
Netherlands 0.000 -0.006 0.006 0.039 0.017 0.061
Spain 0.005 0.002 0.009 0.016 -0.001 0.033
Italy 0.000 -0.005 0.005 0.029 0.011 0.047
France 0.001 -0.003 0.006 0.043 0.025 0.061
Denmark 0.001 0.000 0.003 0.015 0.001 0.029
Greece 0.000 -0.003 0.003 0.041 0.026 0.057
Switzerland 0.002 -0.002 0.006 0.003 -0.020 0.026
Belgium 0.000 -0.003 0.004 0.033 0.016 0.050
"Czech Republic" 0.004 -0.001 0.010 0.073 0.031 0.115
Poland -0.004 -0.010 0.002 0.034 -0.003 0.071
end
// Reshape to long format
reshape long b blb ulb, j(fx "indirect" "direct") i(cntry) string
// Encode variables
encode cntry, gen(country)
encode fx, gen(effect)
label define effect 1 "Direct effect" 2 "Indirect effect", modify
// Sort by biggest direct effect size
egen order_ = rank(b) if effect == 1, unique
labmask order_, val(country) decode
bysort country (order_): replace order_ = order_[1] // Copy value to all cases
// xrescale makes sure that x-axis fits both by() plots
twoway (dot b order_, horizontal by(effect, legend(off) note(" ") xrescale)) ///
(rspike blb ulb order_, horizontal by(effect)) ///
, ylabel(1 (1) 13, val) xscale(alt) ///
ytitle(" ") xline(0)
Apr 14, 2014
Grand-mean centering variables on a higher level
It is good practice to center predictor variables in multilevel modeling. The -center- command is extremely useful in these situations. Especially grand-mean centering is an approach that is often necessary. However, when the variables to be centered are located on the group level and groups at this higher level are of unequal sizes, centering can become inaccurate, as bigger groups get a higher weight when calculating the grand mean. A way to deal with this problem is as follows:
egen pickone = tag(group) // Create a tag variable "pickone" that has a value of 1 for a single, randomly // chosen case per group and 0 for all other cases center grouplevelpredictor if pickone // This then centers the variable "grouplevelpredictor" around the grand mean // at the group level due to the if-condition and creates a new variable // "c_grouplevelpredictor." The problem is now only that this variable has // missing values for all cases where "pickone" does not equal 1 bysort group (c_grouplevelpredictor): /// replace c_grouplevelpredictor = grouplevelpredictor[1] // This command then replaces the missing values per country with the centered, // non-missing value. This works because the data are sorted by the variable // "c_grouplevelpredictor." The centered, non-missing value will be the first // one as it is – at least in Stata – always smaller than the missing values.
Labels:
bysort,
center,
Multilevel modeling,
Subscripting
Subscribe to:
Posts (Atom)


