0
$\begingroup$

I have a dataset like this:

> test
         size   wing genotype season
1   1645.3125 545751       KO     DS
2    365.4297 592994       KO     DS
3    415.7227 640617       KO     DS
4    235.4492 613177       KO     DS
7   1840.9180 553981       WT     DS
8    532.4219 607689       WT     DS
9    811.9141 640617       WT     DS
10   629.1016 613177       WT     DS
155  470.8008 577800       KO     WS
156 1384.9609 559628       KO     WS
157 1219.9219 577766       KO     WS
158  240.3320 551281       KO     WS
159  955.7617 491028       KO     WS
160 3622.2656 557061       WT     WS
161 2962.4023 557715       WT     WS
162 3134.5703 556449       WT     WS
163 2360.2539 572527       WT     WS
164 2032.2266 449802       WT     WS

Now I hope to check the impact of genotype, season, and their interaction, on size value, using wing as a covariate.

I used both the aov() function and the anova_test() function from the rstatix package. Both were performed as type III.

For aov():

> aov=aov(size~wing+season*genotype,data=test)
> Anova(aov,type="III")
Anova Table (Type III tests)

Response: size
                 Sum Sq Df F value  Pr(>F)  
(Intercept)      214667  1  0.5760 0.46142  
wing              87112  1  0.2337 0.63680  
season            18433  1  0.0495 0.82746  
genotype         178372  1  0.4786 0.50122  
season:genotype 2957799  1  7.9367 0.01454 *
Residuals       4844786 13                  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

For anova_test():

> res = test %>% anova_test(size~wing+genotype*season,type = 3)
> get_anova_table(res)
ANOVA Table (type III tests)

           Effect DFn DFd      F     p p<.05   ges
1            wing   1  13  0.234 0.637       0.018
2        genotype   1  13 14.960 0.002     * 0.535
3          season   1  13  6.415 0.025     * 0.330
4 genotype:season   1  13  7.937 0.015     * 0.379

When I tried type II instead of type III, the two analysis did produce the same results:

> aov=aov(size~wing+season*genotype,data=test)
> Anova(aov,type="II")
Anova Table (Type II tests)

Response: size
                 Sum Sq Df F value   Pr(>F)   
wing              87112  1  0.2337 0.636800   
season          2015502  1  5.4082 0.036867 * 
genotype        6523364  1 17.5041 0.001071 **
season:genotype 2957799  1  7.9367 0.014543 * 
Residuals       4844786 13                    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> res = test %>% anova_test(size~wing+genotype*season,type = 2)
> get_anova_table(res)
ANOVA Table (type II tests)

           Effect DFn DFd      F     p p<.05   ges
1            wing   1  13  0.234 0.637       0.018
2        genotype   1  13 17.504 0.001     * 0.574
3          season   1  13  5.408 0.037     * 0.294
4 genotype:season   1  13  7.937 0.015     * 0.379

Why I got different results for typeIII, but not typeII, from the two functions?

$\endgroup$

1 Answer 1

1
$\begingroup$

The different type-3 tables from car and rstatix are using different contrasts for the main effects. From the documentation in rstatix::anova_test() (emphasis mine)

By default, R uses treatment contrasts, where each of the levels is compared to the first level used as baseline. The default contrast can be checked using options('contrasts'). In the function anova_test(), the following setting is used options(contrasts=c('contr.sum','contr.poly')), which gives orthogonal contrasts where you compare every level to the overall mean. This setting gives the same output as the most commonly used commercial softwares, like SPSS. If you want to obtain the same result with the function car::Anova() as the one obtained with rstatix::anova_test(), then don't forget to set options(contrasts=c('contr.sum','contr.poly')).

From the car::Anova documentation (emphasis mine):

Be careful of type-III tests: For a traditional multifactor ANOVA model with interactions, for example, these tests will normally only be sensible when using contrasts that, for different terms, are orthogonal in the row-basis of the model, such as those produced by contr.sum, contr.poly, or contr.helmert, but not by the default contr.treatment. In a model that contains factors, numeric covariates, and interactions, main-effect tests for factors will be for differences over the origin. In contrast (pun intended), type-II tests are invariant with respect to (full-rank) contrast coding. If you don't understand this issue, then you probably shouldn't use Anova for type-III tests.

So if you want the car::Anova() output to match the rstatix output, use:

> options(contrasts=c('contr.sum','contr.poly'))
> Anova(aov,type="III")

Anova Table (Type III tests)

Response: size
                 Sum Sq Df F value   Pr(>F)   
(Intercept)      423852  1  1.1373 0.305625   
wing              87112  1  0.2337 0.636800   
season          2390652  1  6.4148 0.024995 * 
genotype        5575275  1 14.9601 0.001941 **
season:genotype 2957799  1  7.9367 0.014543 * 
Residuals       4844785 13                  

But be careful with the interpretation of main effects from type-3 ANOVA tables when you have interactions in the model. Eg the model implies that the effect of season depends on genotype and vice versa, so you'll need to be clear in your report exactly what contrast is being made in each case.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.