Homogeneity of variance

    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 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.

    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.

    Quick boxplots of these data sets:

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

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

    With one independent variable:

    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. bartlett.test(len ~ dose, data=ToothGrowth)
    8. #>
    9. #> Bartlett test of homogeneity of variances
    10. #>
    11. #> Bartlett's K-squared = 0.66547, df = 2, p-value = 0.717

    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.

    With one independent variable:

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