------------------------------------------------------------------------------------------------------------- name: log: C:\Users\danie\Desktop\uncv.log log type: text opened on: 22 Jun 2023, 16:09:09 . . /* construction of artificial dataset */ . sysuse auto, clear (1978 automobile data) . keep mpg weight . gen id = _n . rename mpg x1 . rename weight x2 . replace x2 = x2/100 variable x2 was int now float (74 real changes made) . 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 (1978 automobile data) . 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 (3 observations deleted) . . . * 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] */ Source | SS df MS Number of obs = 74 -------------+---------------------------------- F(2, 71) = 22.16 Model | 10687.726 2 5343.86299 Prob > F = 0.0000 Residual | 17118.6125 71 241.107218 R-squared = 0.3844 -------------+---------------------------------- Adj R-squared = 0.3670 Total | 27806.3385 73 380.908746 Root MSE = 15.528 ------------------------------------------------------------------------------ y | Coefficient Std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- x1 | 1.55219 .5321335 2.92 0.005 .4911461 2.613234 x2 | -.4668375 .3961253 -1.18 0.243 -1.256689 .3230139 _cons | -25.98694 22.21679 -1.17 0.246 -70.28596 18.31208 ------------------------------------------------------------------------------ . . . /*--------------Output 2------------------*/ . regress y x1 x2 if group==2 /* [2] */ Source | SS df MS Number of obs = 71 -------------+---------------------------------- F(2, 68) = 11.38 Model | 1076.89872 2 538.44936 Prob > F = 0.0001 Residual | 3218.09985 68 47.3249977 R-squared = 0.2507 -------------+---------------------------------- Adj R-squared = 0.2287 Total | 4294.99857 70 61.3571224 Root MSE = 6.8793 ------------------------------------------------------------------------------ y | Coefficient Std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- x1 | .610196 .2396607 2.55 0.013 .1319606 1.088432 x2 | .8038961 .181567 4.43 0.000 .4415848 1.166207 _cons | 14.95656 10.14317 1.47 0.145 -5.283816 35.19694 ------------------------------------------------------------------------------ . . . /*--------------Output 3------------------*/ . gen g2 = (group==2) . gen g2x1 = g2*x1 . gen g2x2 = g2*x2 . regress y x1 x2 g2 g2x1 g2x2 /* [3] */ Source | SS df MS Number of obs = 145 -------------+---------------------------------- F(5, 139) = 191.13 Model | 139815.397 5 27963.0794 Prob > F = 0.0000 Residual | 20336.7123 139 146.307283 R-squared = 0.8730 -------------+---------------------------------- Adj R-squared = 0.8684 Total | 160152.11 144 1112.16743 Root MSE = 12.096 ------------------------------------------------------------------------------ y | Coefficient Std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- x1 | 1.55219 .4145229 3.74 0.000 .7326048 2.371776 x2 | -.4668375 .3085748 -1.51 0.133 -1.076945 .1432697 g2 | 40.9435 24.85126 1.65 0.102 -8.191845 90.07885 g2x1 | -.9419941 .5910998 -1.59 0.113 -2.110703 .2267152 g2x2 | 1.270734 .444 2.86 0.005 .3928667 2.1486 _cons | -25.98694 17.3065 -1.50 0.135 -60.20496 8.231083 ------------------------------------------------------------------------------ . . . // 4. Illustration . /*--------------Output 4------------------*/ . predict r, resid . sum r if group==1 Variable | Obs Mean Std. dev. Min Max -------------+--------------------------------------------------------- r | 74 -1.47e-08 15.31344 -33.60296 32.88157 . gen w = r(Var)*(r(N)-1)/(r(N)-3) if group==1 (71 missing values generated) . sum r if group==2 Variable | Obs Mean Std. dev. Min Max -------------+--------------------------------------------------------- r | 71 2.27e-08 6.780329 -16.45019 16.31729 . replace w = r(Var)*(r(N)-1)/(r(N)-3) if group==2 (71 real changes made) . reg y x1 x2 g2 g2x1 g2x2 [aw=1/w] /* [4] */ (sum of wgt is 1.807181618790305) Source | SS df MS Number of obs = 145 -------------+---------------------------------- F(5, 139) = 193.50 Model | 77628.4369 5 15525.6874 Prob > F = 0.0000 Residual | 11152.7252 139 80.2354329 R-squared = 0.8744 -------------+---------------------------------- Adj R-squared = 0.8699 Total | 88781.1621 144 616.535848 Root MSE = 8.9574 ------------------------------------------------------------------------------ y | Coefficient Std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- x1 | 1.55219 .5321335 2.92 0.004 .5000676 2.604313 x2 | -.4668375 .3961253 -1.18 0.241 -1.250048 .3163726 g2 | 40.9435 24.42273 1.68 0.096 -7.344572 89.23158 g2x1 | -.9419941 .5836123 -1.61 0.109 -2.095899 .2119111 g2x2 | 1.270734 .4357543 2.92 0.004 .4091699 2.132297 _cons | -25.98694 22.21679 -1.17 0.244 -69.91348 17.93961 ------------------------------------------------------------------------------ . . . // 5. The (lack of) importance of not constraining the variance . /*--------------Output 5------------------*/ . test g2x1 g2x2 g2 ( 1) g2x1 = 0 ( 2) g2x2 = 0 ( 3) g2 = 0 F( 3, 139) = 316.69 Prob > F = 0.0000 . . . /*--------------Output 6------------------*/ . regress y x1 x2 g2 g2x1 g2x2 /* [3] */ Source | SS df MS Number of obs = 145 -------------+---------------------------------- F(5, 139) = 191.13 Model | 139815.397 5 27963.0794 Prob > F = 0.0000 Residual | 20336.7123 139 146.307283 R-squared = 0.8730 -------------+---------------------------------- Adj R-squared = 0.8684 Total | 160152.11 144 1112.16743 Root MSE = 12.096 ------------------------------------------------------------------------------ y | Coefficient Std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- x1 | 1.55219 .4145229 3.74 0.000 .7326048 2.371776 x2 | -.4668375 .3085748 -1.51 0.133 -1.076945 .1432697 g2 | 40.9435 24.85126 1.65 0.102 -8.191845 90.07885 g2x1 | -.9419941 .5910998 -1.59 0.113 -2.110703 .2267152 g2x2 | 1.270734 .444 2.86 0.005 .3928667 2.1486 _cons | -25.98694 17.3065 -1.50 0.135 -60.20496 8.231083 ------------------------------------------------------------------------------ . test g2x1 g2x2 g2 ( 1) g2x1 = 0 ( 2) g2x2 = 0 ( 3) g2 = 0 F( 3, 139) = 309.08 Prob > F = 0.0000 . . . . // 6. Another way to fit the variance-unconstrained model . /*--------------Output 7------------------*/ . xtset group Panel variable: group (unbalanced) . xtgls y x1 x2 g2 g2x1 g2x2, panels(het) /* [5] */ Cross-sectional time-series FGLS regression Coefficients: generalized least squares Panels: heteroskedastic Correlation: no autocorrelation Estimated covariances = 2 Number of obs = 145 Estimated autocorrelations = 0 Number of groups = 2 Estimated coefficients = 6 Obs per group: min = 71 avg = 72.5 max = 74 Wald chi2(5) = 1008.72 Prob > chi2 = 0.0000 ------------------------------------------------------------------------------ y | Coefficient Std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- x1 | 1.55219 .5212354 2.98 0.003 .5305875 2.573793 x2 | -.4668375 .3880126 -1.20 0.229 -1.227328 .2936533 g2 | 40.9435 23.91887 1.71 0.087 -5.936616 87.82362 g2x1 | -.9419941 .5715739 -1.65 0.099 -2.062258 .1782701 g2x2 | 1.270734 .4267639 2.98 0.003 .4342917 2.107175 _cons | -25.98694 21.76179 -1.19 0.232 -68.63927 16.66539 ------------------------------------------------------------------------------ . log close name: log: C:\Users\danie\Desktop\uncv.log log type: text closed on: 22 Jun 2023, 16:09:09 -------------------------------------------------------------------------------------------------------------