**Contents**

Comparing two variances is useful in several cases, including:

When you want to perform a two samples t-test to check the equality of the variances of the two samples

When you want to compare the variability of a new measurement method to an old one. Does the new method reduce the variability of the measure?

Typical research questions are:

- whether the variance of group A (\(\sigma^2_A\))
*is equal*to the variance of group B (\(\sigma^2_B\))? - whether the variance of group A (\(\sigma^2_A\))
*is less than*the variance of group B (\(\sigma^2_B\))? - whether the variance of group A (\(\sigma^2_A\))
*is greather than*the variance of group B (\(\sigma^2_B\))?

In statistics, we can define the corresponding *null hypothesis* (\(H_0\)) as follow:

- \(H_0: \sigma^2_A = \sigma^2_B\)
- \(H_0: \sigma^2_A \leq \sigma^2_B\)
- \(H_0: \sigma^2_A \geq \sigma^2_B\)

The corresponding *alternative hypotheses* (\(H_a\)) are as follow:

- \(H_a: \sigma^2_A \ne \sigma^2_B\) (different)
- \(H_a: \sigma^2_A > \sigma^2_B\) (greater)
- \(H_a: \sigma^2_A < \sigma^2_B\) (less)

Note that:

- Hypotheses 1) are called
**two-tailed tests** - Hypotheses 2) and 3) are called
**one-tailed tests**

The test statistic can be obtained by computing the ratio of the two variances \(S_A^2\) and \(S_B^2\).

\[F = \frac{S_A^2}{S_B^2}\]

The degrees of freedom are \(n_A - 1\) (for the numerator) and \(n_B - 1\) (for the denominator).

Note that, the more this ratio deviates from 1, the stronger the evidence for unequal population variances.

Note that, the F-test requires the two samples to be normally distributed.

The R function **var.test**() can be used to compare two variances as follow:

```
# Method 1
var.test(values ~ groups, data,
alternative = "two.sided")
# or Method 2
var.test(x, y, alternative = "two.sided")
```

**x,y**: numeric vectors**alternative**: the alternative hypothesis. Allowed value is one of “two.sided” (default), “greater” or “less”.

To import your data, use the following R code:

```
# If .txt tab file, use this
my_data <- read.delim(file.choose())
# Or, if .csv file, use this
my_data <- read.csv(file.choose())
```

Here, we’ll use the built-in R data set named ToothGrowth:

```
# Store the data in the variable my_data
my_data <- ToothGrowth
```

To have an idea of what the data look like, we start by displaying a random sample of 10 rows using the function **sample_n**()[in **dplyr** package]:

```
library("dplyr")
sample_n(my_data, 10)
```

```
len supp dose
43 23.6 OJ 1.0
28 21.5 VC 2.0
25 26.4 VC 2.0
56 30.9 OJ 2.0
46 25.2 OJ 1.0
7 11.2 VC 0.5
16 17.3 VC 1.0
4 5.8 VC 0.5
48 21.2 OJ 1.0
37 8.2 OJ 0.5
```

We want to test the equality of variances between the two groups OJ and VC in the column “supp”.

F-test is very sensitive to departure from the normal assumption. You need to check whether the data is normally distributed before using the F-test.

Shapiro-Wilk test can be used to test whether the normal assumption holds. It’s also possible to use **Q-Q plot** (quantile-quantile plot) to graphically evaluate the normality of a variable. Q-Q plot draws the correlation between a given sample and the normal distribution.

If there is doubt about normality, the better choice is to use **Levene’s test** or **Fligner-Killeen test**, which are less sensitive to departure from normal assumption.

```
# F-test
res.ftest <- var.test(len ~ supp, data = my_data)
res.ftest
```

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

**F-test** is p = 0.2331433 which is greater than the significance level 0.05. In conclusion, there is no significant difference between the two variances.

The function **var.test**() returns a list containing the following components:

**statistic**: the value of the F test statistic.**parameter**: the degrees of the freedom of the F distribution of the test statistic.**p.value**: the p-value of the test.**conf.int**: a confidence interval for the ratio of the population variances.**estimate**: the ratio of the sample variances

The format of the **R** code to use for getting these values is as follow:

```
# ratio of variances
res.ftest$estimate
```

```
ratio of variances
0.6385951
```

```
# p-value of the test
res.ftest$p.value
```

`[1] 0.2331433`

This analysis has been performed using **R software** (ver. 3.3.2).

Previously, we described the essentials of R programming and provided quick start guides for importing data into **R**. Additionally, we described how to compute descriptive or summary statistics, correlation analysis, as well as, how to compare sample means using R software.

This chapter contains articles describing **statistical tests** to use for **comparing variances**.

- What is F-test?
- When to you use F-test?
- Research questions and statistical hypotheses
- Formula of F-test
- Compute F-test in R

Read more: —> F-Test: Compare Two Variances in R.

This article describes **statistical tests** for comparing the **variances** of two or more samples.

- Compute
**Bartlett’s test**in R - Compute
**Levene’s test**in R - Compute
**Fligner-Killeen**test in R

Read more: —> Compare Multiple Sample Variances in R.

This analysis has been performed using **R statistical software** (ver. 3.2.4).

This article describes **statistical tests** for comparing the **variances** of two or more samples. Equal variances across samples is called **homogeneity** of **variances**.

Some statistical tests, such as two independent samples T-test and ANOVA test, assume that variances are equal across groups. The **Bartlett’s test**, **Levene’s test** or **Fligner-Killeen’s test** can be used to verify that assumption.

