UCLA Academic Technology Services HomeServicesClassesContactJobs

Stata Textbook Examples
Applied Linear Statistical Models by Neter, Kutner, et. al.
Chapter 19: Two-factor Analysis of Variance-Equal Sample Sizes

Inputting the Castle Bakery data, table 19.7, p. 818.
input sales height width store
  47  1  1  1
  43  1  1  2
  46  1  2  1
  40  1  2  2
  62  2  1  1
  68  2  1  2
  67  2  2  1
  71  2  2  2
  41  3  1  1
  39  3  1  2
  42  3  2  1
  46  3  2  2
end

Table 19.7, p. 818.

table height width, contents(sum sales mean sales) row col
-------------------------------
          |        width       
   height |     1      2  Total
----------+--------------------
        1 |    90     86    176
          |    45     43     44
          | 
        2 |   130    138    268
          |    65     69     67
          | 
        3 |    80     88    168
          |    40     44     42
          | 
    Total |   300    312    612
          |    50     52     51
-------------------------------

Fig. 19.6, p. 820. This figure uses anovaplot, a user written program.  You can download it by typing findit anovaplot (see How can I used the findit command to search for programs and get additional help? for more information about using findit).

anova sales height width height*width
anovaplot, scatter(ms(i)) xlabel(1 "A1" 2 "A2" 3 "A3")

Table 19.9, p. 820-824.

anova sales height width height*width
                           Number of obs =      12     R-squared     =  0.9622
                           Root MSE      = 3.21455     Adj R-squared =  0.9308

                  Source |  Partial SS    df       MS           F     Prob > F
            -------------+----------------------------------------------------
                   Model |        1580     5         316      30.58     0.0003
                         |
                  height |        1544     2         772      74.71     0.0001
                   width |          12     1          12       1.16     0.3226
            height*width |          24     2          12       1.16     0.3747
                         |
                Residual |          62     6  10.3333333   
            -------------+----------------------------------------------------
                   Total |        1642    11  149.272727   

Figure 19.8, p. 828.

anova sales height width height*width
predict yhat
predict r, residuals
twoway scatter r yhat, m(x) msize(huge)
qnorm r, m(x) msize(huge)
(a) (b)
F tests of the interaction and main effects, p. 830-831.
Note the F tests are included in the standard output and have been italicized for illustration.
anova sales height width height*width
                         Number of obs =      12     R-squared     =  0.9622
                           Root MSE      = 3.21455     Adj R-squared =  0.9308

                  Source |  Partial SS    df       MS           F     Prob > F
            -------------+----------------------------------------------------
                   Model |        1580     5         316      30.58     0.0003
                         |
                  height |        1544     2         772      74.71     0.0001
                   width |          12     1          12       1.16     0.3226
            height*width |          24     2          12       1.16     0.3747
                         |
                Residual |          62     6  10.3333333   
            -------------+----------------------------------------------------
                   Total |        1642    11  149.272727 
Creating the dummy and interaction variables for the Regression model of the Bakery data, p. 833.
gen x1 = 0
recode x1 0 = 1  if height == 1
recode x1 0 = -1 if height == 3
gen x2 = 0
recode x2 0 = 1  if height == 2
recode x2 0 = -1 if height == 3
gen x3 = -1
recode x3 -1 = 1  if width == 1
gen x1x3 = x1*x3
gen x2x3 = x2*x3
list height width x1 x2 x3 x1x3 x2x3
     +---------------------------------------------+
     | height   width   x1   x2   x3   x1x3   x2x3 |
     |---------------------------------------------|
  1. |      1       1    1    0    1      1      0 |
  2. |      1       1    1    0    1      1      0 |
  3. |      1       2    1    0   -1     -1      0 |
  4. |      1       2    1    0   -1     -1      0 |
  5. |      2       1    0    1    1      0      1 |
     |---------------------------------------------|
  6. |      2       1    0    1    1      0      1 |
  7. |      2       2    0    1   -1      0     -1 |
  8. |      2       2    0    1   -1      0     -1 |
  9. |      3       1   -1   -1    1     -1     -1 |
 10. |      3       1   -1   -1    1     -1     -1 |
     |---------------------------------------------|
 11. |      3       2   -1   -1   -1      1      1 |
 12. |      3       2   -1   -1   -1      1      1 |
     +---------------------------------------------+

Table 19.10a, p. 836.

list height width store sales x1 x2 x3 x1x3 x2x3
     +-------------------------------------------------------------+
     | height   width   store   sales   x1   x2   x3   x1x3   x2x3 |
     |-------------------------------------------------------------|
  1. |      1       1       1      47    1    0    1      1      0 |
  2. |      1       1       2      43    1    0    1      1      0 |
  3. |      1       2       1      46    1    0   -1     -1      0 |
  4. |      1       2       2      40    1    0   -1     -1      0 |
  5. |      2       1       1      62    0    1    1      0      1 |
     |-------------------------------------------------------------|
  6. |      2       1       2      68    0    1    1      0      1 |
  7. |      2       2       1      67    0    1   -1      0     -1 |
  8. |      2       2       2      71    0    1   -1      0     -1 |
  9. |      3       1       1      41   -1    0    1     -1      0 |
 10. |      3       1       2      39   -1    0    1     -1      0 |
     |-------------------------------------------------------------|
 11. |      3       2       1      42   -1    0   -1      1      0 |
 12. |      3       2       2      46   -1    0   -1      1      0 |
     +-------------------------------------------------------------+

Table 19.10c, p. 836.

anova sales x1 x2 x3 x1x3 x2x3, seq cont( x1 x2 x3 x1x3 x2x3)
                           Number of obs =      12     R-squared     =  0.9622
                           Root MSE      = 3.21455     Adj R-squared =  0.9308

                  Source |    Seq. SS     df       MS           F     Prob > F
              -----------+----------------------------------------------------
                   Model |        1580     5         316      30.58     0.0003
                         |
                      x1 |           8     1           8       0.77     0.4128
                      x2 |        1536     1        1536     148.65     0.0000
                      x3 |          12     1          12       1.16     0.3226
                    x1x3 |          18     1          18       1.74     0.2350
                    x2x3 |           6     1           6       0.58     0.4749
                         |
                Residual |          62     6  10.3333333   
              -----------+----------------------------------------------------
                   Total |        1642    11  149.272727

Pooling sums of squares in the Bakery Sales example, p. 837.

anova sales height width
                           Number of obs =      12     R-squared     =  0.9476
                           Root MSE      = 3.27872     Adj R-squared =  0.9280

                  Source |  Partial SS    df       MS           F     Prob > F
              -----------+----------------------------------------------------
                   Model |        1556     3  518.666667      48.25     0.0000
                         |
                  height |        1544     2         772      71.81     0.0000
                   width |          12     1          12       1.12     0.3216
                         |
                Residual |          86     8       10.75   
              -----------+----------------------------------------------------
                   Total |        1642    11  149.272727

How to cite this page

Report an error on this page

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.