This tutorial shows methods test Homogeneity of Variance (or, Equality of Variance) in R. The methods include **F-test**, **Bartlett’s test**, **Levene’s test**, and **Fligner-Killeen’s test**.

**F-test**: Compare variances of 2 groups. Need assumption of normality.**Bartlett’s test**: Compare variances of 2 or more groups. Need assumption of normality.**Levene’s test**: An alternative to Bartlett’s test, as**Levene’s test**is less sensitive to departures from the assumption of normality.**Fligner-Killeen’s test**: A non-parametric test, and very robust against the departure from the assumption of normality.

## DataSet being Used

R Built-in dataset “**ToothGrowth**” will be used, which has 60 observations of 3 variables.

- Y variable is called “
`len`

“, which is a numerical variable. - One X variable is called “
`dose`

“, which has three levels (i.e., 0.5, 1, and 2 mg). - Another X variable is called “
`supp`

“, which has two levels (i.e., OJ and VC).

> 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 > summary(ToothGrowth) len supp dose Min. : 4.20 OJ:30 Min. :0.500 1st Qu.:13.07 VC:30 1st Qu.:0.500 Median :19.25 Median :1.000 Mean :18.81 Mean :1.167 3rd Qu.:25.27 3rd Qu.:2.000 Max. :33.90 Max. :2.000 > table(ToothGrowth$dose) 0.5 1 2 20 20 20

## Example of F-test

The following is the hypothesis for this F-test to test Homogeneity of Variance.

**Null Hypothesis:**The variances of the two groups are equal.**Alternative Hypothesis:**The variances of the two groups are NOT equal.

We use `var.test()`

to test the homogeneity of variance in R. As we can see the output below, the p-value is 0.2331, suggesting that we failed to reject null hypothesis. Thus, the variances of the two groups are equal.

```
> var.test(len ~ supp, data=ToothGrowth, alternative = "two.sided")
F test to compare two variances
data: len by supp
F = 0.6386, num df = 29, denom df = 29, p-value =
0.2331
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.3039488 1.3416857
sample estimates:
ratio of variances
0.6385951
```

## Examples of Bartlett’s test

Bartlett’s test has the same hypothesis as F-test to test Homogeneity of Variance. The following is the R code and output. The p-value is 0.2331, suggesting that we failed to reject null hypothesis. Thus, the variances of the two groups are equal.

```
> bartlett.test(len ~ supp, data=ToothGrowth)
Bartlett test of homogeneity of variances
data: len by supp
Bartlett's K-squared = 1.4217, df = 1, p-value =
0.2331
```

Since Bartlett’s test can test more than 2 groups, we can also use another X variable, namely `dose`

. Since it has more than 2 groups, the hypothesis writing is slightly different.

**Null Hypothesis:**The variances of the 3 groups are equal.**Alternative Hypothesis:**At least 2 groups’ variables are NOT equal.

The p-value of 0.717 suggests that variances of the 3 groups are equal.

```
> bartlett.test(len ~ dose, data=ToothGrowth)
Bartlett test of homogeneity of variances
data: len by dose
Bartlett's K-squared = 0.66547, df = 2, p-value =
0.717
```

## Example of Levene’s test

We need to use `leveneTest()`

function in `car`

package. Thus, the following R code.

**Null Hypothesis:**The variances of the 3 groups are equal.**Alternative Hypothesis:**At least 2 groups’ variables are NOT equal.

We can see that the p-value is 0.5281. Thus, we failed to reject null hypothesis and concluded that variances of the 3 groups are equal. Thus, we meet the assumption of homogeneity of variance.

```
> library(car)
> leveneTest(len ~ as.factor(dose), data = ToothGrowth)
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 2 0.6457 0.5281
57
```

## Example of Fligner-Killeen’s test

**fligner.test()** is available in **stats** package. The R code and output are below.

**Null Hypothesis:**The variances of the 3 groups are equal.**Alternative Hypothesis:**At least 2 groups’ variables are NOT equal.

The p-value of 0.4996 suggests that variances of the 3 groups are equal.

```
> library(stats)
> fligner.test(len ~ as.factor(dose), data = ToothGrowth)
Fligner-Killeen test of homogeneity of variances
data: len by as.factor(dose)
Fligner-Killeen:med chi-squared = 1.3879, df = 2,
p-value = 0.4996
```

## Reference

- Levene’s test (Wikipedia)
- Bartlett’s test (Wikipedia)
- A Discussion about Levne’s test, Barlettt’s test, and Fligner-Killeen’s Test