# Unbalanced two-way ANOVA in R Studio

I have a dataset that is unbalanced. I am testing how temperature and the size of a carcass affect the development rate of maggots. The duration is the time spent in a particular development stage of the maggot. I found the higher the temperature and the larger the carcass, the faster development (shorter duration).

My response variable is Duration of Eggs (Eggs for short in coding) and my two factors are Temperature (4 levels = 15, 20, 25, 30) and Size (2 levels = small and large). The majority of the sample sizes are 4; however one group is 7.

I intend to examine how Duration of Eggs varies with Temperature and Size. I want to use ANOVA and after much reading I think two-way unbalanced ANOVA should be used.

I imported my data set (anova.data). One function I have tried is:

anova(lm(Eggs ~ Temperature * Size, anova.data))


This gave me:

Analysis of Variance Table
Response: Eggs
Df  Sum Sq Mean Sq F value Pr(>F)
Temperature       1 1828.37 1828.37 71.3971 1.521e-09 ***
Size              1    1.71    1.71  0.0669 0.7977
Temperature:Size  1    1.02    1.02  0.0399 0.8429
Residuals        31  793.86    25.61
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


However, I am uncertain if this takes into account that it is unbalanced. After further reading I found the function Anova() [in car package] can be used to compute two-way ANOVA test for unbalanced designs. Out of the three fundamentally different ways to run an ANOVA in an unbalanced design, I read that the recommended method is the Type-III sums of squares. (Not sure why this is though).

So (after install.packages("car")), I tried a second function:

library(car)
my_anova <- aov(Eggs ~ Temperature * Size, data = anova.data)
Anova(my_anova, type = "III")
Anova Table (Type III tests)
Response: Eggs
Sum Sq  Df F value Pr(>F)
(Intercept)     2875.68  1 112.2941 7.883e-12 ***
Temperature      858.05  1  33.5065 2.243e-06 ***
Size               0.45  1 0.0178 0.8948
Temperature:Size   1.02  1 0.0399 0.8429
Residuals        793.86 31
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


However, this function gives different values and this second function has an additional Intercept value, which I am not familiar with. Which function is correct to use? Or is there an alternative function?

Also, how do I know if to use Type I, II and III sums of squares? I have done some reading but I am still unsure. I do not know if there is an interaction between Temperature and Size.

This is my dataset:

15°C    Small:  43.0, 43.0, 43.0, 43.0
15°C    Large:  40.5, 40.5, 40.5, 40.5
20°C    Small:  24.0, 24.0, 24.0, 23.5, 23.5, 23.5, 23.5
20°C    Large:  24.0, 24.0, 24.0, 24.0
25°C    Small:  20.0, 20.0, 20.0, 20.0
25°C    Large:  20.0, 20.0, 20.0, 20.0
30°C    Small:  20.0, 20.0, 20.0, 20.0
30°C    Large:  20.0, 20.0, 20.0, 20.0

• Despite the emphasis on R, this does look like a statistical question. You have (if I understand correctly) about $4 \times 2 \times 5 = 40$ observations; that's a small enough dataset to post. Jan 26, 2017 at 18:46
• Text says Duration is the response; code says Eggs. Please confirm that they are the same variable. Jan 26, 2017 at 18:47
• I agree with the comments made by Nick in his answer. A few additional comments: (1) What you have done is not a two-way ANOVA, because you have treated Temperature as a continuous variable (this is indicated by the Df=1 for Temperature). To do a two-way ANOVA, you have to treat Temperature as a factor by first doing anova.data$Temperature <- factor(anova.data$Temperature), although this isn't necessarily a good thing to do. Jan 29, 2017 at 9:15
• (2) If you're going to use Anova() with type="III", you should use "sum" contrasts instead of the default "treatment" contrasts: Anova(lm(Eggs ~ Temperature*Size, data=anova.data, contrasts=list(Temperature="contr.sum", Size="contr.sum")), type="III"). (3) It would be easier to help you if you posted your data in a more convenient format. For R users, you can paste the output of dput(anova.data). Jan 29, 2017 at 9:16
• @mark999, Does the contrast make any difference? I think the contrast functions only affect how you interpret the result but does not affect the statistical inference (e.g. p-value). And I am not sure why the contrast has to be contr.sum for type III. Actually, I am not even sure that they are related.
– WCMC
May 23, 2018 at 22:09

Thanks for posting the data. I suspect that this version will be easier for many people to work with.

Temp Size Duration
15 Small 43
15 Small 43
15 Small 43
15 Small 43
15 Large 40.5
15 Large 40.5
15 Large 40.5
15 Large 40.5
20 Small 24
20 Small 24
20 Small 24
20 Small 23.5
20 Small 23.5
20 Small 23.5
20 Small 23.5
20 Large 24
20 Large 24
20 Large 24
20 Large 24
25 Small 20
25 Small 20
25 Small 20
25 Small 20
25 Large 20
25 Large 20
25 Large 20
25 Large 20
30 Small 20
30 Small 20
30 Small 20
30 Small 20
30 Large 20
30 Large 20
30 Large 20
30 Large 20


I have two comments by way of an answer. I have to say that these data seem very strange. I doubt the utility of any kind of analysis of variance here. Whether your approach is appropriate statistically and scientifically needs to be clear before you worry about how to do it.

First off, there is almost no variability within groups defined by the same factors. That is a real surprise for any kind of data, and certainly for biological data.

Second, the big deal is being at 15$^\circ$C and a medium deal is at being at 20$^\circ$C. Size seems to have a minor or even negligible effect.

Perhaps you need a model with non-linear forcing of temperature. I can't say what functional forms best suit the underlying science here.

P.S. If I were trying to publish a similar graph, I would be more careful about giving measurement units. But this is just an exploratory graph.

• Whoever downvoted is welcome to explain their dissent with this or to post a better answer. Jan 29, 2017 at 8:38
• The term "very strange" to describe the data-set is an example of the famous British habit of understatement. Jan 29, 2017 at 13:23
• The data set I have provided is only for Eggs and is the first ANOVA test I want to compute. I will then use the same approach to analyse five other development stages (1st instar, 2nd instar, 3rd instar, postfeed and pupa). These data sets have more variability. I would post them however I am unfamiliar with Markdown and can not present it clearly. Jan 30, 2017 at 14:07
• A colleague gave me the response "both the means and p-values are different when using unweighted means and Type III SS compared to weighted means and Type I SS. In certain cases, this difference can be quite pronounced and lead to entirely different outcomes between the two methods. Hence, choosing the appropriate means and SS for a given analysis is a matter that should be approached with conscious consideration." Which one to use is perhaps the first answer I require. Thanks. Jan 30, 2017 at 14:11
• There is very little to learn about posting output and/or data in questions. You mainly need to use the {} button in the editor to ensure that line breaks are respected. I wouldn't regard the data you posted as being (easily) publishable or usable in a report. Rightly or wrongly, the extreme lack of variability strikes me as implausible. Jan 30, 2017 at 14:14