Showing posts with label Subscripting. Show all posts
Showing posts with label Subscripting. Show all posts

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)

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.