/* Regression 5 - Categorical Interactions */ clear all set seed 210315 set obs 60 scalar b0 = 1 // reference mean | x1==0, x2==0 scalar b1 = 0.75 // x1==1 difference in mean scalar b2 = 0.75 // x2==1 difference in mean scalar b12 = -0.5 // x1 AND x2 both 1, difference in mean scalar sigma = 0.75 // residual standard deviation generate x1 = runiformint(0,1) generate x2 = runiformint(0,1) generate mu_y = b0 + b1*x1 + b2*x2 + b12*x1*x2 generate eps = rnormal(0, sigma) generate y = mu_y + eps * Several equivalent ways of writing the model specification regress y x1##x2 * regress y i.x1##i.x2 * regress y i.x1 i.x2 i.x1#i.x2 estimates store full // test that the last three combinations (x1==1 | x2==1) are all equal regress, coeflegend // to see how to refer to each coefficient test _b[1.x1] = _b[1.x2] = _b[1.x1] + _b[1.x2] + _b[1.x1#1.x2] // coef form test 1.x1 = 1.x2 = 1.x1 + 1.x2 + 1.x1#1.x2 // varname form * Make each combination a separate category generate x = strofreal(x1) + strofreal(x2) encode x, generate(x1_x2) regress y i.x1_x2 graph twoway (scatter y x1_x2) (scatter mu_y x1_x2), /// xlabel(1 "00" 2 "10" 3 "01" 4 "11") * "nested" or "conditional" effects graph dot y, over(x1) over(x2) // x1 "within" x2, visually regress y i.x2 i.x2#i.x1 // sometimes useful, especially where some combinations of x1 and x2 don't appear estimates store conditional estimates table full conditional, stats(rmse r2 F)