
Hypothesis testing is a central skill in statistics as it enables you to make inferences about populations based on sample datasets. Among these tests, a chi-square test is one of the most commonly used methods for analyzing categorical data, such as determining if two categorical variables are related. This article will explore the basics of chi-squared testing using SciPy in Python, along with how to interpret your test results.
What is a Chi Square Test?
The chi-square test is used to analyze categorical data. It measures the discrepancy between observed and expected frequencies in a distribution. There are two broad categories of chi-square tests. The goodness of fit test is used to determine if an observed frequency distribution matches a preset expected distribution while the test of independence determines if two categorical variables are related to each other.
Chi-square tests are commonly used to assess fairness, such as determining if a dice being rolled matches the expected distribution, understanding associations between demographic variables and categorical responses, and determining if certain factors apply uniformly to a grouping variable.
Set Up
To conduct a chi-square test in Python, you will need a few libraries installed. Numpy and Pandas are helpful in data management and manipulation, while the actual statistical test is contained within the SciPy package. These packages should be installed and loaded into your Python environment. If you have never installed these packages before, a common method is using pip.
pip install scipy numpy pandas
import numpy as np import pandas as pd from scipy.stats import chisquare
To run the chi-square test, your data should be in the form of observed frequencies. For a goodness of fit test, an array of observed and expected frequencies is needed while for a test of independence, a contingency table should be created with the rows and columns representing the different categories of your two variables.
Running the Chi Square Test
Consider a goodness of fit chi-square test where you roll a six-sided dice 60 times. If the dice is fair, you would expect each number on the dice to show up one-sixth of the time, or 10 times each. To test an observed distribution using SciPy, you will need to input both the observed and expected frequencies. Then, you can run the chisquare function and obtain your chi-squared statistic along with the p-value.
observed = [9, 11, 16, 10, 9, 5]
expected = [10, 10, 10, 10, 10, 10]
chi2_stat, p_value = chisquare(f_obs=observed, f_exp=expected)
print("Chi-Square Statistic:", chi2_stat)
print("P-value:", p_value)
The output from this test gives a chi-square statistic of 6.4 and a p-value of 0.27.
To run a chi-square test of independence between two variables, you will need to input your observed contingency table and run the chi2_contingency function instead. An expected contingency table is not required for this function as it will automatically calculate the expected values needed for the test. For example, to run a test to see whether males and females have the same preference for three products, you can run the following:
data = [
[45, 35, 10], # Male: Prefer Product A, B, C
[30, 50, 15] # Female: Prefer Product A, B, C
]
chi2_stat, p_value, dof, expected = chi2_contingency(data)
print("Chi-Square Statistic:", chi2_stat)
print("P-value:", p_value)
The output from this test gives a chi-square statistic of 6.5 and a p-value of 0.04.
Interpreting the Results
The two main results of a chi-square test are the chi-square statistic and the p-value. The chi-square statistic quantifies how far the observed data deviates from the expected distribution. When compared against the standard chi-square distribution, this measure translates to a p-value which gives the statistical significance of the deviation from the expected distribution. A p-value less than the significance level (e.g., 0.05) means you reject the null hypothesis.
When running a chi-square test of independence between two variables, it is also important to print out the expected frequencies table. One of the requirements of a chi-square test is that none of the expected value cells should have a value less than 5. This ensures that the chi-square approximation to the theoretical distribution is accurate. If this assumption is violated, the test results may not be reliable. In these cases, consider combining categories or using a non-parametric alternative like the Fisher’s Exact test.
Summary
The chi-square test is a versatile statistical method used to compare an observed distribution with an expected one for categorical variables. By carefully setting up your data, running the appropriate test, and interpreting the results with consideration for statistical assumptions, you can confidently apply chi-square analysis to a variety of real-world problems.
In setting up the data:
observed = [9, 11, 16, 10, 9, 5]
expected = [10, 10, 10, 10, 10, 10]
The list observed would look at the values as 0,1,2,3,4,5
0 is not an outcome on the roll of one die?
Should the observed list read:
observed = [ 0, 9, 11, 16, 10, 9, 5 ] ?
Hi Seymour…The original list is correct. Python starts indexing lists at 0, but that does not mean 0 is an outcome of the die.
In this case, `observed[0] = 9` simply means that a 1 was rolled 9 times, `observed[1] = 11` means that a 2 was rolled 11 times, and so on.
If you added a 0 at the beginning, you would actually be creating a seventh category, which would no longer match the six possible outcomes of a standard die.