Stata FAQ
How can I do classical ANOVA designs using xtmixed? (Stata 10)

Note: The syntax used on this page is appropriate for Stata 10 and earlier.

This FAQ presents some classical ANOVA designs using xtmixed. Included is the code for factorial designs, a randomized block design, a randomized block factorial design, three split-plot factorial designs, and a completely randomized hierarchical (nested) design. The examples are taken from Roger Kirk's Experimental Design.

For the purposes of this FAQ only the code for the examples are presented. You will actually need to run the examples to see the output.

All of the examples are run using REML and for balanced designs will produce the same results as classical analysis of variance. For unbalanced designs the results will differ from the ANOVA solutions.


/*****************************************************************************************/
/*                                                                                       */
/*              completely randomized factorial design                                   */
/*                                                                                       */
/* from kirk -- see http://www.ats.ucla.edu/stat/stata/examples/kirk/kirkstata9.htm      */
/*                                                                                       */
/*****************************************************************************************/

use http://www.ats.ucla.edu/stat/stata/examples/kirk/crf33, clear


xi3 e.a*e.b

/**************************************/
/* fixed effects model: a & b fixed   */
/* anova code: anova y a b a*b        */  
/**************************************/

xtmixed y _Ia_2 _Ia_3 _Ib_2 _Ib_3 _Ia2Xb2 _Ia2Xb3 _Ia3Xb2 _Ia3Xb3

test _Ia_2 _Ia_3

/* scale chi^2 as F-ratio to compare with anova */
display r(chi2)/r(df)

test _Ib_2 _Ib_3

display r(chi2)/r(df)

test _Ia2Xb2 _Ia2Xb3 _Ia3Xb2 _Ia3Xb3

display r(chi2)/r(df)

/*******************************************/
/* mixed-effects model; a fixed & b random */
/* a*b interaction is also random          */
/* can only test the fixed effect          */
/*******************************************/

/* create a by b factor for interaction */
egen ab = group(a b), label  

xi: xtmixed y i.a || _all: R.b || _all: R.ab, var

test _Ia_2 _Ia_3

/* scale chi^2 as F-ratio to compare with anova */
display r(chi2)/r(df)


/**************************************/
/* random effects model; a & b random */
/* no fixed effects in model          */ 
/**************************************/

/* create a by b factor for interaction */
egen ab = group(a b), label

xtmixed y  || _all: R.a || _all: R.b || _all: R.ab, var


/*****************************************************************************************/
/*                                                                                       */
/*              randomized block design                                                  */
/*                                                                                       */
/* from kirk -- see http://www.ats.ucla.edu/stat/stata/examples/kirk/kirkstata7.htm      */
/*                                                                                       */
/*****************************************************************************************/

use http://www.ats.ucla.edu/stat/stata/examples/kirk/rb4, clear

/* anova code: */ anova y a s, repeated(a)  

xi: xtmixed y i.a || s:

test _Ia_2 _Ia_3 _Ia_4

/* scale chi^2 as F-ratio to compare with anova */
display r(chi2)/r(df)



/******************************************************************************************/
/*                                                                                        */
/*              randomized block factorial design                                         */
/*                                                                                        */
/* from kirk -- see http://www.ats.ucla.edu/stat/stata/examples/kirk/kirkstata10.htm      */
/*                                                                                        */
/******************************************************************************************/

use http://www.ats.ucla.edu/stat/stata/examples/kirk/rbf33, clear

/* anova code: */ anova y s a b a*b, repeated(a b) 

xi3 e.a*e.b

xtmixed y _Ia_2 _Ia_3 _Ib_2 _Ib_3 _Ia2Xb2 _Ia2Xb3 _Ia3Xb2 _Ia3Xb3 || s:

test _Ia_2 _Ia_3

/* scale chi^2 as F-ratio to compare with anova */
display r(chi2)/r(df)

test _Ib_2 _Ib_3

display r(chi2)/r(df)

test _Ia2Xb2 _Ia2Xb3 _Ia3Xb2 _Ia3Xb3

display r(chi2)/r(df)



/**************************************************************************************/
/*                                                                                    */
/*              split-plot factorial designs                                          */
/*                                                                                    */
/* from kirk -- see http://www.ats.ucla.edu/stat/stata/examples/kirk/kirkstata12.htm  */
/*                                                                                    */
/**************************************************************************************/

/* spf-2.4    subjects nested in a */
/* between subjects: a             */
/* within  subjects: b             */

use http://www.ats.ucla.edu/stat/stata/examples/kirk/spf2-4, clear

/* anova code: */ anova y a / s|a b a*b /, repeated(b) 

xi3 e.a*e.b

xtmixed y _Ia_2 _Ib_2 _Ib_3 _Ib_4 _Ia2Xb2 _Ia2Xb3 _Ia2Xb4 || s:

test _Ia_2

test _Ib_2 _Ib_3 _Ib_4

/* scale chi^2 as F-ratio to compare with anova */
display r(chi2)/r(df)

test _Ia2Xb2 _Ia2Xb3 _Ia2Xb4

display r(chi2)/r(df)


/* spf-22.4   subjects nested in a*c  */
/* between subjects: a & c        */
/* within  subjects: b                */

use http://www.ats.ucla.edu/stat/stata/examples/kirk/spf22-4, clear

xi3 e.a*e.b*e.c

/* anova code: */ anova y a c a*c / s|a*c b a*b b*c a*b*c /, repeated(b) 

xtmixed y _Ia_2 _Ib_2 _Ib_3 _Ib_4 _Ic_2 _Ia2Xb2 _Ia2Xb3 _Ia2Xb4 _Ia2Xc2 _Ib2Xc2 _Ib3Xc2 _Ib4Xc2 _Ia2Xb2Xc2 _Ia2Xb3Xc2 _Ia2Xb4Xc2 || s:

test _Ia_2

test _Ic_2

test _Ia2Xc2

test _Ib_2 _Ib_3 _Ib_4

/* scale chi^2 as F-ratio to compare with anova */
display r(chi2)/r(df)

test _Ia2Xb2 _Ia2Xb3 _Ia2Xb4

display r(chi2)/r(df)

test _Ib3Xc2 _Ib4Xc2 _Ib2Xc2

display r(chi2)/r(df)

test _Ia2Xb2Xc2 _Ia2Xb3Xc2 _Ia2Xb4Xc2

display r(chi2)/r(df)


/* spf-2.22    subjects nested in a */
/* between subjects: a              */
/* within  subjects: b & c      */

use http://www.ats.ucla.edu/stat/stata/examples/kirk/spf2-22, clear

/* anova code: */ anova y a / s|a b a*b / b*s|a c a*c / c*s|a b*c a*b*c /, repeated(b c)  

xi3 e.a*e.b*e.c

xtmixed y _Ia_2 _Ib_2 _Ic_2 _Ia2Xb2 _Ia2Xc2 _Ib2Xc2 _Ia2Xb2Xc2 || s: || s: R.b || s: R.c

test _Ia_2

test _Ib_2

test _Ia2Xb2

test _Ic_2

test _Ib2Xc2

test _Ia2Xb2Xc2



/****************************************************************************************/
/*                                                                                      */
/*               completely randomized hierarchical (nested) design                     */
/*                                                                                      */
/* from kirk --  see http://www.ats.ucla.edu/stat/stata/examples/kirk/kirkstata11.htm   */
/*                                                                                      */
/****************************************************************************************/

/* chr-2(8)   b is nested in a   */

use http://www.ats.ucla.edu/stat/stata/examples/kirk/crh28, clear

/* anova code: */ anova y a / b|a /

xtmixed y a || b:

test a

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.