Levene's Test for Homogeneity of Variance
tags: #statistics/inferential/assumption_check
We can test for the homogeneity of variance of residuals between two (or more) independent groups using Levene's test for equality of variance (
Levene's test is used to check if the population variance equals between two groups. This has implications for which formula to use when calculating pooled standard deviation and DF for two-sample t-tests.
To conduct a Levene's test in python, we can use the stats module in SciPy. This test does NOT make any assumptions about the distribution of the data:
from scipy.stats import levene
There are three variations of Levene depending on the distribution of the sample, which can be set using the center parameter. The general syntax with iterable unpacking for running Levene is as follows:
# get list of unique classes in a categorical variable
groups = df["categorical_var"].unique()
# create list of df subsets
subgroups = [df[df['categorical_var'] == group]['dv'] for group in groups]
statistic, p = levene(*subgroups, center="mean")
# center is optional, but set to median by desult
print(statistic, p)
NOTE: Multiple arrays of input are inputted into the function as we are comparing across multiple groups in determining whether they have equal variance.