[R] Nested anovas in R not doing what they ought to...

Bill.Venables@cmis.csiro.au Bill.Venables at cmis.csiro.au
Fri Jul 12 01:37:39 CEST 2002


It has nothing to do with your syntax, but everything to do with your
semantics.

One thing you appear not to have done is to declare habitat, fish, lake as
factors (unless each of these has only two levels).  That is why you are
getting one degree of freedom for every term.  This is often a clear signal
that something is amiss.

In R the term A/B is only a shorthand for A + A:B, which is (mathematically
isomorphic to) a nested model.  Also A*B is only a shorthnd for A + B + A:B,
which is the crossed model.  The meaning (= semantics) of the A:B term
depends on what has gone into the model before, and in these two cases it is
different and will in general have a different number of degrees of freedom
in the anova table.  It is very rare indeed that you really need to use ":"
or "%in%" as formula operators explicitly, +, -, * and / generally cover the
field.

Please don't expect the world always to behave like SAS, it is not the gold
standard.  There is none, in fact.  Like so many software environments,
including R and S-PLUS, SAS presents only one view of a complex and
intriguing reality, and at times that is a quirky view.  You need to
understand these things at a level of abstraction that no software
environment currently provides.

Bill Venables.

-----Original Message-----
From: Michael Rennie [mailto:mike.rennie at ene.gov.on.ca]
Sent: Friday, July 12, 2002 1:08 AM
To: R-help at stat.math.ethz.ch
Subject: [R] Nested anovas in R not doing what they ought to...



Hi, there

I first sent this e-mail a couple months ago, to no avail.Since I am not a
member on your mailing list, so could you please cc: a response to me? I'll
be sure to check the list today for replies.

I am currently attempting to perform an ANOVA with both nested and normal
factors.  My problem is that R is treating my nested factors the exact same
way as it would interaction terms.  

My output for the nested model is as follows:

> nested <- anova(lm(ltotinv ~ habitat + fish/lake + habitat:fish +
habitat:(la\
ke %in% fish)))
> nested
Analysis of Variance Table
 
Response: ltotinv
                  Df  Sum Sq Mean Sq F value    Pr(>F)
habitat            1 17.0829 17.0829 50.0917 3.083e-09 ***
fish               1  0.9131  0.9131  2.6774  0.107600
habitat:fish       1 13.0100 13.0100 38.1488 8.877e-08 ***
fish:lake          1  1.0256  1.0256  3.0072  0.088599 .
habitat:fish:lake  1  2.7641  2.7641  8.1051  0.006229 **
Residuals         54 18.4158  0.3410
---lysis of Variance Table
Signif. codes:  0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1

With just interactions, not nested:

> test <- anova(lm(ltotinv ~ habitat + fish + lake:fish + habitat:fish +
fish:h\
abitat:lake))
> test
Analysis of Variance Table
 
Response: ltotinv
                  Df  Sum Sq Mean Sq F value    Pr(>F)
habitat            1 17.0829 17.0829 50.0917 3.083e-09 ***
fish               1  0.9131  0.9131  2.6774  0.107600
habitat:fish       1 13.0100 13.0100 38.1488 8.877e-08 ***
fish:lake          1  1.0256  1.0256  3.0072  0.088599 .
habitat:fish:lake  1  2.7641  2.7641  8.1051  0.006229 **
Residuals         54 18.4158  0.3410
---
Signif. codes:  0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
>


We get the same answer.  The reason I figured this out is because I did this
analysis first in SAS, and then tried to reproduce it in R.  It must be
something to do with my syntax, I imagine, but I can't see what it is.

Any help anyone has would be greatly appreciated.


Sincerely, 

Mike Rennie
M.Sc. Candidate, University of Toronto at Mississauga 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list