UCLA Academic Technology Services HomeServicesClassesContactJobs
Help the Stat Consulting Group by giving a gift             
Loading

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

Note: The xtmixed syntax used on this page is works in Stata 11, but the contrast command needs Stata 12.

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.

Note: The contrast outputs the results as chi-square after xtmixed, to rescale as F-ratios divide chi-square by the degrees of freedom.


/*****************************************************************************************/
/*                                                                                       */
/*              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

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

xtmixed y a##b, var reml

contrast a##b

/*******************************************/
/* 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  

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

contrast a


/**************************************/
/* 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 reml


/*****************************************************************************************/
/*                                                                                       */
/*              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)  

xtmixed y i.a || s:, var reml

contrasr a



/******************************************************************************************/
/*                                                                                        */
/*              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, repeated(a b) 

anovalator a b, main 2way fratio



/**************************************************************************************/
/*                                                                                    */
/*              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) 

xtmixed y a##b || s:, var reml

contrast a##b


/* 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

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

xtmixed y a##c##b || s:, var reml

contrast a##c##b



/* 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)  

xtmixed y a##b##c || s: || s: R.b || s: R.c, var reml

contrast a##b##c




/****************************************************************************************/
/*                                                                                      */
/*               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 i.a || b:, var reml

/* test for main-effect of a */

contrast a

/* test for b nested in a */

xtmixed y i.a a#b, var reml

contrast b, noestimcheck

How to cite this page

Report an error on this page or leave a comment

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.