Note: The syntax used on this page is appropriate for Stata 11 and makes use of the user written program anovalator (search anovalator) for many of the designs.
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 */ /* */ /* */ /*****************************************************************************************/ use https://stats.idre.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 anovalator a b, main 2way fratio /*******************************************/ /* 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 anovalator a, main fratio /**************************************/ /* 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 */ /* */ /* */ /*****************************************************************************************/ use https://stats.idre.ucla.edu/stat/stata/examples/kirk/rb4, clear /* anova code: */ anova y a s, repeated(a) xtmixed y i.a || s:, var anovalator a, main fratio /******************************************************************************************/ /* */ /* randomized block factorial design */ /* */ /* */ /******************************************************************************************/ use https://stats.idre.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 */ /* */ /* */ /**************************************************************************************/ /* spf-2.4 subjects nested in a */ /* between subjects: a */ /* within subjects: b */ use https://stats.idre.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 anovalator a b, main 2way fratio /* spf-22.4 subjects nested in a*c */ /* between subjects: a & c */ /* within subjects: b */ use https://stats.idre.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 anovalator a c b, main fratio anovalator a c, 2way fratio anovalator a b, 2way fratio anovalator b c, 2way fratio anovalator a c b, 3way fratio /* spf-2.22 subjects nested in a */ /* between subjects: a */ /* within subjects: b & c */ use https://stats.idre.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 anovalator a b c, main fratio anovalator a b, 2way fratio anovalator a c, 2way fratio anovalator b c, 2way fratio anovalator a b c, 3way fratio /****************************************************************************************/ /* */ /* completely randomized hierarchical (nested) design */ /* */ /* */ /****************************************************************************************/ /* chr-2(8) b is nested in a */ use https://stats.idre.ucla.edu/stat/stata/examples/kirk/crh28, clear /* anova code: */ anova y a / b|a / xtmixed y i.a || b:, var /* test for a main-effect */ anovalator a, main fratio /* test for b nested in a */ xtmixed y i.a b|a, var anovalator b, main fratio
