How to Perform Levene’s Test in Python


Levene’s Test is used to determine whether two or more groups have equal variances. It is commonly used because many statistical tests make the assumption that groups have equal variances and Levene’s Test allows you to determine if this assumption is satisified.

This tutorial explains how to perform Levene’s Test in Python.

Example: Levene’s Test in Python

Researchers want to know if three different fertilizers lead to different levels of plant growth. They randomly select 30 different plants and split them into three groups of 10, applying a different fertilizer to each group. At the end of one month they measure the height of each plant.

Use the following steps to perform Levene’s Test in Python to determine whether or not the three groups have equal variances.

Step 1: Input the data.

First, we’ll create three arrays to hold the data values:

group1 = [7, 14, 14, 13, 12, 9, 6, 14, 12, 8]
group2 = [15, 17, 13, 15, 15, 13, 9, 12, 10, 8]
group3 = [6, 8, 8, 9, 5, 14, 13, 8, 10, 9]

Step 2: Perform Levene’s Test.

Next, we’ll perform Levene’s Test using the levene() function from the SciPy library, which uses the following syntax:

levene(sample1, sample2, …, center=’median’)

where:

  • sample1, sample2, etc: Names of the samples.
  • center: Method to use for Levene’s test. The default is ‘median’, but other choices include ‘mean’ and ‘trimmed.’

As mentioned in the documentation, there are actually three different variations of Levene’s test you can use. The recommended usages are as follows:

  • ‘median’: recommended for skewed distributions.
  • ‘mean’: recommended for symmetric, moderate-tailed distributions.
  • ‘trimmed’: recommended for heavy-tailed distributions.

The following code illustrates how to perform Levene’s test using both the mean and the median as the center:

import scipy.stats as stats

#Levene's test centered at the median
stats.levene(group1, group2, group3, center='median')

(statistic=0.1798, pvalue=0.8364)

#Levene's test centered at the mean
stats.levene(group1, group2, group3, center='mean')

(statistic=0.5357, pvalue=0.5914)

In both methods, the p-value is not less than .05. This means in both cases we would fail to reject the null hypothesis. This means we do not have sufficient evidence to say that the variance in plant growth between the three fertilizers is significantly different.

In other words, the three groups have equal variances. If we were to perform some statistical test (like a one-way ANOVA) that assumes each group has equal variance, then this assumption would be met.

Leave a Reply

Your email address will not be published.