There are many solutions to test for the equality (**homogeneity**) of variance across groups, including:

**F-test**: Compare the variances of two samples. The data must be normally distributed.**Bartlett’s test**: Compare the variances of k samples, where k can be more than two samples. The data must be normally distributed. The Levene test is an alternative to the Bartlett test that is less sensitive to departures from normality.**Levene’s test**: Compare the variances of k samples, where k can be more than two samples. It’s an alternative to the Bartlett’s test that is less sensitive to departures from normality.**Fligner-Killeen test**: a non-parametric test which is very robust against departures from normality.

The **F-test** has been described in our previous article: F-test to compare equality of two variances. In the present article, we’ll describe the tests for comparing more than two variances.

For all these tests (**Bartlett’s test**, **Levene’s test** or **Fligner-Killeen’s test**),

- the null hypothesis is that all populations variances are equal;
- the alternative hypothesis is that at least two of them differ.

To import your data, use the following R code:

```
# If .txt tab file, use this
my_data <- read.delim(file.choose())
# Or, if .csv file, use this
my_data <- read.csv(file.choose())
```

Here, we’ll use ToothGrowth and PlantGrowth data sets:

```
# Load the data
data(ToothGrowth)
data(PlantGrowth)
```

To have an idea of what the data look like, we start by displaying a random sample of 10 rows using the function **sample_n**()[in **dplyr** package]. First, install dplyr package if you don’t have it: **install.packages(“dplyr”)**.

Show 10 random rows:

```
set.seed(123)
# Show PlantGrowth
dplyr::sample_n(PlantGrowth, 10)
```

```
weight group
24 5.50 trt2
12 4.17 trt1
25 5.37 trt2
26 5.29 trt2
2 5.58 ctrl
14 3.59 trt1
22 5.12 trt2
13 4.41 trt1
11 4.81 trt1
21 6.31 trt2
```

```
# PlantGrowth data structure
str(PlantGrowth)
```

```
'data.frame': 30 obs. of 2 variables:
$ weight: num 4.17 5.58 5.18 6.11 4.5 4.61 5.17 4.53 5.33 5.14 ...
$ group : Factor w/ 3 levels "ctrl","trt1",..: 1 1 1 1 1 1 1 1 1 1 ...
```

```
# Show ToothGrowth
dplyr::sample_n(ToothGrowth, 10)
```

```
len supp dose
28 21.5 VC 2.0
40 9.7 OJ 0.5
34 9.7 OJ 0.5
6 10.0 VC 0.5
51 25.5 OJ 2.0
14 17.3 VC 1.0
3 7.3 VC 0.5
18 14.5 VC 1.0
50 27.3 OJ 1.0
46 25.2 OJ 1.0
```

```
# ToothGrowth data structure
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: num 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
```

Note that, R considers the column “dose” [in ToothGrowth data set] as a numeric vector. We want to convert it as a grouping variable (factor).

`ToothGrowth$dose <- as.factor(ToothGrowth$dose)`

We want to test the equality of variances between groups.

The R function **bartlett.test**() can be used to compute Barlett’s test. The simplified format is as follow:

`bartlett.test(formula, data)`

**formula**: a formula of the form values ~ groups**data**: a matrix or data frame

The function returns a list containing the following component:

**statistic**: Bartlett’s K-squared test statistic**parameter**: the degrees of freedom of the approximate chi-squared distribution of the test statistic.**p.value**: the p-value of the test

To perform the test, we’ll use the *PlantGrowth* data set, which contains the weight of plants obtained under 3 treatment groups.

**Bartlett’s test with one independent variable**:

```
res <- bartlett.test(weight ~ group, data = PlantGrowth)
res
```

```
Bartlett test of homogeneity of variances
data: weight by group
Bartlett's K-squared = 2.8786, df = 2, p-value = 0.2371
```

From the output, it can be seen that the p-value of 0.2370968 is not less than the significance level of 0.05. This means that there is no evidence to suggest that the variance in plant growth is statistically significantly different for the three treatment groups.

**Bartlett’s test with multiple independent variables**: the**interaction**() function must be used to collapse multiple factors into a single variable containing all combinations of the factors.

`bartlett.test(len ~ interaction(supp,dose), data=ToothGrowth)`

```
Bartlett test of homogeneity of variances
data: len by interaction(supp, dose)
Bartlett's K-squared = 6.9273, df = 5, p-value = 0.2261
```

As mentioned above, Levene’s test is an alternative to Bartlett’s test when the data is not normally distributed.

The function **leveneTest**() [in **car** package] can be used.

```
library(car)
# Levene's test with one independent variable
leveneTest(weight ~ group, data = PlantGrowth)
```

```
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 2 1.1192 0.3412
27
```

```
# Levene's test with multiple independent variables
leveneTest(len ~ supp*dose, data = ToothGrowth)
```

```
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 5 1.7086 0.1484
54
```

The **Fligner-Killeen test** is one of the many tests for homogeneity of variances which is most robust against departures from normality.

The R function **fligner.test**() can be used to compute the test:

`fligner.test(weight ~ group, data = PlantGrowth)`

```
Fligner-Killeen test of homogeneity of variances
data: weight by group
Fligner-Killeen:med chi-squared = 2.3499, df = 2, p-value = 0.3088
```

This analysis has been performed using **R software** (ver. 3.2.4).