Unpacking Lists for Statistical Tests

tags: #python

The * symbol can be used in the input of statistical functions in Python is used to unpack an iterable (such as a list or a tuple) into individual arguments of the function.

Example:

This is useful when conducting statistical test (e.g., Levene's test for homogeneity of variance) for checking the normality of the continuous dependent variable across each class of a categorical variable in ANOVAs:

from scipy.stats import levene

# get list of unique classes in a categorical variable
groups = df["categorical_var"].unique()

# create list of df subsets
subgroups = [data[data['categorical_var'] == group]['dv'] for group in groups]
statistic, p = levene(*subgroups, center="median")

# Perform Levene's test
statistic, p_value = levene(*subgroups)

print(f"Levene's test statistic: {statistic:.4f}")
print(f"p-value: {p_value:.4f}")
Powered by Forestry.md