[R] Nested effects (was: "no subject")

Rolf Turner r.turner at auckland.ac.nz
Fri Oct 30 23:00:47 CET 2015


On 31/10/15 03:32, Wagenaar, Daniel (wagenadl) wrote:
> Dear R users:
>
> All textbook references that I consult say that in a nested ANOVA
> (e.g., A/B), the F statistic for factor A should be calculated as F_A =
> MS_A / MS_(B within A). But when I run this simple example:
>
> set.seed(1)
> A = factor(rep(1:3, each=4))
> B = factor(rep(1:2, 3, each=2))
> Y = rnorm(12)
> anova(lm(Y ~ A/B))
>
> I get this result:
>

> Analysis of Variance Table
>
> Response: Y Df Sum Sq Mean Sq F value Pr(>F) A 2 0.4735 0.23675
> 0.2845 0.7620 A:B 3 1.7635 0.58783 0.7064 0.5823 Residuals 6 4.9931
> 0.83218
>
> Evidently, R calculates the F value for A as MS_A / MS_Residuals.
> While it is straightforward enough to calculate what I think is the
> correct result from the table, I am surprised that R doesn't give me
> that answer directly. Does anybody know if R's behavior is intentional,
> and if so, why? And, perhaps most importantly, how to get the "textbook"
> result in the most straightforward way? (I'd like to be able to give me
> students a simple procedure...)

The formula that you specify is based upon factor "B" being a *random* 
effect.  The lm() function handles *fixed* effects only, and thus treats 
"B" as a fixed effect --- whether this makes any sense or not is another 
story.  (IMHO only random effects make sense as nested effects.)

Kevin Wright has already told you how to get what you want/need using 
aov() and the Error() function.  This works only for balanced designs, 
essentially.  For more complicated designs you will need to dive into 
the nlme and lme4 packages.  For which you will need *lots* of patience, 
determination, and luck! :-)

cheers,

Rolf Turner

P. S. Please provide a useful *subject line* in your posts to this list.

R. T.

-- 
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276



More information about the R-help mailing list