3.12 Analysis of Variance

Analysis of Variance (ANOVA) is an extension of the t-test to compare means between multiple groups.

ANOVA works by comparing two different sources of variation in the dataset: within-group variance and between-group variance. Consider the example using variation in birthweight by gender. Suppose that there is no true difference between mean birthweights by gender. If there is no true difference, then the means in our data are different only because their sample means happen to vary about a single underlying (unknown) population mean.

Consider if all birthweights were taken as one sample. These data would have some level of variation. If the null hypothesis were true, we could split this sample into two subsamples by gender and expect that the variance within each subsample (within-group variance) is the same as the variance of the overall sample and thus there is only a small amount of difference or variance between the two groups - a difference completely attributable to sampling variation instead of any systematic difference. If the null hypothesis were false and there is a difference by gender, we would expect the between-group variance (that is, how the groups are different from each other) to be greater than the within group variance (how individuals within each group are different).

To fit an ANOVA we use the R command aov(), which takes a formula as its input. Let’s replicate our t-test with an ANOVA. The default output is not particularly helpful to interpreting the test, so we assign our ANOVA the name anova1 and call for a summary() of the results. Note that the ANOVA result appears now in the Environment tab in the upper right and we can access it at any point. Note that the ANOVA formula is the same as the t-test formula.

An ANOVA has three major assumptions: normality, homoskedasticity (or equality of variance), and independence. We have already examined normality and homoskedasticity when we examined the plots of birthweight by gender, and independence holds by design (whether a given baby is male or female is independent of any other baby genders). Stata automatically tests for homoskedasticity when performing an ANOVA using Bartlett’s test but in R it is a separate command, bartlett.test(). Bartlett’s test is a generalisation to more than one variance of the homogeneity of variance test we conducted for the t-test.

#--- Run the ANOVA
anova1 <- bab9 %$% aov(bweight ~ sex)
##              Df    Sum Sq Mean Sq F value Pr(>F)   
## sex           1   4476041 4476041    10.7 0.0012 **
## Residuals   639 268244081  419787                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#--- Conduct Bartlett's test
bab9 %$% bartlett.test(bweight ~ sex)
##  Bartlett test of homogeneity of variances
## data:  bweight by sex
## Bartlett's K-squared = 1, df = 1, p-value = 0.3

In this case, we note that there is strong evidence against the null hypothesis that there is no true difference in birthweights by gender. The probability that we obtained a difference this large or larger under the null hypothesis is 0.0012. Note this is the same p-value as given by the t-test.