* equivalence.do.  Showing that a particular OLS specification is numerically equivalent to "four averages and three subtractions". Data from Cheng and Hoekstra (2013) castle doctrine study.

clear
capture log close

use https://github.com/scunning1975/mixtape/raw/master/castle.dta, clear
xtset sid year

* Keep only states treated in 2006 and those never treated
drop if effyear==2005 | effyear==2007 | effyear==2008 | effyear==2009

* Create dummies for treatment states and post treatment period
drop 	post
gen 	post = 0
replace post = 1 if year>=2006

gen 	treat = 0
replace treat = 1 if effyear==2006

* Four averages
egen y11 = mean(l_homicide) if post==1 & treat==1
egen y10 = mean(l_homicide) if post==0 & treat==1
egen ey11 = max(y11)
egen ey10 = max(y10)

egen y01 = mean(l_homicide) if post==1 & treat==0
egen y00 = mean(l_homicide) if post==0 & treat==0
egen ey01 = max(y01)
egen ey00 = max(y00)

* Diff-in-diff equation as "four averages and three subtractions"
gen did = (ey11 - ey10) - (ey01 - ey00) // (1.798907 - 1.78607 ) - (1.196448 - 1.251846)
sum did // 0.0682359
di (1.798907 - 1.78607 ) - (1.196448 - 1.251846) // 0.068235

* OLS regression model equivalence
xi: reg l_homicide post treat i.post*i.treat, cluster(state) // 0.0682359