// 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
Showing posts with label recastci. Show all posts
Showing posts with label recastci. Show all posts
Apr 23, 2020
Random graphs (144): Automatically labeling figures with letters
Labels:
center,
European Social Survey,
levelsof,
marginsplot,
Random graphs,
recastci,
tokenize
May 24, 2015
Random graphs (48): Interaction plot with overlaid data points
// Simulate data
clear
set seed 1
set obs 500
generate e = 0 + (500 - 0) * runiform() // To generate random variates over the
generate x1 = 0 + (800 - 0) * runiform() // interval [a,b), a+(b-a)*runiform()
generate x2 = round(0 + (1 - 0) * runiform()) // Binary variable
generate y = (x1 + x2 + (x1*x2) + e) / 1000
// Calculations for Aiken & West (1991) style plot
regress y c.x1##x2
qui sum x1
local x1_minsd = r(mean) - (2*r(sd))
local x1_mean = r(mean)
local x1_plusd = r(mean) + (2*r(sd))
// Calculate predicted values for plot
margins, at(c.x1 = (`x1_minsd' `x1_mean' `x1_plusd') ///
c.x2 = (0 1)) vsquish
// Plot
qui marginsplot, recastci(rarea) ciopts(color(gs10)) ///
title("Interaction plot with overlaid data points") ///
ytitle("y") ///
ylabel(, format(%6.1f)) ///
xtitle("") ///
plotopts(msymbol(none)) /// // Turn off markers
plot1opts(lpattern(longdash)) /// // Define line types here
plot2opts(lpattern(solid)) ///
addplot(scatter y x1 ///
, symbol(o) ///
xlabel(`x1_minsd' "-2 SD" /// // The addplot seems to override
`x1_mean' "Average x1" /// // the regular axis label
`x1_plusd' "+2 SD") /// // command
legend(subtitle(x2) /// // And the legend command
order(3 "x2 = 0" 4 "x2 = 1" 2 "95 % CI"))) ///
legend(pos(5) ring(0)) /// // But not completely
name(plot, replace)
Subscribe to:
Posts (Atom)

