Stata Textbook Examples
Applied Linear Statistical Models by Neter, Kutner, et. al.
Chapter 22: Two-Factor Studies--Unequal Sample Sizes and Unequal Treatment
Importance
Inputting the Growth Hormone data on page 892.
input growth gender depress rep
1.4 1 1 1
2.4 1 1 2
2.2 1 1 3
2.1 1 2 1
1.7 1 2 2
0.7 1 3 1
1.1 1 3 2
2.4 2 1 1
2.5 2 2 1
1.8 2 2 2
2.0 2 2 3
0.5 2 3 1
0.9 2 3 2
1.3 2 3 3
end
label define fm 1 "Male" 2 "Female"
label define dp 1 "Severely" 2 "Moderately" 3 "Mildly"
label values gender fm
label values depress dp
Table 22.1, p. 892.
table gender depress, contents(mean growth)
----------------------------------------------
| depress
gender | Severely Moderately Mildly
----------+-----------------------------------
Male | 2 1.9 .9
Female | 2.4 2.1 .9
----------------------------------------------
Fig. 22.1, p. 892 using a user-written program called anovaplot.You
can download the program from within Stata by issuing command "findit
anovaplot".
anova growth depress gender depress*gender
anovaplot, scatter(ms(i))

Creating the dummy variables to be used in the regression model that will
be equivalent to the ANOVA model (22.3), p. 893.
gen x1 = 1
replace x1 = -1 if gender == 2
gen x2 = 0
replace x2 = 1 if depress == 1
replace x2 = -1 if depress == 3
gen x3 = 0
replace x3 = 1 if depress == 2
replace x3 = -1 if depress == 3
gen x1x2 = x1*x2
gen x1x3 = x1*x3
list gender depress rep growth x1 x2 x3 x1x2 x1x3, clean
gender depress rep growth x1 x2 x3 x1x2 x1x3
1. Male Severely 1 1.4 1 1 0 1 0
2. Male Severely 2 2.4 1 1 0 1 0
3. Male Severely 3 2.2 1 1 0 1 0
4. Male Moderately 1 2.1 1 0 1 0 1
5. Male Moderately 2 1.7 1 0 1 0 1
6. Male Mildly 1 .7 1 -1 -1 -1 -1
7. Male Mildly 2 1.1 1 -1 -1 -1 -1
8. Female Severely 1 2.4 -1 1 0 -1 0
9. Female Moderately 1 2.5 -1 0 1 0 -1
10. Female Moderately 2 1.8 -1 0 1 0 -1
11. Female Moderately 3 2 -1 0 1 0 -1
12. Female Mildly 1 .5 -1 -1 -1 1 1
13. Female Mildly 2 .9 -1 -1 -1 1 1
14. Female Mildly 3 1.3 -1 -1 -1 1 1
Table 22.3a, p. 895.
regress growth x1 x2 x3 x1x2 x1x3
Source | SS df MS Number of obs = 14
-------------+------------------------------ F( 5, 8) = 5.51
Model | 4.47428596 5 .894857193 Prob > F = 0.0172
Residual | 1.30000007 8 .162500009 R-squared = 0.7749
-------------+------------------------------ Adj R-squared = 0.6342
Total | 5.77428604 13 .444175849 Root MSE = .40311
------------------------------------------------------------------------------
growth | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
x1 | -.1 .1163687 -0.86 0.415 -.3683466 .1683466
x2 | .5000001 .1777561 2.81 0.023 .0900938 .9099063
x3 | .3 .1575639 1.90 0.093 -.0633431 .663343
x1x2 | -.1 .1777561 -0.56 0.589 -.5099063 .3099062
x1x3 | 1.66e-09 .1575639 0.00 1.000 -.3633431 .3633431
_cons | 1.7 .1163687 14.61 0.000 1.431653 1.968347
------------------------------------------------------------------------------
Table 22.3b, p. 895.
regress growth x1 x2 x3
Source | SS df MS Number of obs = 14
-------------+------------------------------ F( 3, 10) = 10.66
Model | 4.39885736 3 1.46628579 Prob > F = 0.0019
Residual | 1.37542867 10 .137542867 R-squared = 0.7618
-------------+------------------------------ Adj R-squared = 0.6903
Total | 5.77428604 13 .444175849 Root MSE = .37087
------------------------------------------------------------------------------
growth | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
x1 | -.0857143 .1044801 -0.82 0.431 -.3185106 .147082
x2 | .4666667 .1541844 3.03 0.013 .1231224 .8102111
x3 | .3266666 .1403478 2.33 0.042 .0139523 .639381
_cons | 1.67619 .0997285 16.81 0.000 1.453981 1.8984
------------------------------------------------------------------------------
Table 22.3c, p. 895.
regress growth x2 x3 x1x2 x1x3
Source | SS df MS Number of obs = 14
-------------+------------------------------ F( 4, 9) = 6.90
Model | 4.35428595 4 1.08857149 Prob > F = 0.0080
Residual | 1.42000009 9 .157777787 R-squared = 0.7541
-------------+------------------------------ Adj R-squared = 0.6448
Total | 5.77428604 13 .444175849 Root MSE = .39721
------------------------------------------------------------------------------
growth | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
x2 | .4444445 .1631593 2.72 0.023 .0753526 .8135364
x3 | .3277777 .1519553 2.16 0.059 -.0159691 .6715246
x1x2 | -.0666667 .1709331 -0.39 0.706 -.4533441 .3200107
x1x3 | -.0166667 .154077 -0.11 0.916 -.365213 .3318797
_cons | 1.688889 .1139554 14.82 0.000 1.431104 1.946674
------------------------------------------------------------------------------
Table 22.3d, p. 895.
regress growth x1 x1x2 x1x3
Source | SS df MS Number of obs = 14
-------------+------------------------------ F( 3, 10) = 0.17
Model | .284571455 3 .094857152 Prob > F = 0.9124
Residual | 5.48971458 10 .548971458 R-squared = 0.0493
-------------+------------------------------ Adj R-squared = -0.2359
Total | 5.77428604 13 .444175849 Root MSE = .74093
------------------------------------------------------------------------------
growth | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
x1 | .0190476 .1992394 0.10 0.926 -.4248855 .4629807
x1x2 | .0666667 .3080324 0.22 0.833 -.6196723 .7530056
x1x3 | -.1933333 .2803893 -0.69 0.506 -.8180795 .4314129
_cons | 1.628571 .2087323 7.80 0.000 1.163487 2.093656
------------------------------------------------------------------------------
Testing the interactions, factor A main effects and factor B main effects,
p. 894-896.
regress growth x1 x2 x3 x1x2 x1x3
test x1x2=x1x3=0
test x1=0
test x2=x3=0
Source | SS df MS Number of obs = 14
-------------+------------------------------ F( 5, 8) = 5.51
Model | 4.47428596 5 .894857193 Prob > F = 0.0172
Residual | 1.30000007 8 .162500009 R-squared = 0.7749
-------------+------------------------------ Adj R-squared = 0.6342
Total | 5.77428604 13 .444175849 Root MSE = .40311
------------------------------------------------------------------------------
growth | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
x1 | -.1 .1163687 -0.86 0.415 -.3683466 .1683466
x2 | .5000001 .1777561 2.81 0.023 .0900938 .9099063
x3 | .3 .1575639 1.90 0.093 -.0633431 .663343
x1x2 | -.1 .1777561 -0.56 0.589 -.5099063 .3099062
x1x3 | 1.66e-09 .1575639 0.00 1.000 -.3633431 .3633431
_cons | 1.7 .1163687 14.61 0.000 1.431653 1.968347
------------------------------------------------------------------------------
( 1) x1x2 - x1x3 = 0
( 2) x1x2 = 0
F( 2, 8) = 0.23
Prob > F = 0.7980
( 1) x1 = 0
F( 1, 8) = 0.74
Prob > F = 0.4152
( 1) x2 - x3 = 0
( 2) x2 = 0
F( 2, 8) = 12.89
Prob > F = 0.0031
Table 22.4, p. 897.
anova growth gender depress gender*depress
Number of obs = 14 R-squared = 0.7749
Root MSE = .403113 Adj R-squared = 0.6342
Source | Partial SS df MS F Prob > F
---------------+----------------------------------------------------
Model | 4.47428596 5 .894857193 5.51 0.0172
|
gender | .120000014 1 .120000014 0.74 0.4152
depress | 4.18971451 2 2.09485725 12.89 0.0031
gender*depress | .075428603 2 .037714302 0.23 0.7980
|
Residual | 1.30000007 8 .162500009
---------------+----------------------------------------------------
Total | 5.77428604 13 .444175849
Creating the dummy variables to get the regression model that will supply
us with the value of SSE(F), p. 908.
drop x1-x1x3
gen x1 = 0
replace x1 = 1 if gender==1 & depress==1
gen x2 = 0
replace x2 = 1 if gender==1 & depress==2
gen x3 = 0
replace x3 = 1 if gender==1 & depress==3
gen x4 = 0
replace x4 = 1 if gender==2 & depress==1
gen x5 = 0
replace x5 = 1 if gender==2 & depress==2
gen x6 = 0
replace x6 = 1 if gender==2 & depress==3
gen z1 = 0
replace z1 = x1-2*x4
gen z2 = 0
replace z2 = x2+2*x4+2*x6
gen z3 = 0
replace z3 = x3-2*x6
gen z4 = 0
replace z4 = x4+x5+x6
Table 22.6, p. 909.
list gender depress rep growth x1 x2 x3 x4 x5 x6 z1 z2 z3 z4, nolabel
+-----------------------------------------------------------------------------------+
| gender depress rep growth x1 x2 x3 x4 x5 x6 z1 z2 z3 z4 |
|-----------------------------------------------------------------------------------|
1. | 1 1 1 1.4 1 0 0 0 0 0 1 0 0 0 |
2. | 1 1 2 2.4 1 0 0 0 0 0 1 0 0 0 |
3. | 1 1 3 2.2 1 0 0 0 0 0 1 0 0 0 |
4. | 1 2 1 2.1 0 1 0 0 0 0 0 1 0 0 |
5. | 1 2 2 1.7 0 1 0 0 0 0 0 1 0 0 |
|-----------------------------------------------------------------------------------|
6. | 1 3 1 .7 0 0 1 0 0 0 0 0 1 0 |
7. | 1 3 2 1.1 0 0 1 0 0 0 0 0 1 0 |
8. | 2 1 1 2.4 0 0 0 1 0 0 -2 2 0 1 |
9. | 2 2 1 2.5 0 0 0 0 1 0 0 0 0 1 |
10. | 2 2 2 1.8 0 0 0 0 1 0 0 0 0 1 |
|-----------------------------------------------------------------------------------|
11. | 2 2 3 2 0 0 0 0 1 0 0 0 0 1 |
12. | 2 3 1 .5 0 0 0 0 0 1 0 2 -2 1 |
13. | 2 3 2 .9 0 0 0 0 0 1 0 2 -2 1 |
14. | 2 3 3 1.3 0 0 0 0 0 1 0 2 -2 1 |
+-----------------------------------------------------------------------------------+
Full Model, p. 909.
Note that the following code uses global macros to store results and
perform calculations. For more information about macros, see help macro
in Stata.
regress growth x1 x2 x3 x4 x5 x6, noconstant
global ssef = e(rss)
global dff = e(df_r)
Source | SS df MS Number of obs = 14
-------------+------------------------------ F( 6, 8) = 43.34
Model | 42.2600004 6 7.04333341 Prob > F = 0.0000
Residual | 1.30000007 8 .162500009 R-squared = 0.9702
-------------+------------------------------ Adj R-squared = 0.9478
Total | 43.5600005 14 3.11142861 Root MSE = .40311
------------------------------------------------------------------------------
growth | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
x1 | 2 .2327373 8.59 0.000 1.463307 2.536693
x2 | 1.9 .2850439 6.67 0.000 1.242688 2.557312
x3 | .9 .2850439 3.16 0.013 .2426877 1.557312
x4 | 2.4 .4031129 5.95 0.000 1.47042 3.32958
x5 | 2.1 .2327373 9.02 0.000 1.563307 2.636693
x6 | .9 .2327373 3.87 0.005 .3633067 1.436693
------------------------------------------------------------------------------
Reduced Model, p. 909.
regress growth z1 z2 z3 z4, noconstant
global sser = e(rss)
global dfr = e(df_r)
Source | SS df MS Number of obs = 14
-------------+------------------------------ F( 4, 10) = 20.41
Model | 38.8057145 4 9.70142864 Prob > F = 0.0001
Residual | 4.75428597 10 .475428597 R-squared = 0.8909
-------------+------------------------------ Adj R-squared = 0.8472
Total | 43.5600005 14 3.11142861 Root MSE = .68951
------------------------------------------------------------------------------
growth | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
z1 | 1.6 .3132161 5.11 0.000 .9021111 2.297889
z2 | 1.514286 .3191826 4.74 0.001 .8031025 2.225469
z3 | 1.885714 .3191826 5.91 0.000 1.174531 2.596898
z4 | 1.971429 .3786598 5.21 0.000 1.127722 2.815135
------------------------------------------------------------------------------
F* statistic, F critical value and p-value p. 909.
display (($sser-$ssef)/($dfr-$dff))/($ssef/$dff)
10.628571
display invF(2, 8, .95)
4.4589701
display Ftail(2, 8, 10.628571)
.00559026
Repeating the same test using SSA, p. 914.
sum growth if gender==1
global y1bar = r(mean)
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
growth | 7 1.657143 .6241184 .7 2.4
sum growth if gender==2
global y2bar = r(mean)
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
growth | 7 1.628571 .7565586 .5 2.5
sum growth
global ybar = r(mean)
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
growth | 14 1.642857 .6664652 .5 2.5
global ssa = 7*(($y1bar-$ybar)^2)+7*(($y2bar-$ybar)^2)
display $ssa/($ssef/$dff)
.01758249
display invF(1, 8, .95)
5.3176551
display Ftail(1, 8, .01758249)
.89778507
UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services
The content of this web site should not be
construed as an endorsement of any particular web site, book, or software
product by the University of California.
|