[R] Two-way ANOVA gives different results using anova(lm()) than doing it by hand

Greg Snow Greg.Snow at imail.org
Wed Jul 8 19:11:14 CEST 2009


Well, since we don't have Data.txt it is kind of hard for us to replicate what you have done.

Here goes a guess as to what the problem may be.

Have you told R anywhere that S1 and S2 are factors with 6 levels rather than numeric vectors? Or are you just hoping that the computer can read your mind to find out this information?  

(reading minds is one of the things that R and computers in general are not very good at yet.  I have made a note to my future self to use the TimeTravel package to send a copy of the ESP package back to my past self, but I have not received it yet).


-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Lars Bergemann
> Sent: Wednesday, July 08, 2009 8:35 AM
> To: r-help at r-project.org
> Subject: [R] Two-way ANOVA gives different results using anova(lm())
> than doing it by hand
> 
> 
> Hey!
> 
> 
> 
> Could you please take a quick look at what I have done? Somehow I get
> wrong results using the anova(lm()) combination compared to doing a two
> way ANOVA by hand.
> 
> 
> 
> Running:
> 
> 
> 
> Data<-read.table("Data.txt");
> g<-lm(ExM~S1*S2,Data);
> anova(g);
> 
> 
> 
> Gives:
> 
> 
> 
> Analysis of Variance Table
> 
> Response: ExM
>            Df Sum Sq Mean Sq F value    Pr(>F)
> S1          1 4.3679  4.3679 167.045 < 2.2e-16 ***
> S2          1 0.9427  0.9427  36.053 8.236e-09 ***
> S1:S2       1 0.3231  0.3231  12.357 0.0005371 ***
> Residuals 212 5.5434  0.0261
> 
> 
> I compared it to the work done by hand, ie calculated all the different
> square sums using sum() and tapply().
> 
> So I know that anova(lm()) gets the degrees of freedom equal two 1, 1,
> 1 and 212 when it should be 5, 5, 25 and 180. Also, the square sums are
> quite different ... I get 4.xx, 4.xx, 1.xx, 0.xx ... as you see, what
> anova(lm()) gets is different.
> 
> 
> 
> The data: S1 has 6 levels, so has S2. On average, each cell has 6
> values, most cells have actually 6 values, and there are two of each:
> 5, 7, 4, 8 - so average 6.
> 
> 
> 
> Could you please help me, why it does not work with anova(lm())? I
> tried quite a few thinks found with Google, but it all gave me the same
> result as anova(lm()) ...
> 
> 
> 
> Thanks a lot!
> 
> 
> 
> Lars
> 
> _________________________________________________________________
> 
> 




More information about the R-help mailing list