/* Simulations of Dummy Variables - omitted variables 1. Generate the full range of x for both group0 and group1 2. Drop some observation from each group and simulate the possible bias Created by Jin Man Lee Updated 2/17/2015 */ /* Simulation 1 : Positive Bias */ clear set obs 500 gen group = rbinomial(1, 0.5) tab group gen x = rnormal(10, 2) gen e = rnormal(0,1) gen y = 0 replace y = 10 + 0.5*x + e if group == 0 replace y = 5 + 0.5*x + e if group == 1 tw (scatter y x if group == 0 ) (scatter y x if group == 1 ) /// (lfit y x if group == 0) (lfit y x if group == 1) (lfit y x) , /// legend( lab(1 "Group 0") lab(2 "Group 1") /// lab(3 "Fitted Group 0 ") lab(4 "Fitted Group 1") /// lab(5 "Fitted all") ) drop if x < 8 & group == 0 drop if x > 12 & group == 1 tw (scatter y x if group == 0 ) (scatter y x if group == 1 ) /// (lfit y x if group == 0) (lfit y x if group == 1) (lfit y x) , /// legend( lab(1 "Group 0") lab(2 "Group 1") /// lab(3 "Fitted Group 0 ") lab(4 "Fitted Group 1") /// lab(5 "Fitted all") ) reg y x reg y x if group == 0 reg y x if group == 1 reg y i.group##c.x /* Simulation 2 : Negative Bias */ clear set obs 500 gen group = rbinomial(1, 0.5) tab group gen x = rnormal(10, 2) gen e = rnormal(0,1) gen y = 0 replace y = 10 + 0.9*x + e if group == 0 replace y = 9 + 0.3*x + e if group == 1 tw (scatter y x if group == 0 ) (scatter y x if group == 1 ) /// (lfit y x if group == 0) (lfit y x if group == 1) (lfit y x) , /// legend( lab(1 "Group 0") lab(2 "Group 1") /// lab(3 "Fitted Group 0 ") lab(4 "Fitted Group 1") /// lab(5 "Fitted all") ) drop if x > 11 & group == 0 drop if x < 10 & group == 1 tw (scatter y x if group == 0 ) (scatter y x if group == 1 ) /// (lfit y x if group == 0) (lfit y x if group == 1) (lfit y x) , /// legend( lab(1 "Group 0") lab(2 "Group 1") /// lab(3 "Fitted Group 0 ") lab(4 "Fitted Group 1") /// lab(5 "Fitted all") ) reg y x reg y x if group == 0 reg y x if group == 1 reg y i.group##c.x