/* DiD II M Perraillon */ * Card and Krueger use "H:\Teaching\Methods 2020\lectures\Week 9 difference-in-differences\DiD\code\CardKrueger1994.dta", clear rename t post describe misstable summarize * Keep nonmissing stores (closed stores are give fte=0) qui reg fte i.treated##i.post kfc roys wendys, robust gen keep =1 if e(sample) by id, sort: egen _miss1 = sum(keep) drop if _miss1 == 1 drop _* * Baseline sum treated bk kfc roys wendys if treated ==1 & post ==0 sum treated bk kfc roys wendys if treated ==0 & post ==0 list id fte treated post bk kfc roys wendys in 1/10, sep(2) nolabel * Treated sum fte if treated ==1 & post==1 scalar y_tpost = r(mean) sum fte if treated ==1 & post==0 scalar y_tpre = r(mean) * Control sum fte if treated ==0 & post==1 scalar y_cpost = r(mean) sum fte if treated ==0 & post==0 scalar y_cpre = r(mean) * DiD estimator di y_tpost - y_tpre - (y_cpost - y_cpre) ** Regression reg fte i.treated##i.post, robust * Predictive margins margins treated, at(post=(0 1)) * Marginal effects margins, dydx(treated) at(post=(0 1)) reg fte i.treated##i.post kfc roys wendys, robust reg fte i.treated##i.post if bk ==1, robust reg fte i.treated##i.post if kfc ==1, robust reg fte i.treated##i.post if roys ==1, robust reg fte i.treated##i.post if wendys ==1, robust // -- Chain type and treatment gen chaintype = 1 if bk==1 replace chaintype = 2 if kfc==1 replace chaintype = 3 if roys==1 replace chaintype = 4 if wendys==1 tab chaintype treated if post ==0 tab chaintype treated if post ==1 tabstat fte, by(chaintype) stats(N mean sd) // -- root MSE vs outcome variance and R^2 reg fte i.treated##i.post kfc roys wendys * Save root MSE scalar rmse = e(rmse) * Save outcome variance qui sum fte scalar yvar = r(sd)^2 * R^2 di 1-( (rmse^2)*(782-7)) / (yvar*(782-1)) // --- Different parametrization * Dummy for treatment is zero in the control group in both periods * Dummy for treatment is 1 in the treated group only in the post periods gen d = treated*post order d treatp list id fte treated treatp post time in 1/6, sep(2) nolabel * Original model reg fte i.treated##i.post, robust gen treatp = treated replace treatp = 0 if treated ==1 & post ==0 * Same without interaction (explicit) reg fte post treated treatp, robust reg fte i.post i.treated i.post#i.treated, robust nofvlabel gen time = 1 if post ==0 replace time = 2 if post ==1 gen stateid = 4 if treated == 1 replace stateid = 8 if treated == 0 * Time fixed effects reg fte i.time i.stateid treatp, robust // -- Checking trends use "H:\Teaching\Methods 2020\lectures\Week 9 difference-in-differences\DiD\code\Ita_exp_euro.dta", clear rename reporter country capture drop treated capture drop treated gen treated = 0 replace treated = 1 if inlist(country, 12, 18, 19, 9, 4, 10, 2) gen y = ddva1 replace y = y + rnormal(0.01,0.05) if treated ==0 sort country year list year year2 country y treated in 1/20 // -- Graph means by group and year * raw data scatter y year if treated ==1, mcolor(blue) legend(off) || /// scatter y year if treated ==0, mcolor(red) msize(small) jitter(2) graph export raw.png, replace * By year, calculate means sort year by year: egen mean_y1 = mean(y) if treated==1 by year: egen mean_y0 = mean(y) if treated==0 * Plot scatter mean_y1 year, connect(l) sort || scatter mean_y0 year, sort connect(l) /// xline(2004) xlabel(1995(1)2011, angle(vertical)) graph export trends1.png, replace * Same with a model reg y i.year##i.treated margins treated, at(year=(1995(1)2010)) vsquish marginsplot, noci xlabel(1995(2)2011) title("") xline(2004) graph export trends2.png, replace // --- Only to pre-intervention periods reg y i.year##i.treated if inlist(year, 1995, 2004) margins treated, at(year=(1995 2004)) vsquish marginsplot, noci title("") graph export twop.png, replace // -- More periods reg y c.year##i.treated if year <= 2004 predict yhat if e(sample) line yhat year if year <= 2004 & treated ==1, sort || /// line yhat year if year <= 2004 & treated ==0, sort || /// scatter y year if year <=2004, msize(vsmall) legend(off) graph export lin1.png, replace * Margins version qui reg y c.year##i.treated if year <= 2004 qui margins treated, at(year=(1995(1)2004)) marginsplot, noci xlabel(1995(2)2004) title("") graph export linmar.png, replace // -- Covariates reg y c.year##i.treated comlang log_distw if year <= 2004 margins treated, at(year=(1995(1)2004)) marginsplot, noci xlabel(1995(2)2004) title("Adjusted") graph export paradj.png, replace * at means reg y c.year##i.treated comlang log_distw if year <= 2004 margins treated, at(year=(1995(1)2004) (mean) comlang log_distw) marginsplot, noci xlabel(1995(2)2004) title("Adjusted - means") graph export paradj_mean.png, replace // --- More parametrization * Same as before reg fte i.treated##i.post, robust * Code the interaction directly gen treatp = treated replace treatp = 0 if treated ==1 & post ==0 reg fte post treated treatp, robust * Same as reg fte i.post i.treated i.post#i.treated, robust nofvlabel * State and time fixed effects gen time = 1 if post ==0 replace time = 2 if post ==1 gen stateid = 4 if treated == 1 replace stateid = 8 if treated == 0 * Time and state fixed effects reg fte i.time i.stateid treatp, robust * enter all states in the model tab stateid, gen(st) reg fte st1 st2 i.time treatp, robust noconstant