/* Regress 2 - Binary Variable */ clear all set seed 210313 scalar b0 = 1 // group 0 mean scalar b1 = 0.5 // group 1 mean - group 0 mean, change in mean scalar sigma = 0.75 // residual standard deviation set obs 25 generate x = runiformint(0,1) sort x // for graphing generate mu_y = b0 + b1*x generate eps = rnormal(0, sigma) generate y = mu_y + eps graph twoway (scatter y x) (line mu_y x, lpattern(dash) lcolor(red)) (pcspike y x mu_y x, lcolor(black)), /// title(Data Generating Process) legend(label(2 "true E(y)") label(3 "epsilon")) /// name(datagenbinary) twoway (scatter y x) (scatter mu_y x, mcolor(red)) /// (pcarrowi 1 0.5 1.5 0.5), /// yline(1 1.5) /// title(Interpretation as Means) /// legend(label(2 "true E(y)") label(3 "b1")) /// name(interpretation, replace) twoway (scatter y x) (line mu_y x, lpattern(dash) lcolor(red)) (lfit y x, lcolor(black)), /// title(Estimation Result) legend(label(2 "true E(y)") label(3 "estimated E(y)")) /// name(estimation) regress y x * Compared to a t-test ttest y, by(x) * When the data is NOT an indicator (not 0s & 1s) generate xc = cond(x, "A", "B") // notice order is reversed, too ttest y, by(xc) regress y xc // error encode xc, generate(xn) regress y i.xn regress y ib2.xn