Today: We use chi-square distributions to test claims aboutcategorical variables — whether a distribution fits a model, and whether two variables are associated.
Big picture map: today’s topic is part of our broader inference toolkit.
Same inference workflow: state \(H_0/H_a\), check conditions, compute statistic, get p‑value, conclude in context. Chi‑square is different only in that it works with counts and a right‑tailed \(\chi^2\) model.
Interested? Email Prof. Vinay with your full name, student ID, and section (in‑person/online).
The chi-square distribution with k degrees of freedom($\chi^2_k$) is a family of right-skewed curves used fortests involving counts in categories.
Sketch of $\chi^2_k$ for a few values of $k$. Larger $k$ shifts the peak to the right.
For a chi-square distribution with $k$ degrees of freedom:
Most technology (software, calculators, tables) will give you right-tail areas $P(\chi^2_k \ge x)$ directly.
Chi-square procedures compare observed counts $O$ in categories toexpected counts $E$ predicted by a model or null hypothesis.
Each term $\dfrac{(O-E)^2}{E}$ measures the standardized squared distance between what we saw and what the null hypothesis expects. Large chi-square values signal that the data do not fit the null model well.
A goodness-of-fit test asks whether the distribution of a single categorical variable in a population matches a claimed or known model.
A college claims that the proportions of smartphone operating systems among students are 40% Android, 40% iOS, and 20% other. You take a random sample and record the counts in each category.
This is not yet about association between variables; we are just judging how well one distribution fits a model.
Before using a chi-square goodness-of-fit test, check:
When some expected counts are less than 5, you may need to combine categories or use a different method.
A campus cafeteria believes that 30% of students prefer chips, 40% prefer fruit, and 30% prefer cookies.In a random sample of 150 students, we observe the following preferences.
| Snack | Observed count $O$ | Expected count $E$ |
|---|---|---|
| Chips | 38 | $150 \times 0.30 = 45$ |
| Fruit | 70 | $150 \times 0.40 = 60$ |
| Cookies | 42 | $150 \times 0.30 = 45$ |
For the snack example:
Add these contributions to obtain the test statistic $\chi^2$. Then find the p-value from a chi-square distribution with $k - 1$ degrees of freedom, where $k$ is the number of categories.
In practice, you will usually have technology compute $\chi^2$ and the p-value once you supply the table of observed and expected counts.
A contingency table (two-way table) displays counts for two categorical variables at once,showing how the variables are related in a sample.
A two-way table would cross these variables to show, for example, how many first-year students prefer group study,how many seniors prefer online study, and so on.
Chi-square tests of independence and homogeneity are both based on contingency tables.
Choose two categorical variables, then tally.
Now convert to a 2×2 table of counts.
| B1 | B2 | Row total ri | |
|---|---|---|---|
| A1 | 7 | 3 | 10 |
| A2 | 4 | 6 | 10 |
| Col total cj | 11 | 9 | 20grand N |
Always include row totals, column totals, and the grand total.
Under independence, the expected count in cell \((i,j)\) is:
For cell \(A_1, B_1\): \(E_{11} = \dfrac{\color{#2e7d32}{10} \times \color{#1e88e5}{11}}{\color{#6a1b9a}{20}} = 5.5\).Similarly, \(E_{12}=4.5\), \(E_{21}=5.5\), \(E_{22}=4.5\).
| B1 | B2 | Row total ri | |
|---|---|---|---|
| A1 | O=7 \(E_{11}=5.5\) | O=3 \(E_{12}=4.5\) | 10 |
| A2 | O=4 \(E_{21}=5.5\) | O=6 \(E_{22}=4.5\) | 10 |
| Col total cj | 11 | 9 | 20grand N |
Check the rule: expected counts should be \(\ge 5\) (or most \(\ge 5\) with none too small).
Two categorical variables in a population are said to be associated if theconditional distributions of one variable differ across levels of the other variable.
If the conditional distributions are the same across all levels of the other variable,then the variables are not associated (we say they are independent).
We will use chi-square tests to decide, based on sample data, whether we have enough evidence to claim an association in the population.
A chi-square test of independence examines whether two categorical variables are independent in a single population.
We start with a contingency table of observed counts $O_{ij}$ for row $i$ and column $j$.
Under $H_0$ (independence), the expected count in each cell is:
“Independent” means knowing one variable does not help predict the other.
Before using a chi-square test of independence, check:
If these conditions are not met, the chi-square approximation may not be accurate and a different method may be needed.
Adjust the four cell counts in this 2×2 table. We will automatically compute the expected counts and the chi-square statistic.
| Category B₁ | Category B₂ | Row total | |
|---|---|---|---|
| Category A₁ | 50 | ||
| Category A₂ | 50 | ||
| Col total | 55 | 45 | 100 |
Think of A as one variable (e.g., Gender) and B as another (e.g., Preference).
$\chi^2 =$ 0.000 withdf = 1.
Expected counts under independence:
Adjust the counts to see how the chi-square statistic reacts to stronger or weaker associations.
Large $\chi^2$ (far from 0) suggests the data are inconsistent with independence —the variables may be associated. A very small $\chi^2$ means the observed table is close to what we’d expect if the variables were independent.
A chi-square test of homogeneity compares the distribution of a categorical variable across two or more populations or treatments.
Example: Are the grade-level distributions of preferred study method the same across three different schools?
We take one random sample from each population (or treatment group),record counts for the categorical outcome, and place them in a contingency table.
The calculations for the chi-square statistic and expected counts are the same.The main difference lies in the research question and study design.
All chi-square procedures use
$$\chi^2 = \sum \frac{(O - E)^2}{E}$$and compare the statistic to a chi-square distribution with$\text{df} = (\text{rows} - 1)(\text{columns} - 1)$ (or $k - 1$ for goodness-of-fit).
k = 5 categories ⇒ df = 4. Use counts (not percentages). If some expected counts are small, use the simulated p‑value.
# Goodness-of-fit (k = 5 ⇒ df = 4)
counts <- c(12, 3, 6, 20, 9) # observed
probs <- c(0.2, 0.1, 0.1, 0.3, 0.3) # stated distribution (sums to 1)
chisq.test(counts, p = probs, correct = FALSE)
# If any expected count < 5, prefer a simulated p-value:
chisq.test(counts, p = probs, simulate.p.value = TRUE, B = 10000)
Tip: If rounding makes sum(probs) ≠ 1, call chisq.test(counts, p = probs, rescale.p = TRUE).
Same code for both tests; supply an r×c table of counts. df = (rows − 1)(cols − 1).
# Independence / Homogeneity (2×2 example)
tab <- matrix(c(30, 20,
25, 25),
nrow = 2, byrow = TRUE)
chisq.test(tab, correct = FALSE) # Yates off for clarity in class
# Larger tables work the same way; enter counts row by row.
# Always interpret in context and check expected counts ≥ 5.
Heads‑up: If any expected count < 5, be cautious about the p‑value. Combine sensible categories or collect more data; for 2×2, an exact method exists (concept only).
Good practice: Sketch the table, label variables clearly, and write a complete sentence answer that mentions the direction of any association (who has higher proportions where?).