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.
Why do we need the contrast command? The reason we need the contrast command with xtmixed is because the standard coding of factor variables is dummy coding. Consider the model, xtmixed y a##b. If we were to do test 2.a 3.a we would not get the “true” main effect for a. We would get the simple effect of a for b held at its reference value. However, contrast a will give us the “true” main effect.
/*****************************************************************************************/ /* */ /* completely randomized factorial design */ /* */ /* from kirk -- see https://stats.idre.ucla.edu/stat/stata/examples/kirk/kirkstata9.htm */ /* */ /*****************************************************************************************/ 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 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 https://stats.idre.ucla.edu/stat/stata/examples/kirk/kirkstata7.htm */ /* */ /*****************************************************************************************/ 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 reml contrast a /******************************************************************************************/ /* */ /* 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 reml contrast a##b /* 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 reml contrast a##c##b /* 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 reml contrast a##b##c /****************************************************************************************/ /* */ /* 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 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