Hey everyone. I'll be teaching intro to stats for undergrads this Fall semester for policy students, and the last stats topic we cover is difference-in-differences. I begin by showing DD as a simplified regression estimator, and then I show how we can use Stata to automate the process. I want to get the counterfactual predictions produced by DD for the state of California. My current code is
This is fine, as we get the DD coefficient, the ATT. But, I want the actual counterfactual prediction line for California, our treated unit, and naturally this is not what predict returns in this case because of the interaction term.
Getting the counterfactual prediction line is simple enough if we re-express DD as
, but I figured there's got to be another way to derive the counterfactual using the interaction term approach (since that's what the students who choose to use DD for their papers would be doing anyways, instead of needing to fiddle with reshape and cnsreg). How might I do this? I must confess I've never had to do this before in Stata.
Code:
clear * import delim "https://raw.githubusercontent.com/OscarEngelbrektson/SyntheticControlMethods/master/examples/datasets/smoking_data.csv", clear g treat = cond(state=="California",1,0) g post = cond(year > 1988,1,0) egen id = group(state) xtset id year, y reg cig i.treat##i.post, vce(cl id) predict cf
Getting the counterfactual prediction line is simple enough if we re-express DD as
Code:
clear * cls import delim "https://raw.githubusercontent.com/OscarEngelbrektson/SyntheticControlMethods/master/examples/datasets/smoking_data.csv", clear egen id = group(state) keep cig id year reshape wide cig, i(year) j(id) order cigsale3, a(year) egen ymean = rowmean(cigsale1-cigsale39) keep year cigsale3 ymean constraint define 1 ymean = 1 cnsreg cigsale3 ymean if year < 1989, constraints(1) predict cf line cig cf year , lcol(black blue) // if year < 1989
Comment