/************* classicalmixed.sas ********************* Three levels of factor A, four levels of B Both fixed Both random A fixed, B random B nested within A ***************************************************/ data mixedup; infile '/folders/myfolders/mixedup.data.txt'; input ID Y A B; proc glm plots=none; title 'Default proc glm: Both Fixed'; class A B ; model y = a | b; means a*b; proc glm plots=none; title 'Both Random with proc glm'; class A B ; model y = a | b; random a b a*b / test ; /* Have to specify interaction random too, and explicitly ask for tests */ proc glm plots=none; title 'A Fixed, B Random'; class A B ; model y = a | b; random b a*b / test; proc glm plots=none; title 'A fixed, B random and nested within A'; class A B ; model y = a b(a); random b(a) / test; proc mixed cl; /*Confidence Limits for variances (There are no tests) */ title 'A fixed, B random and nested within A'; title2 'Using proc mixed'; title3 'Compare F for A = 7.32, F for B(A) = 3.11, p = 0.0126'; class A B ; model y = a ; random b(a); proc glm plots=none; title 'Both random, B nested within A'; class A B ; model y = a b(a); random a b(a) / test; proc sort; by A B; /* Data must be sorted in order of nesting*/ proc nested; title 'Nested random effects with proc nested'; title2 'Compare F for A = 7.32, F for B(A) = 3.11'; class A B; var Y; proc mixed cl; title 'Pure nested with proc mixed'; class A B ; model y = ; random a b(a); /****** What happens when the data are unbalanced? ******/ proc glm plots=none; title 'UNBALANCED: Both random, B nested within A'; title2 'Using proc glm'; title3 'Compare F for A = 7.32, F for B(A) = 3.11'; where id ne 1; /* Throw out one case */ class A B ; model y = a b(a); random a b(a) / test; proc sort; by A B; /* Data must be sorted in order of nesting*/ proc nested; title 'UNBALANCED: Nested random effects with proc nested'; title2 'Proc glm gave F for A = 7.38, F for B(A) = 2.77'; where id ne 1; /* Throw out one case */ class A B; var Y; proc mixed cl; title 'UNBALANCED: Pure nested with proc mixed'; title2 'Proc nested estimated Var[A]=14.39, Var[B(A)]=6.25'; where id ne 1; /* Throw out one case */ class A B ; model y = ; random a b(a); /* Conclusion: Proc glm tries to fix the problem, proc nested gives up and proc mixed never cared in the first place. */