Login
📚 Python for Introductory Statistics
Chapters ▾

13.2 Test of Independence

Watch demo video

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.

H o : The row and column variables are independent

H a : The row and column variables are dependent

The Chi-square test statistic is given by:

χ 2 = i , j = 1 r c ( O i j E i j ) 2 E i j

where

r and c are the number of rows and columns respectively

Eij=ricjn where ri,cj row i, column j totals.

df = ( r 1 ) ( c 1 )

Note: In order to perfrom the Chi-Square test of independence, we assume:

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/borrowYesNo
White4964
Black85123
Hispanic85180

H0: Borrowing money for college and race are independent

Ha: 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.04994844064430499

Swapping 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.04994844064430501

At α=0.05, 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.

Covid19WhiteNativeAsianBlackHispanic
Cases10017070110190
Hospitalization100340100280280
Death100240100200230

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-05

Interpretation: 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.