This FAQ presents some classical ANOVA designs using the mixed command. 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 command outputs the results as chi-square after mixed; 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 mixed is because the standard coding of factor variables is dummy coding. Consider the model, mixed 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 */ /**************************************/ mixed y a##b, 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 mixed y i.a || _all: R.b || _all: R.ab, 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 mixed y || _all: R.a || _all: R.b || _all: R.ab, 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) mixed y i.a || s:, 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) mixed y a##b || s:, 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) mixed y a##c##b || s:, 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) mixed y a##b##c || s: || s: R.b || s: R.c, 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 / mixed y i.a || b:, reml /* test for main-effect of a */ contrast a /* test for b nested in a */ mixed y i.a a#b, reml contrast b, noestimcheck