STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 11
Categorical Data Analysis
Many problems in applied statistics involve categorical outcomes or explanatory variables. This chapter introduces methods for summarising and analysing categorical data using contingency tables, goodness–of–fit tests and measures of association. We also discuss the use of the chi–square distribution and alternative exact methods when sample sizes are small.
Contingency Tables
A contingency table cross–classifies observations into categories. For example, Table 1.1 shows the relationship between smoking status and disease outcome.
| Disease | No disease | |
|---|---|---|
| Smoker | ||
| Non–smoker |
The null hypothesis of independence () posits that the probability of disease is the same for smokers and non–smokers. We can test this using the chi–square statistic where are the observed counts and are the expected counts under independence, . Under and large samples, follows a chi–square distribution with degrees of freedom, where and are the number of rows and columns.
Example: Smoking and Disease
Suppose in a study of people, 40 of 80 smokers have the disease and 20 of 120 non–smokers have the disease. We wish to test whether smoking is associated with disease.
Worked example
Chi–square test in Python and R.
In Python, we can use scipy.stats.chi2_contingency:
import numpy as np
from scipy.stats import chi2_contingency
table = np.array([[40, 40], [20, 100]])
chi2, p_val, dof, exp = chi2_contingency(table)
print(chi2, p_val)
In R, use chisq.test:
tbl <- matrix(c(40, 40, 20, 100), nrow = 2, byrow = TRUE)
chisq.test(tbl, correct = FALSE)
Both functions return the test statistic and p–value. With and , we reject the null hypothesis of independence.
Goodness–of–Fit Tests
When you have one categorical variable and wish to compare the observed frequencies to theoretical proportions, use a goodness–of–fit test. Suppose we observe counts in categories with expected proportions under . The test statistic is which follows a distribution with degrees of freedom. When some expected counts are small, you may need to combine categories or use an exact test.
Measures of Association
Beyond testing independence, we may quantify the strength of association between two categorical variables. Two common measures are:
Odds Ratio (OR): defined as for a table, representing the ratio of odds of disease in smokers vs non–smokers. See Chapter [chap:risk] for interpretation.
Relative Risk (RR): defined as , more intuitive but only estimable in cohort studies.
Exact Tests
When expected counts are very small (e.g., less than 5), the
asymptotic chi–square approximation may be inaccurate. Fisher’s exact
test computes the exact probability of observing tables as extreme as
the observed table under
.
In Python, use scipy.stats.fisher_exact; in R, use
fisher.test. Exact methods are conservative but valid
regardless of sample size.
Exercises
Practice
A survey asks 200 men and 250 women whether they support a policy; 150 men and 120 women say yes. Perform a chi–square test of independence. Compute the chi–square statistic by hand and verify your result using R/Python/Julia.
A genetic experiment produces counts of four eye colours in fruit flies: 60 red, 25 brown, 10 scarlet and 5 white. Test the hypothesis that eye colour occurs in the ratio 9:3:3:1. Use a goodness–of–fit test.
Use a contingency table to show that the odds ratio and relative risk can differ numerically. Provide code to compute both measures.
Simulate a table where one cell has an expected count of 1. Compare the p–value from the chi–square test to Fisher’s exact test. Discuss why the exact test is preferred here.
References
Agresti A. An Introduction to Categorical Data Analysis. 3rd ed. Wiley; 2019.
Alan Agresti and Barbara Finlay. Statistical Methods for the Social Sciences. 4th ed. Pearson; 2009.
Mehta CR, Patel NR. Exact tests in categorical data analysis. Biometrika. 1983;70(2):377–384.