Stata FAQ
How can I do ANOVA contrasts in Stata?

Stata does not have a built-in contrast command; however, ATS has developed a program that will do ANOVA contrasts. You can download the program anovacontrast.ado by typing findit anovacontrast (see How can I use the findit command to search for programs and get additional help? for more information about using findit)

Now, let's read in an example dataset, crf24, adapted from Kirk (1968, First Edition).
use http://www.ats.ucla.edu/stat/stata/faq/crf24
These data are from a 2x4 factorial design but the same data can also be used for one-way ANOVA examples. The variable y is the dependent variable. The variable a is an independent variable with two levels while b is an independent variable with four levels.

Using the anovacontrast command in a one-way ANOVA

anova y b

             Number of obs =      32     R-squared     =  0.8259
             Root MSE      = 1.21008     Adj R-squared =  0.8072

    Source |  Partial SS    df       MS           F     Prob > F
-----------+----------------------------------------------------
     Model |      194.50     3  64.8333333      44.28     0.0000
           |
         b |      194.50     3  64.8333333      44.28     0.0000
           |
  Residual |       41.00    28  1.46428571   
-----------+----------------------------------------------------
     Total |      235.50    31  7.59677419

table b, contents(mean y)

----------+-----------
        b |    mean(y)
----------+-----------
        1 |       2.75
        2 |        3.5
        3 |       6.25
        4 |          9
----------+-----------
It is quite clear that there is a significant overall F for the independent variable b. Now, let's devise some contrasts that we can test:
1) group 3 versus group 4
2) the average of groups 1 and 2 versus the average of groups 3 and 4
3) the average of groups 1, 2, and 3 versus group 4
anovacontrast b, values(0 0 1 -1)

Contrast variable b (0 0 1 -1)                 Dep Var  =        y
source           SS          df      MS        N of obs =       32
---------+---------------------------------    F        =    20.66
contrast |      30.25         1     30.2500    Prob > F =   0.0001
error    |         41        28      1.4643
---------+---------------------------------

anovacontrast b, values(1 1 -1 -1)

Contrast variable b (1 1 -1 -1)                Dep Var  =        y
source           SS          df      MS        N of obs =       32
---------+---------------------------------    F        =   110.63
contrast |        162         1    162.0000    Prob > F =   0.0000
error    |         41        28      1.4643
---------+---------------------------------

anovacontrast b, values(1 1 1 -3)

Contrast variable b (1 1 1 -3)                 Dep Var  =        y
source           SS          df      MS        N of obs =       32
---------+---------------------------------    F        =    95.72
contrast | 140.166667         1    140.1667    Prob > F =   0.0000
error    |         41        28      1.4643
---------+---------------------------------

Using the anovacontrast command in a two-way ANOVA

Now let's try the same contrasts on b but in a two-way ANOVA.
anova y a b a*b

              Number of obs =      32     R-squared     =  0.9214
              Root MSE      = .877971     Adj R-squared =  0.8985

    Source |  Partial SS    df       MS           F     Prob > F
-----------+----------------------------------------------------
     Model |      217.00     7       31.00      40.22     0.0000
           |
         a |       3.125     1       3.125       4.05     0.0554
         b |      194.50     3  64.8333333      84.11     0.0000
       a*b |      19.375     3  6.45833333       8.38     0.0006
           |
  Residual |       18.50    24  .770833333   
-----------+----------------------------------------------------
     Total |      235.50    31  7.59677419 

anovacontrast b, values(0 0 1 -1)

Contrast variable b (0 0 1 -1)                 Dep Var  =        y
source           SS          df      MS        N of obs =       32
---------+---------------------------------    F        =    39.24
contrast |      30.25         1     30.2500    Prob > F =   0.0000
error    |       18.5        24      0.7708
---------+---------------------------------

anovacontrast b, values(1 1 -1 -1)

Contrast variable b (1 1 -1 -1)                Dep Var  =        y
source           SS          df      MS        N of obs =       32
---------+---------------------------------    F        =   210.16
contrast |        162         1    162.0000    Prob > F =   0.0000
error    |       18.5        24      0.7708
---------+---------------------------------

anovacontrast b, values(1 1 1 -3)

Contrast variable b (1 1 1 -3)                 Dep Var  =        y
source           SS          df      MS        N of obs =       32
---------+---------------------------------    F        =   181.84
contrast | 140.166667         1    140.1667    Prob > F =   0.0000
error    |       18.5        24      0.7708
---------+---------------------------------
Note that the F-ratios in these contrasts are larger than the F-ratios in the one-way ANOVA example. This is because the two-way ANOVA has a smaller mean square residual than the one-way ANOVA.

How to cite this page

Report an error on this page or leave a comment

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.