Chi square test of independence - mauriceling/mauriceling.github.io GitHub Wiki

Purpose: To test whether 2 categorical variables (defining the rows and columns) are independent

Null hypothesis: There is no relationship between between the 2 categorical variables; that is, the 2 categorical variables are not independent.

Alternate hypothesis: There is a relationship between the 2 categorical variables; that is, the 2 categorical variables are not independent.

Note: This test is commonly used for large sample size (more than 10 counts per category). Use Fisher exact test for small sample size.

Code:

>>> from scipy import stats
>>> table = [[77, 404],
...          [16, 122]]
>>> result = stats.chi2_contingency(table)
>>> print("statistic = %.3f" % result[0])
statistic = 1.309
>>> print("p-value = %.3f" % result[1])
p-value = 0.253