* Simple difference in mean outcome and checks for covariate balance

use https://github.com/scunning1975/mixtape/raw/master/titanic.dta, clear
numlabel, add

gen female=(sex==0)
label variable female "Female"
gen male=(sex==1)
label variable male "Male"

gen     s=1 if (female==1 & age==1) // adult female
replace s=2 if (female==1 & age==0) // child female
replace s=3 if (female==0 & age==1) // adult male
replace s=4 if (female==0 & age==0) // child male

gen 	d=1 if class==1
replace d=0 if class!=1

summarize survived if d==1 // 0.6246154, or 62.5 percent of first class survived
gen ey1=r(mean)
summarize survived if d==0 // 0.2707889, or 27.1 percent of other classes survived
gen ey0=r(mean)

gen sdo=ey1-ey0
su sdo // 0.3538265, or 35.4 percentage point higher survival if seated in first class versus any other class.

regress survived d // 0.3538265, the same number as before.

* Check for imbalance in women (female=1) and children (age=1)
bysort d: summarize age
bysort d: summarize female

* Check for imbalance by strata
tab s, gen(s)
bysort d: summarize s1-s4

exit