Stata Textbook Examples
Design and Analysis by Geoffrey Keppel
Chapter 16: The Single Factor Within Subjects Design

Please Note: thecode on this page has been updated to Stata 12.
On page 349, table 16-4, Keppel shows the results of a one way within subjects anova. We show this analysis in Stata below.
use http://www.ats.ucla.edu/stat/stata/examples/da/chap16n, clear

anova score a s, repeated(a)

                           Number of obs =      18     R-squared     =  0.5789
                           Root MSE      = 3.95811     Adj R-squared =  0.2841

                  Source |  Partial SS    df       MS           F     Prob > F
              -----------+----------------------------------------------------
                   Model |  215.333333     7  30.7619048       1.96     0.1608
                         |
                       a |      148.00     2       74.00       4.72     0.0360
                       s |  67.3333333     5  13.4666667       0.86     0.5396
                         |
                Residual |  156.666667    10  15.6666667   
              -----------+----------------------------------------------------
                   Total |      372.00    17  21.8823529   


Between-subjects error term:  s
                     Levels:  6         (5 df)
     Lowest b.s.e. variable:  s

Repeated variable: a
                                          Huynh-Feldt epsilon        =  1.6280
                                          *Huynh-Feldt epsilon reset to 1.0000
                                          Greenhouse-Geisser epsilon =  0.9875
                                          Box's conservative epsilon =  0.5000

                                            ------------ Prob > F ------------
                  Source |     df      F    Regular    H-F      G-G      Box
              -----------+----------------------------------------------------
                       a |      2     4.72   0.0360   0.0360   0.0367   0.0818
                Residual |     10
              -----------+----------------------------------------------------
When you specify the repeated(a) that tells Stata that a is a within subjects variable and that it should pick the appropriate error term. It also includes the H-F and G-G adjusted probabilities. You can also manually specify the error term as shown below. The "/" indicates to Stata that s a should be tested against s|a (i.e., s nested within a). This way, we are telling Stata to test a against s nested within a (we also indicate to test s against s nested within a, but we are not as interested in that test).
anova score s a / s|a 

                           Number of obs =      18     R-squared     =  1.0000
                           Root MSE      =    0.00     Adj R-squared =

                  Source |  Partial SS    df       MS           F     Prob > F
              -----------+----------------------------------------------------
                   Model |      372.00    17  21.8823529   
                         |
                       s |  67.3333333     5  13.4666667       0.86     0.5396
                       a |      148.00     2       74.00       4.72     0.0360
                     s|a |  156.666667    10  15.6666667   
              -----------+----------------------------------------------------
                         |
                Residual |        0.00     0
              -----------+----------------------------------------------------
                   Total |      372.00    17  21.8823529
You might ask why you would use this second syntax instead of the first, since the first is easier. Well, on page 358, table 16-6, Keppel shows a contrast on the repeated factor. We accomplish the contrast by creating acomp1 which shows the comparison of interest.
generate acomp1 = a
recode acomp1 1=-1 2=2 3=-1
As Keppel shows in table 16-6 Acomp is compared against Acomp x S. We use acomp1 s / acomp1#s to indicate this effect to Stata.
anova score  c.acomp1 s / c.acomp1#s

                           Number of obs =      18     R-squared     =  0.6989
                           Root MSE      = 4.32049     Adj R-squared =  0.1470

                  Source |  Partial SS    df       MS           F     Prob > F
              -----------+----------------------------------------------------
                   Model |         260    11  23.6363636       1.27     0.4038
                         |
                  acomp1 |         121     1         121       8.44     0.0336
                       s |  67.3333333     5  13.4666667       0.94     0.5264
                s#acomp1 |  71.6666667     5  14.3333333   
              -----------+----------------------------------------------------
                         |
                Residual |         112     6  18.6666667   
              -----------+----------------------------------------------------
                   Total |         372    17  21.8823529 
On page 363, table 16-8, Keppel shows a similar analysis which also includes the position p (trial number) for removing practice effects. Since all of the factors to be tested are tested against the residual, we can test this simply by the following anova test.
anova score a s p

                           Number of obs =      18     R-squared     =  0.8217
                           Root MSE      = 2.87953     Adj R-squared =  0.6211

                  Source |  Partial SS    df       MS           F     Prob > F
              -----------+----------------------------------------------------
                   Model |  305.666667     9   33.962963       4.10     0.0298
                         |
                       a |      148.00     2       74.00       8.92     0.0092
                       s |  67.3333333     5  13.4666667       1.62     0.2580
                       p |  90.3333333     2  45.1666667       5.45     0.0321
                         |
                Residual |  66.3333333     8  8.29166667   
              -----------+----------------------------------------------------
                   Total |      372.00    17  21.8823529
We should note that when doing tests that involve repeated measures in Stata, you sometimes need to manually specify error terms, and because of this you have to be very careful with such analyses. If you would like more information, you can see the Stata FAQ about this at http://www.stata.com/support/faqs/stat/anova2.html.

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.