Two-way ANOVA_ToothGrowth

Note: This article shows an R example on how to conduct a two-way analysis of variance (ANOVA) on the dataset ToothGrowth. For general information about ANOVA please refer to the following article: ANOVA.

In short: In this article an exemplary two-way ANOVA is conducted on the dataset ToothGrowth which examines if the Growth of the teeth in guinea pigs differ dependent on different treatments with vitamin C and if there is an interaction effect between the dose of vitamin C and the supplement in which it is provided.

The Dataset[edit]

The dataset ToothGrowth is provided in the R base package and contains information on the growth of teeth in guinea pigs and the different types and doses of treatment with vitamin C they received.

The variables we will look at in our analysis are:

  • len: a numeric variable containing the length of cells responsible for the growth of teeth in guinea pigs.
  • supp: a factorial variable indicating the type of vitamin C supplement the guinea pig received (VC for ascorbic acid, OJ for orange juice).
  • dose: a numeric variable containing the dose of vitamin C that was given to the guinea pig. It is saved as a numeric variable but only appears in the values 0.5, 1 and 2.

Examining the Dataset[edit]

Before conducting the analysis we can examine the dataset and check for the different assumptions of an ANOVA to make sure that the ANOVA produces valid results. The assumptions of the ANOVA are:

  • Normality of the residuals. This can be roughly approximated by checking for the normality of the dependent variable (len)
  • Equal variance for each factor combination
  • Independent measurements
  • Equal sample size in each treatment combination

The variable dose is converted to a factor which is important when performing an ANOVA. The structure of the dataset is reviewed with the command str().

Since there are 60 randomly sampled guinea pigs in the different treatment groups, the measurements in this dataset are independent of each other. To check for the normality of the dependent variable we can have a look at the histogram of len (Fig. 1). The equal sample size of each factor combination is reviewed by creating a table of the factor combinations. The equal variances are checked later when we create a boxplot to visualize the ANOVA results.

data("ToothGrowth")
#View the first rows
head(ToothGrowth)

#convert dose to factor (important for ANOVA)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)

#check structure
str(ToothGrowth)

#check normal distribution with histogram (Fig. 1)
hist(ToothGrowth$len)
#table to check for balanced design
table(ToothGrowth$dose, ToothGrowth$supp)

Output[edit]

> head(ToothGrowth)
   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5
5  6.4   VC  0.5
6 10.0   VC  0.5

> str(ToothGrowth)
'data.frame':	60 obs. of  3 variables:
 $ len : num  4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
 $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
 $ dose: Factor w/ 3 levels "0.5","1","2": 1 1 1 1 1 1 1 1 1 1 ...
>

> table(ToothGrowth$dose, ToothGrowth$supp)
     
      OJ VC
  0.5 10 10
  1   10 10
  2   10 10
>
Fig. 1: Histogram of len

The dependent variable is normally distributed and the design is completely balanced. We can thus say that a classic two-way ANOVA is appropriate.

Conducting Two-way ANOVA[edit]

A two-way ANOVA tests for the main effects of the dose and the supplement as well as the interaction between those two variables. An interaction describes the phenomenon that the two main effects impact each other. In other words an interaction effect means that the outcome of one variable is influenced by another variable. In this case the interaction tests if dose and supplement have an effect on each other.

The two-way ANOVA with interaction term will test three 0 hypothesis which are then printed in the results.

  • The main effect of supplement: len does not differ between OJ and VC if the dose is ignored.
  • The main effect of dose: len does not differ between the three different doses of vitamin C if the type of supplement in which the dose is given is ignored.
  • The interaction effect: the effects of dose and supplement do not impact each other.

The code could also be written as: len~supp+dose+supp:dose. Here it gets more clear that the two main effects and the interaction effect are tested.

#perform two-way ANOVA
anova_model <- aov(len ~ supp * dose, data = ToothGrowth)
summary(anova_model)

The results can be visualized with a boxplot, which is also used to check the aforementioned assumptions, as well as with an interaction plot.

#boxplot of ANOVA results (Fig. 2)
boxplot(len ~ supp * dose, data = ToothGrowth,
        xlab = "Supplement and Dose",
        ylab = "Tooth Length",
        main = "Tooth Growth by Supplement and Dose",
        col = c("lightblue", "lightgreen"))

#check interaction effects visually (Fig. 3)
interaction.plot(ToothGrowth$dose, ToothGrowth$supp, ToothGrowth$len,
                 col = c("blue", "red"),
                 lwd = 2,
                 ylab = "Tooth Length",
                 xlab = "Dose",
                 trace.label = "Sup. Type")

Output[edit]

The analysis delivers the following output:

> anova_model <- aov(len ~ supp * dose, data = ToothGrowth)
> summary(anova_model)
            Df Sum Sq Mean Sq F value   Pr(>F)    
supp         1  205.4   205.4  15.572 0.000231 ***
dose         2 2426.4  1213.2  92.000  < 2e-16 ***
supp:dose    2  108.3    54.2   4.107 0.021860 *  
Residuals   54  712.1    13.2                     
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>

Df: The Degrees of freedom are a measure of the Independent values that can vary in our design. For each factor they are calculated by n-1 (where n is the number of levels of that factors).

Sum Sq: The sum of squares is a measure of how much variance is explained by the independent variables. The group Sum Sq plus the residuals Sum Sq equals to the total variation. If we divide Sum Sq of one variable by the total variation, we get the explained variation by variable group. Dose for example explains 2426.4 out of 3452.2 of the variation which is quite a large amount. That the dose of vitamin C explains more of the variation in the length of the cells than the type of supplement, or its interaction seems plausible.

