Homogeneity of variance

Problem

You want test samples to see for homogeneity of variance (homoscedasticity) – or more accurately. Many statistical tests assume that the populations are homoscedastic.

Solution

There are many ways of testing data for homogeneity of variance. Three methods are shown here.

  • Bartlett’s test - If the data is normally distributed, this is the best test to use. It is sensitive to data which is not non-normally distribution; it is more likely to return a “false positive” when the data is non-normal.
  • Levene’s test - this is more robust to departures from normality than Bartlett’s test. It is in the car package.
  • Fligner-Killeen test - this is a non-parametric test which is very robust against departures from normality.

For all these tests, the null hypothesis is that all populations variances are equal; the alternative hypothesis is that at least two of them differ.

Sample data

The examples here will use the InsectSprays and ToothGrowth data sets. The InsectSprays data set has one independent variable, while the ToothGrowth data set has two independent variables.

  1. head(InsectSprays)
  2. #> count spray
  3. #> 1 10 A
  4. #> 2 7 A
  5. #> 3 20 A
  6. #> 4 14 A
  7. #> 5 14 A
  8. #> 6 12 A
  9. tg <- ToothGrowth
  10. tg$dose <- factor(tg$dose) # Treat this column as a factor, not numeric
  11. head(tg)
  12. #> len supp dose
  13. #> 1 4.2 VC 0.5
  14. #> 2 11.5 VC 0.5
  15. #> 3 7.3 VC 0.5
  16. #> 4 5.8 VC 0.5
  17. #> 5 6.4 VC 0.5
  18. #> 6 10.0 VC 0.5

Quick boxplots of these data sets:

  1. plot(count ~ spray, data = InsectSprays)

plot of chunk unnamed-chunk-2

  1. plot(len ~ interaction(dose,supp), data=ToothGrowth)

plot of chunk unnamed-chunk-3

On a first glance, it appears that both data sets are heteroscedastic, but this needs to be properly tested, which we’ll do below.

Bartlett’s test

With one independent variable:

  1. bartlett.test(count ~ spray, data=InsectSprays)
  2. #>
  3. #> Bartlett test of homogeneity of variances
  4. #>
  5. #> data: count by spray
  6. #> Bartlett's K-squared = 25.96, df = 5, p-value = 9.085e-05
  7. # Same effect, but with two vectors, instead of two columns from a data frame
  8. # bartlett.test(InsectSprays$count ~ InsectSprays$spray)

With multiple independent variables, the interaction() function must be used to collapse the IV’s into a single variable with all combinations of the factors. If it is not used, then the will be the wrong degrees of freedom, and the p-value will be wrong.

  1. bartlett.test(len ~ interaction(supp,dose), data=ToothGrowth)
  2. #>
  3. #> Bartlett test of homogeneity of variances
  4. #>
  5. #> data: len by interaction(supp, dose)
  6. #> Bartlett's K-squared = 6.9273, df = 5, p-value = 0.2261
  7. # The above gives the same result as testing len vs. dose alone, without supp
  8. bartlett.test(len ~ dose, data=ToothGrowth)
  9. #>
  10. #> Bartlett test of homogeneity of variances
  11. #>
  12. #> data: len by dose
  13. #> Bartlett's K-squared = 0.66547, df = 2, p-value = 0.717

Levene’s test

The leveneTest function is part of the car package.

With one independent variable:

  1. library(car)
  2. leveneTest(count ~ spray, data=InsectSprays)
  3. #> Levene's Test for Homogeneity of Variance (center = median)
  4. #> Df F value Pr(>F)
  5. #> group 5 3.8214 0.004223 **
  6. #> 66
  7. #> ---
  8. #> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

With two independent variables. Note that the interaction function is not needed, as it is for the other two tests.

  1. leveneTest(len ~ supp*dose, data=tg)
  2. #> Levene's Test for Homogeneity of Variance (center = median)
  3. #> Df F value Pr(>F)
  4. #> group 5 1.7086 0.1484
  5. #> 54

Fligner-Killeen test

With one independent variable:

  1. fligner.test(count ~ spray, data=InsectSprays)
  2. #>
  3. #> Fligner-Killeen test of homogeneity of variances
  4. #>
  5. #> data: count by spray
  6. #> Fligner-Killeen:med chi-squared = 14.483, df = 5, p-value = 0.01282
  7. # Same effect, but with two vectors, instead of two columns from a data frame
  8. # fligner.test(InsectSprays$count ~ InsectSprays$spray)

The fligner.test function has the same quirks as bartlett.test when working with multiple IV’s. With multiple independent variables, the interaction() function must be used.

  1. fligner.test(len ~ interaction(supp,dose), data=ToothGrowth)
  2. #>
  3. #> Fligner-Killeen test of homogeneity of variances
  4. #>
  5. #> data: len by interaction(supp, dose)
  6. #> Fligner-Killeen:med chi-squared = 7.7488, df = 5, p-value = 0.1706
  7. # The above gives the same result as testing len vs. dose alone, without supp
  8. fligner.test(len ~ dose, data=ToothGrowth)
  9. #>
  10. #> Fligner-Killeen test of homogeneity of variances
  11. #>
  12. #> data: len by dose
  13. #> Fligner-Killeen:med chi-squared = 1.3879, df = 2, p-value = 0.4996