13.2 Test of Independence
Sometimes, categorical data can be organized by a two-way classification or contingency table. Each cell (intersection of row and column) is the observed count.
The Chi-square test statistic is given by:
where
and are the number of rows and columns respectively
where row i, column j totals.
Note: In order to perfrom the Chi-Square test of independence, we assume:
- samples are randomly selected
- sample data consist of frequency counts for each two-way table
- For each cell, the expected frequency is at least 5
Use the function chi2_test_of_indp below with argument: each column from the observed frequency combined as obs = [c1, c2,...]. Where c1 is the list from column 1, c2 is the list from column 2...
def chi2_test_of_indp(obs):
from scipy import stats
chi2, p, dof, ex = stats.chi2_contingency(obs)
print("chi-square test statitic:",chi2)
print("p-value:", p)
Example: The contingency table below shows a random sample of white, black and Hispanic college students based on whether their family borrowed money to pay their college education. Test the hypothesis that borrowing money for college and race are independent at 1% level of significance. (adapted from Larson and Farber)
| race/borrow | Yes | No |
|---|---|---|
| White | 49 | 64 |
| Black | 85 | 123 |
| Hispanic | 85 | 180 |
Borrowing money for college and race are independent
Borrowing money for college and race are dependent
c1 = [49,85,85]
c2 = [64,123,180]
obs = [c1,c2]
chi2_test_of_indp(obs)
Show expected output
chi-square test statitic: 5.993527985414218
p-value: 0.04994844064430499Swapping the column and row still gives the same result (see below).
c1 = [49,64]
c2 = [85,123]
c3 = [85,180]
obs = [c1,c2,c3]
chi2_test_of_indp(obs)
Show expected output
chi-square test statitic: 5.993527985414217
p-value: 0.04994844064430501At , the p-value is less than.05. Reject the null hypothesis that race and borrowing college money are independent.
Example: The contingency table below shows the effects of Covid19 by race in the USA. Test the hypothesis that effects of Covid19 is independent of race at 5% level of significance.
| Covid19 | White | Native | Asian | Black | Hispanic |
|---|---|---|---|---|---|
| Cases | 100 | 170 | 70 | 110 | 190 |
| Hospitalization | 100 | 340 | 100 | 280 | 280 |
| Death | 100 | 240 | 100 | 200 | 230 |
Use the chi2_test_of_indp function below.
def chi2_test_of_indp(obs):
from scipy import stats
chi2, p, dof, ex = stats.chi2_contingency(obs)
print("chi-square test statistic:",chi2)
print("p-value:", p)
r1 = [100,170,70,110,190]
r2 = [100,340,100,280,280]
r3 = [100,240,100,200,230]
obs = [r1,r2,r3]
chi2_test_of_indp(obs)
Show expected output
chi-square test statistic: 36.38048143358525
p-value: 1.4955960363094355e-05Interpretation: Since the p-value 1.4955960363094355e-05 is less than 5%, reject the null hypothesis. That is, there is enough evidence to reject the claim that the effects of Covid10 and race are independent.
Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.