// Last update: 20150327 // Author: Lu Jin /*4. Linear regression with simple error structures*/ //faq4.3 ----------------------------------------------------------------------- //How can I pool data (and perform Chow tests) in linear regression without \ //constraining the residual variances to be equal? log using uncv.log, text /* construction of artificial dataset */ sysuse auto, clear keep mpg weight gen id = _n rename mpg x1 rename weight x2 replace x2 = x2/100 label var x1 label var x2 * the equation for group 1 will be y = x1 - x2, se(u) = 15 tempfile base one qui save `base' set seed 1245 gen y = x1 - x2 - 15*invnormal(uniform()) gen group = 1 qui save `one' * the equation for group 2 will be y = x1 + x2, se(u) = 7 use `base', clear gen y = x1 + x2 + 7*invnormal(uniform()) gen group = 2 append using `one' sort group id drop id * next, the groups will made slightly unbalanced drop in -3/l * BEGINNING OF DEMONSTRATION // 2. Illustration (See the do-file and the log with the results in section 7) /*--------------Output 1------------------*/ regress y x1 x2 if group==1 /* [1] */ /*--------------Output 2------------------*/ regress y x1 x2 if group==2 /* [2] */ /*--------------Output 3------------------*/ gen g2 = (group==2) gen g2x1 = g2*x1 gen g2x2 = g2*x2 regress y x1 x2 g2 g2x1 g2x2 /* [3] */ // 4. Illustration /*--------------Output 4------------------*/ predict r, resid sum r if group==1 gen w = r(Var)*(r(N)-1)/(r(N)-3) if group==1 sum r if group==2 replace w = r(Var)*(r(N)-1)/(r(N)-3) if group==2 reg y x1 x2 g2 g2x1 g2x2 [aw=1/w] /* [4] */ // 5. The (lack of) importance of not constraining the variance /*--------------Output 5------------------*/ test g2x1 g2x2 g2 /*--------------Output 6------------------*/ regress y x1 x2 g2 g2x1 g2x2 /* [3] */ test g2x1 g2x2 g2 // 6. Another way to fit the variance-unconstrained model /*--------------Output 7------------------*/ xtset group xtgls y x1 x2 g2 g2x1 g2x2, panels(het) /* [5] */ log close exit