Book contents

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 status by smoking
Disease No disease
Smoker aa bb
Non–smoker cc dd

The null hypothesis of independence (H0H_0) posits that the probability of disease is the same for smokers and non–smokers. We can test this using the chi–square statistic χ2=∑i,j(Oij−Eij)2Eij,\chi^2 = \sum_{i,j} \frac{(O_{ij}-E_{ij})^2}{E_{ij}}, where OijO_{ij} are the observed counts and EijE_{ij} are the expected counts under independence, Eij=(row total)(column total)nE_{ij} = \frac{(\text{row total})(\text{column total})}{n}. Under H0H_0 and large samples, χ2\chi^2 follows a chi–square distribution with (r−1)(c−1)(r-1)(c-1) degrees of freedom, where rr and cc are the number of rows and columns.

Example: Smoking and Disease

Suppose in a study of n=200n=200 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 χ2=16.7\chi^2=16.7 and p<0.001p<0.001, 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 OiO_i in kk categories with expected proportions pip_i under H0H_0. The test statistic is χ2=∑i=1k(Oi−npi)2npi,\chi^2 = \sum_{i=1}^k \frac{(O_i - n p_i)^2}{n p_i}, which follows a χ2\chi^2 distribution with k−1k-1 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:

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 H0H_0. In Python, use scipy.stats.fisher_exact; in R, use fisher.test. Exact methods are conservative but valid regardless of sample size.

Exercises

Practice

  1. 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.

  2. 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.

  3. Use a 2×22\times2 contingency table to show that the odds ratio and relative risk can differ numerically. Provide code to compute both measures.

  4. Simulate a 2×22\times2 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