Pr: The p-value indicates the probability that the null hypothesis for each effect is true. The lower the p-value is the more probable it is that we can reject the null hypothesis and assume a significant effect with a p-value of 0.05 usually being the threshold. As we can see, each effect is significant, with the main effect of dose being the most significant effect and the interaction effect being the least significant.

Altogether we can say that the length of the cells responsible for the growth of teeth in guniea pigs is highly influenced by the dose and the supplement as well as their interaction. In other words, the growth of teeth in guinea pigs is influenced by vitamin C supplements to different degrees.

Fig. 2: Boxplot of len dependent on supp and dose

The effect can be visualised in a boxplot (Fig. 2). We can see that there are major differences between the length in the cells between the different doses. A difference can also be observed when it comes to the type of supplement that was provided. In lower doses the vitamin c provided as ascorbic acid seems to have a smaller effect on the growth than the vitamin C provided as orange juice. At a dose of 2 this difference cannot be observed anymore. This shows the interaction effect between supplement and dose.

This boxplot can also be used to check for the assumption of equal variance. When quickly taking a look at the boxes we can see that the variance is not completely equal which is likely if each factor combination only has 10 replicates. An ANOVA can still be performed but might be a little less robust.

Fig. 3:Interaction plot of the effect of supp and dose on len

The interaction plot (Fig. 3) plots the means of the length dependent on the dose. Each type of supplement is indicated as a different line. If the lines are parallel we don´t assume an interaction effect. The less parallel the lines are, the stronger the interaction effect is. To determine how significant the interaction effect is, we need to look at the results of the two-way ANOVA which shows a significant interaction.

Post-hoc Tests[edit]

The result of the ANOVA only indicates that there is a significant difference between the treatment groups but does not contain information on which treatment groups differ. Therefore a TukeyHSD test is performed that tests which groups differ while correcting for type I [link] errors. Every treatment combination is tested against every other combination. Since there is an interaction effect, the main effects cannot be analysed separately because there is no single effect of dose or of supplement. These effects are thus ignored.

#TukeyHSD test on model
TukeyHSD(anova_model)

Output[edit]

The TukeyHSD test delivers the following results:

> TukeyHSD(anova_model)
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = len ~ supp * dose, data = ToothGrowth)

$supp
      diff       lwr       upr     p adj
VC-OJ -3.7 -5.579828 -1.820172 0.0002312

$dose
        diff       lwr       upr   p adj
1-0.5  9.130  6.362488 11.897512 0.0e+00
2-0.5 15.495 12.727488 18.262512 0.0e+00
2-1    6.365  3.597488  9.132512 2.7e-06

$`supp:dose`
               diff        lwr        upr     p adj
VC:0.5-OJ:0.5 -5.25 -10.048124 -0.4518762 0.0242521
OJ:1-OJ:0.5    9.47   4.671876 14.2681238 0.0000046
VC:1-OJ:0.5    3.54  -1.258124  8.3381238 0.2640208
OJ:2-OJ:0.5   12.83   8.031876 17.6281238 0.0000000
VC:2-OJ:0.5   12.91   8.111876 17.7081238 0.0000000
OJ:1-VC:0.5   14.72   9.921876 19.5181238 0.0000000
VC:1-VC:0.5    8.79   3.991876 13.5881238 0.0000210
OJ:2-VC:0.5   18.08  13.281876 22.8781238 0.0000000
VC:2-VC:0.5   18.16  13.361876 22.9581238 0.0000000
VC:1-OJ:1     -5.93 -10.728124 -1.1318762 0.0073930
OJ:2-OJ:1      3.36  -1.438124  8.1581238 0.3187361
VC:2-OJ:1      3.44  -1.358124  8.2381238 0.2936430
OJ:2-VC:1      9.29   4.491876 14.0881238 0.0000069
VC:2-VC:1      9.37   4.571876 14.1681238 0.0000058
VC:2-OJ:2      0.08  -4.718124  4.8781238 1.0000000
>

The results of the TukeyHSD test show that most of the factor combinations differ significantly. Especially interesting is that at high doses the difference between the type of supplement provided disappears. This could potentially be due to a saturation of the effect of vitamin C on tooth growth. This would however need to be further investigated.

Diagnostic plots[edit]

To evaluate the validity of our mathematical model and test once again if the mathematical assumptions are met we can plot the model in diagnostic plots (Fig. 4).

#evaluating model 
par(mfrow = c(2, 2))
plot(anova_model)

Output[edit]

Fig. 4: Diagnostic plots of the model

The residuals vs. fitted shows the fitted (predicted) values by the model and the residual/ error of this observation. The dots should ideally be equally distributed across the plot. When evaluating an ANOVA the factors can still be observed, which is unavoidable in an ANOVA. In this model there seems to be roughly the same amount of variance although the distribution could be more equal. The Q-Q plot shows the theoretical quantiles of the normal distribution on the x axis and the ordered values of the residuals from the model. In this plot the residuals should roughly follow the line. Alternatively, one can look at the distribution of the residuals on all factor combination, ideally with boxplots or histograms.

Further Information[edit]

ToothGrowth dataset: RDocumentation

aov() command: RDocumentation

Interaction plot: RDocumentation

ANOVA: Cookbook for R


The author of this entry is Jana Simon.