01 / 22
\(\chi^2\)

Chi-Square Test
of Independence

Determine whether two categorical variables are associated or independent using observed and expected frequencies in a contingency table.

Biostatistics

What Are Categorical Variables?

Categorical variables place individuals into groups or categories rather than measuring them on a numeric scale. Examples include blood type, treatment group, or disease status.

Nominal
Categories with no natural order: blood type (A, B, AB, O), eye color
Ordinal
Categories with a meaningful order: disease severity (mild, moderate, severe)

The Big Question

Imagine you collect data on smoking status (smoker vs. non-smoker) and lung disease (present vs. absent). You notice that smokers seem more likely to have lung disease. But is this difference real or just random chance?

Smoking Status Smoker / Non-smoker Lung Disease Present / Absent Associated?

The Contingency Table

Data for two categorical variables is organized into a contingency table (also called a cross-tabulation). Each cell shows the count of observations falling into that combination of categories.

Disease Present Disease Absent Total
Smoker 120 80 200
Non-smoker 90 210 300
Total 210 290 500

Hypotheses for the Test

\(H_0\): Null Hypothesis
The two variables are independent: there is no association between them in the population.
\(H_a\): Alternative Hypothesis
The two variables are not independent: there is an association between them in the population.

Expected Frequencies

If the null hypothesis is true (variables are independent), we can calculate expected frequencies for each cell:

$$ E_{ij} = \frac{(\text{Row } i \text{ total}) \times (\text{Column } j \text{ total})}{\text{Grand total}} $$

Example: Expected count for Smoker + Disease Present:

The \(\chi^2\) Statistic

$$ \chi^2 = \sum_{\text{all cells}} \frac{(O_{ij} - E_{ij})^2}{E_{ij}} $$
1
\(O_{ij}\): the observed count in row \(i\), column \(j\)
2
\(E_{ij}\): the expected count under independence
3
Sum over all cells: a large total means observed data deviates greatly from what independence predicts

Degrees of Freedom & the Distribution

$$ df = (r - 1)(c - 1) $$

For our \(2 \times 2\) table: \(df = (2-1)(2-1) = 1\). We compare our \(\chi^2\) statistic to the chi-square distribution with that many degrees of freedom.

\(\chi^2\) value Density df = 1 df = 3 df = 5 0 2 4 6 8

Independence vs. Goodness of Fit

Test of Independence Goodness of Fit
Purpose Association between two variables Does one variable match a known distribution?
Data layout \(r \times c\) contingency table One-row frequency table
Expected values \(\frac{\text{row} \times \text{col}}{\text{total}}\) From hypothesized proportions
Degrees of freedom \((r-1)(c-1)\) \(k - 1\)
Example Smoking vs. lung disease Observed vs. expected Mendelian ratios

Conditions for Validity

1
Random sampling: Data must come from a random sample or randomized experiment
2
Independence of observations: Each individual contributes to only one cell
3
Expected frequency rule: All expected counts \(E_{ij} \geq 5\)
4
Categorical data: Both variables must be categorical, not continuous

Step-by-Step Procedure

1
State hypotheses: \(H_0\): variables are independent; \(H_a\): variables are associated
2
Build contingency table with observed counts and row/column totals
3
Compute expected counts: \(E_{ij} = \frac{(\text{row total}_i)(\text{col total}_j)}{n}\). Verify all \(E_{ij} \geq 5\)
4
Calculate \(\chi^2\): \(\sum \frac{(O - E)^2}{E}\) across all cells
5
Find degrees of freedom: \(df = (r-1)(c-1)\)
6
Compare to critical value or find the p-value. If \(p < \alpha\), reject \(H_0\)

Worked Example: Setup

A researcher investigates whether exercise frequency is associated with flu incidence in 400 adults. Use \(\alpha = 0.05\).

Got Flu No Flu Total
Regular Exercise 30 120 150
No Exercise 70 180 250
Total 100 300 400

Worked Example: Calculation

Step 1: Compute expected counts for each cell:

E (Got Flu) E (No Flu)
Regular Exercise \(\frac{150 \times 100}{400} = \) 37.5 \(\frac{150 \times 300}{400} = \) 112.5
No Exercise \(\frac{250 \times 100}{400} = \) 62.5 \(\frac{250 \times 300}{400} = \) 187.5

Step 2: All \(E_{ij} \geq 5\) ✓. Compute \(\chi^2\):

Worked Example: Conclusion

\(\chi^2\) Statistic
\(\chi^2 = 3.20\)
Critical Value
\(\chi^2_{0.05, 1} = 3.841\)
Decision
\(3.20 < 3.841\) → Fail to reject \(H_0\)
3.841 3.20 Our value Reject region 0

Example 2: \(3 \times 2\) Table

A study examines whether blood type (A, B, O) is associated with infection status in 300 patients.

Infected Not Infected Total
Type A 40 60 100
Type B 50 50 100
Type O 30 70 100
Total 120 180 300

Common Mistakes to Avoid

✕ Using Percentages
The test requires raw counts, not percentages or proportions. Using percentages gives incorrect \(\chi^2\) values.
✕ Ignoring Expected Counts
Forgetting to check that all \(E_{ij} \geq 5\). Violations make the \(\chi^2\) approximation unreliable.
✕ Claiming Causation
A significant result shows association, not causation. Only randomized experiments support causal claims.

Summary Reference Table

Component Details
Purpose Test association between two categorical variables
Hypotheses \(H_0\): independent; \(H_a\): associated
Statistic \(\chi^2 = \sum \frac{(O - E)^2}{E}\)
Expected counts \(E_{ij} = \frac{(\text{row total})(\text{col total})}{n}\)
Degrees of freedom \((r-1)(c-1)\)
Key condition All \(E_{ij} \geq 5\)
Decision Reject \(H_0\) if \(\chi^2 > \chi^2_{\alpha, df}\) (or \(p < \alpha\))

Question 1 of 5

What does the null hypothesis \(H_0\) state in a chi-square test of independence?

Question 2 of 5

For a \(4 \times 3\) contingency table, what are the degrees of freedom?

Question 3 of 5

In a \(2 \times 2\) table with row totals 80 and 120, column totals 90 and 110, and grand total 200, what is the expected count for the cell in row 1, column 1?

Question 4 of 5

A researcher computes \(\chi^2 = 9.21\) with \(df = 2\). The critical value at \(\alpha = 0.01\) is 9.210. What is the correct conclusion?

Question 5 of 5

Which condition would violate the assumptions for a chi-square test of independence?