[R] Why do I get different results for type III anova using the drop1 or Anova command?

John Fox jfox at mcmaster.ca
Mon Oct 8 15:08:39 CEST 2012


Dear Christiane,

I found the code and output that you attached difficult to read, but I see
at least one error: I'm not sure why you think that Anova() takes a
contrasts argument. It doesn't; contrasts is an argument to lm(). See ?Anova
and ?lm.

Here's a reproducible example that shows that you get the same tests from
Anova() and drop1():

---------------- snip ------------

> # proper "type III" tests:
> mod.1 <- lm(conformity ~ fcategory*partner.status, data=Moore, 
+           contrasts=list(fcategory=contr.poly, partner.status=contr.sum))

> drop1(mod.1, .~.,test="F")
Single term deletions

Model:
conformity ~ fcategory * partner.status
                         Df Sum of Sq     RSS    AIC F value   Pr(>F)   
<none>                                 817.76 142.50                    
fcategory                 2    36.019  853.78 140.44  0.8589 0.431492   
partner.status            1   239.562 1057.33 152.06 11.4250 0.001657 **
fcategory:partner.status  2   175.489  993.25 147.25  4.1846 0.022572 * 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

> Anova(mod.1, type=3)
Anova Table (Type III tests)

Response: conformity
                         Sum Sq Df  F value    Pr(>F)    
(Intercept)              5752.8  1 274.3592 < 2.2e-16 ***
fcategory                  36.0  2   0.8589  0.431492    
partner.status            239.6  1  11.4250  0.001657 ** 
fcategory:partner.status  175.5  2   4.1846  0.022572 *  
Residuals                 817.8 39                       
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 


> # hypotheses not sensible:
> mod.2 <- lm(conformity ~ fcategory*partner.status, data=Moore)
> drop1(mod.2, .~.,test="F") 
Single term deletions

Model:
conformity ~ fcategory * partner.status
                         Df Sum of Sq    RSS    AIC F value  Pr(>F)  
<none>                                817.76 142.50                  
fcategory                 2    89.674 907.44 143.18  2.1383 0.13147  
partner.status            1     2.201 819.97 140.62  0.1050 0.74767  
fcategory:partner.status  2   175.489 993.25 147.25  4.1846 0.02257 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

> Anova(mod.2, type=3)
Anova Table (Type III tests)

Response: conformity
                         Sum Sq Df F value    Pr(>F)    
(Intercept)              984.14  1 46.9348 3.436e-08 ***
fcategory                 89.67  2  2.1383   0.13147    
partner.status             2.20  1  0.1050   0.74767    
fcategory:partner.status 175.49  2  4.1846   0.02257 *  
Residuals                817.76 39                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

---------------- snip ------------

I hope that this helps,
 John

-----------------------------------------------
John Fox
Senator McMaster Professor of Social Statistics
Department of Sociology
McMaster University
Hamilton, Ontario, Canada



> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of C. Weiner
> Sent: Sunday, October 07, 2012 7:37 PM
> To: r-help at r-project.org
> Subject: [R] Why do I get different results for type III anova using the
> drop1 or Anova command?
> 
> Dear experts,
> 
> I just noticed that I get different results conducting type III anova
> using drop1 or the Anova command from the car package. I suppose I made
> a mistake and hope you can offer me some help. I have no idea where I
> got wrong and would be very grateful for explaination as R is new
> terrain for me.
> If I run the commands in line, they produce the same results. But if I
> run them in seperate windows, they produce different results.
> 
> What I did in easy words:
> Opened R prompt, ran parts 0 and 1-> got result 1
> Opened new R prompt, ran parts 0 and 2 ->got result 2, ran part 1
> following part 2, got result 2a
> 
> Why are the results different when they are run seperately and why do
> the commands produce the same results when running one after the other?
> Which results can I rely on? I based my script on:
> http://mcfromnz.wordpress.com/2011/03/0 ... explained/
> <http://mcfromnz.wordpress.com/2011/03/02/anova-type-iiiiii-ss-
> explained/>
> but I surely made mistakes.
> 
> Thanks for help.
> Kind regards,
> Christiane
> 
> 
> 
> #0
> library(car)
> P<-read.table("rhoPgegenrhoAgewichtet-mit Befruchtungssystem0912.txt",
> sep="\t",dec=",", header=T,encoding="latin1")
> summary(P)
> str(P)
> 
> #1
> modD3 <- lm(P$rhoPug ~ P$rhoAg*P$dprime*P$Befruchtungssystem, data=P)
> 
> Anova(modD3,contrasts=list(rhoAg=contr.sum, dprime=contr.poly),
> type="III") ###liefert dieses Ergebnis, wenn der Befehl als erster läuft
> #Response: P$rhoPug
> # Sum Sq Df F value Pr(>F)
> #(Intercept) 0.07719 1 4.5075 0.03530 *
> #P$rhoAg 0.00687 1 0.4014 0.52730
> #P$dprime 0.00176 1 0.1029 0.74882
> #P$Befruchtungssystem 0.00250 1 0.1461 0.70280
> #P$rhoAg:P$dprime 0.00931 1 0.5439 0.46193
> #P$rhoAg:P$Befruchtungssystem 0.00001 1 0.0004 0.98343
> #P$dprime:P$Befruchtungssystem 0.00108 1 0.0630 0.80214
> #P$rhoAg:P$dprime:P$Befruchtungssystem 0.00206 1 0.1205 0.72893
> #Residuals 2.70584 158
> 
> 
> #2
> options(contrasts = c("contr.sum","contr.poly")) #liefert ein anderes
> Ergebnis als der Befehl oben, wenn der obere Befehl NICHT gelaufen ist
> modD3 <- lm(P$rhoPug ~ P$rhoAg*P$dprime*P$Befruchtungssystem, data=P)
> drop1(modD3,.~.,test="F") # type III SS and F Test
> P$rhoPug ~ P$rhoAg * P$dprime * P$Befruchtungssystem
> Df Sum of Sq RSS AIC F value Pr(F)
> <none> 2.7058 -667.35
> P$rhoAg 1 0.0131973 2.7190 -668.54 0.7706 0.3814 !!
> P$dprime 1 0.0002240 2.7061 -669.34 0.0131 0.9091 !!
> P$Befruchtungssystem 1 0.0025022 2.7083 -669.20 0.1461 0.7028 !!
> P$rhoAg:P$dprime 1 0.0283677 2.7342 -667.62 1.6565 0.2000 !!
> P$rhoAg:P$Befruchtungssystem 1 0.0000074 2.7058 -669.35 0.0004 0.9834
> P$dprime:P$Befruchtungssystem 1 0.0010790 2.7069 -669.29 0.0630 0.8021
> P$rhoAg:P$dprime:P$Befruchtungssystem 1 0.0020641 2.7079 -669.22 0.1205
> 0.7289
> 
> 
> #2a (command is the same as in 1, but results are different if the
> command is run after the one above)
> Anova(modD3,contrasts=list(Befruchtungssystem="contr.sum",
> dprime="contr.poly"), type="III") #liefert in nachstehend nach dem
> 2.Befehl dieses Ergebnis
> #Response: P$rhoPug #also jetzt dasselbe wie drop1, aber nicht dasselbe,
> das
> #der exakt gleich Befehl alleine geliefert hätte. Was
> #stimmt den nun? Wo liegt mein Fehler?
> # Sum Sq Df F value Pr(>F)
> #(Intercept) 0.06877 1 4.0158 0.04679 *
> #P$rhoAg 0.01320 1 0.7706 0.38136
> #P$dprime 0.00022 1 0.0131 0.90909
> #P$Befruchtungssystem 0.00250 1 0.1461 0.70280
> #P$rhoAg:P$dprime 0.02837 1 1.6565 0.19997
> #P$rhoAg:P$Befruchtungssystem 0.00001 1 0.0004 0.98343
> #P$dprime:P$Befruchtungssystem 0.00108 1 0.0630 0.80214
> #P$rhoAg:P$dprime:P$Befruchtungssystem 0.00206 1 0.1205 0.72893
> #Residuals 2.70584 158
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list