21 Chi-Square Tests
By the end of this chapter you will be able to:
- Identify when each chi-square test applies (goodness-of-fit vs. independence), and check the expected-count assumption.
- Run both tests in R and read the output.
- Explain Yates’ correction — and why R’s corrected χ² can differ from the uncorrected value
assocstatsuses for Cramér’s V. - Compute and interpret Cramér’s V, reported regardless of significance.
- Compute Cohen’s w for a goodness-of-fit test, and explain why χ² grows with sample size while w does not.
In a survey of 40 students, morning people mostly chose coffee and evening people mostly chose tea. There are no means to compare here — just counts in categories — so the tests of the last several chapters have nothing to grab onto. What you need is a test that compares the counts you observed to the counts independence would predict. That is the chi-square.
The chi-square (\(\chi^2\)) test is the standard tool for analyzing categorical data — observations that fall into distinct categories rather than landing on a continuous scale. It comes in two main flavors, and you need to know which one your research question calls for.
Before the formal machinery below, work through the chi-square preview activity — a real two-category question, answered start to finish with real data, before χ², df, or Cramér’s V are ever named.
21.1 The two chi-square tests
- Chi-square goodness-of-fit — tests whether the distribution of one categorical variable matches an expected distribution.
- Chi-square test of independence — tests whether two categorical variables are associated.
Both tests:
- Work with counts (frequencies), not percentages or means.
- Compare observed counts to expected counts under a null hypothesis.
- Use the same chi-square statistic and chi-square distribution.
What differs is the structure of the data and the null hypothesis.
21.2 Chi-square goodness-of-fit
Purpose: Test whether the observed counts of one categorical variable match a specific expected distribution.
Use when: You have one categorical variable and you want to ask whether the observed proportions match a hypothesized set.
Examples:
- Are the colors of M&Ms in a bag distributed as the manufacturer claims?
- Are equal numbers of students enrolled across the four class years?
- Do the observed frequencies of plant species in a field match an ecological model’s predictions?
Hypotheses:
- \(H_0\): Observed frequencies = expected frequencies.
- \(H_1\): Observed frequencies ≠ expected frequencies.
21.2.1 R syntax
# Observed counts
observed <- c(12, 18, 10, 20)
# Expected proportions (must sum to 1)
expected <- c(0.25, 0.25, 0.25, 0.25)
# Test
chisq.test(x = observed, p = expected)If your null is “equal proportions across categories” (the most common case), R does that by default:
chisq.test(observed)21.2.2 Worked example
A student opens a bag of candy and counts:
- Red: 12
- Green: 18
- Blue: 10
- Yellow: 20
The company claims the colors are evenly distributed. Test it.
observed <- c(12, 18, 10, 20)
expected <- rep(0.25, 4)
chisq.test(x = observed, p = expected)Output:
Chi-squared test for given probabilities
data: observed
X-squared = 4.5333, df = 3, p-value = 0.2093
- \(\chi^2 = 4.53\) — the test statistic.
df = 3— number of categories minus 1.p = 0.209— not significant. The data are consistent with equal proportions.

21.3 Chi-square test of independence
Purpose: Test whether two categorical variables are associated.
Use when: You have two categorical variables and want to know whether the level of one is related to the level of the other.
Examples:
- Is there a relationship between class year and preferred social media platform?
- Is voting preference independent of region of residence?
- Is education level associated with employment status?
Hypotheses:
- \(H_0\): The two variables are independent.
- \(H_1\): The two variables are associated.
21.3.1 R syntax
The test takes a contingency table — a matrix of counts where rows are levels of one variable and columns are levels of the other.
# Create the table from a matrix
data_table <- matrix(c(10, 5,
8, 17),
nrow = 2, byrow = TRUE)
colnames(data_table) <- c("Coffee", "Tea")
rownames(data_table) <- c("Morning", "Evening")
# Or build from your raw data
data_table <- table(data$timepref, data$drink)
# Run the test
chisq.test(data_table)21.3.2 Worked example
A survey of 40 students asks (a) whether they prefer coffee or tea, and (b) whether they’re morning or evening people:
| Coffee | Tea | |
|---|---|---|
| Morning | 10 | 5 |
| Evening | 8 | 17 |
Is drink preference associated with time-of-day preference?
data_table <- matrix(c(10, 5, 8, 17), nrow = 2, byrow = TRUE)
colnames(data_table) <- c("Coffee", "Tea")
rownames(data_table) <- c("Morning", "Evening")
chisq.test(data_table)Output:
Pearson's Chi-squared test with Yates' continuity correction
data: data_table
X-squared = 3.2593, df = 1, p-value = 0.07102
- \(\chi^2 = 3.26\) — test statistic (Yates-corrected).
df = 1— (rows − 1)(cols − 1).p = 0.071— not significant at α = .05. We fail to reject the null hypothesis of independence.
For 2 × 2 tables, R applies Yates’ continuity correction by default. It subtracts 0.5 from each |O − E| term before squaring, producing a slightly smaller (more conservative) χ². For this example, the uncorrected χ²(1) = 4.55, p = .033 would be significant, but the Yates-corrected χ²(1) = 3.26, p = .071 is not. The correction matters most in small samples. For tables larger than 2 × 2, R doesn’t apply it. To turn off the correction explicitly, use chisq.test(data_table, correct = FALSE).
Read this output carefully. The R code and the contingency table that produced it are not shown — you will need to infer the pattern from the cell counts below.
Cell counts:
| Passed | Failed | |
|---|---|---|
| Tutoring | 31 | 9 |
| No tutoring | 18 | 22 |
Pearson's Chi-squared test with Yates' continuity correction
data: grade_table
X-squared = 7.5839, df = 1, p-value = 0.005889
- What is the name of the test? Why was Yates’ continuity correction applied?
- Is the result statistically significant at α = .05?
- Describe the pattern in the cell counts. What association does the data suggest?
- A classmate says “tutoring significantly causes higher pass rates.” What is wrong with this?
- Calculate Cramér’s V from the output values. (Hint: \(V = \sqrt{\chi^2 / [N \cdot \min(r-1, c-1)]}\); N = sum of all cells.)
- Interpret that V value.
- Write one APA 7 sentence reporting this result, including Cramér’s V.
21.4 Inspecting where the association comes from
A significant chi-square tells you that some cells have more or fewer observations than expected. To find which cells drive the result, look at the standardized residuals ($stdres).
chisq.test() actually stores two kinds of residual:
$residuals— Pearson residuals, \((O_{ij} - E_{ij}) / \sqrt{E_{ij}}\). Simple, but their size depends on the table’s margins.$stdres— standardized (adjusted) residuals: the same deviation divided by its full standard error, which also accounts for the row and column totals. These behave like z-scores, so the rough rule applies to them: |residual| > 2 marks a cell that deviates meaningfully from independence. These are the residuals the course worksheets use.
Large positive standardized residuals indicate cells with more observations than expected; large negative ones indicate cells with fewer.
result <- chisq.test(data_table)
result$observed # what you counted
result$expected # what you'd expect under H0
result$stdres # standardized residuals (use these for the |2| rule)Chi-square is sensitive to sample size: a tiny, practically meaningless association can produce a significant result when N is large enough. A significant χ² tells you the association is probably not zero — not how large it is. Always report Cramér’s V to quantify the strength of the association independently of sample size.
21.5 Effect size: Cramér’s V
A p-value tells you whether the association exists; it doesn’t tell you how strong it is. Cramér’s V is the standard effect size for chi-square tests of independence.
\[ V = \sqrt{\frac{\chi^2}{n \cdot \min(r - 1, c - 1)}} \]
where \(n\) is the total sample size, \(r\) is the number of rows, and \(c\) is the number of columns.
library(vcd)
assocstats(data_table)Output gives you the chi-square, Cramér’s V, and other association measures.
For a 2 × 2 table, assocstats() computes V from the uncorrected Pearson chi-square, not the Yates-corrected value chisq.test() reports by default. The two will differ slightly — both are valid, but name which one you’re citing rather than reporting the chisq.test() statistic next to a V built from a different number.
Interpretation:
| Cramér’s V | Strength |
|---|---|
| 0.1 | Small |
| 0.3 | Moderate |
| 0.5 | Large |
(These conventions apply for small tables. For larger tables with many df, the cutoffs are smaller — see Cohen’s tables.)
For a 2 × 2 table, Cramér’s V equals the phi coefficient.
21.6 Effect size for goodness of fit: Cohen’s w
Cramér’s V is defined for contingency tables. A goodness-of-fit test compares one variable against a set of expected proportions, so there are no rows and columns to plug in. Its effect size is Cohen’s w:
\[ w = \sqrt{\frac{\chi^2}{N}} \]
where \(N\) is the total count. There is no assocstats() shortcut — compute it directly from the test object:
result <- chisq.test(x = observed, p = expected)
w <- sqrt(result$statistic / sum(observed))
wInterpretation:
| Cohen’s w | Strength |
|---|---|
| 0.1 | Small |
| 0.3 | Moderate |
| 0.5 | Large |
Read these the way you read every other benchmark table in this book — as rough conventions for a shared vocabulary, not thresholds that decide whether a finding matters (see Chapter 27).
χ² grows with sample size even when nothing about the effect has changed. Take four categories with observed counts 12, 18, 10, 20 tested against equal proportions, then multiply every count by 100 so the proportions are identical:
| N = 60 | N = 6,000 | |
|---|---|---|
| χ² | 4.53 | 453.33 |
| p | .209 | < .001 |
| w | 0.27 | 0.27 |
The pattern in the data never changed. χ² grew a hundredfold, p collapsed from non-significant to vanishingly small, and w did not move at all. χ² and p tell you how much data you collected as much as they tell you about the effect; w tells you about the effect alone.
This is why both get reported. The test result answers “could this be chance?” The effect size answers “is it big enough to care about?” Neither question answers the other.
21.7 Assumptions
- Independence of observations. Each observation contributes to exactly one cell.
- Sufficient expected frequencies. Generally, expected counts should be ≥ 5 in each cell. With many cells below 5, the chi-square approximation breaks down — use Fisher’s exact test instead (
fisher.test()). - Counts, not percentages. Always feed the test raw counts, never percentages.
Before running the test, try the Rossman/Chance Analyzing Two-Way Tables applet.
Predict first: looking at the cell counts in your table, which cells do you expect to show the largest deviation from what independence would predict? If there were truly no association, how often would a χ² statistic this large arise by chance? Run the simulation and check.
21.8 Reporting
21.8.1 Goodness-of-fit
A chi-square goodness-of-fit test indicated that the observed distribution of candy colors did not differ significantly from the manufacturer’s claim of equal proportions, \(\chi^2\)(3, N = 60) = 4.53, p = .209, Cohen’s w = .27.
21.8.2 Test of independence
A chi-square test of independence (with Yates’ continuity correction) indicated no significant association between time-of-day preference and drink choice, χ²(1, N = 40) = 3.26, p = .071, Cramér’s V = .34 (moderate effect; computed by
assocstats()from the uncorrected χ²). Although morning people tended to prefer coffee and evening people tended to prefer tea, the association did not reach statistical significance.
Report Cramér’s V regardless of whether the result is significant. A non-significant test with V = .34 is meaningfully different from one with V = .03 — the first is consistent with a real association the study was too small to detect, while the second suggests a negligible effect.
Conventions:
- Report \(\chi^2(\text{df}, N = \text{sample size}) = \text{value}\).
- Italicize N, p, V; χ² stays upright (Greek letters are not italicized).
- Round \(\chi^2\) to two decimals, p to three.
- Cramér’s V for tests of independence — report regardless of significance.
21.9 When the assumptions don’t hold
| Problem | Fix |
|---|---|
| Expected cells < 5 in many cells | Use fisher.test() (Fisher’s exact test) |
| Paired/matched categorical data | Use McNemar’s test (mcnemar.test()), not chi-square |
| Ordinal categories with order that matters | Use the linear-by-linear test or polychoric correlation |
| Small total N | Fisher’s exact is also good here |
21.10 Common mistakes
| Wrong | Right |
|---|---|
Feeding percentages to chisq.test() |
Pass raw counts |
| Running chi-square on continuous data | Categorize first, or use a different test |
| Not saying whether a 2×2 χ² is Yates-corrected | Name it (“with Yates’ continuity correction”), especially next to Cramér’s V |
| Reporting “association” as “causation” | Chi-square detects association only |
| Skipping Cramér’s V | Always report effect size (regardless of significance) |
21.11 Quick reference
| Element | R code |
|---|---|
| Goodness-of-fit | chisq.test(x = observed, p = expected) |
| Test of independence | chisq.test(table) |
| Build contingency table from raw data | table(data$varA, data$varB) |
| Standardized residuals | chisq.test(table)$stdres |
| Cramér’s V | library(vcd); assocstats(table) |
| Fisher’s exact (small expected) | fisher.test(table) |
| McNemar’s (paired categorical) | mcnemar.test(table) |
21.12 The same test, different disciplines
Chi-square applies whenever both variables are categorical and you want to know whether they are associated.
| Discipline | Typical association | Categorical variables |
|---|---|---|
| Psychology | Diagnostic or condition category × treatment outcome | Diagnosis (anxiety/depression/PTSD) × response (remission/no remission) |
| Biology | Species or genotype × categorical phenotype or habitat | Species × microhabitat type; genotype × disease presence/absence |
| Sociology | Demographic group × social or political outcome | Education level × voting behavior; gender × employment type |
This concludes Part IV. Part V turns to regression — the framework that, in its multiple-predictor form, ties together everything in Parts IV and V into one general model.
Core idea: Chi-square tests are for categorical data. The goodness-of-fit test checks whether observed frequencies match an expected distribution; the test of independence checks whether two categorical variables are related.
You should understand:
- When to use goodness-of-fit vs. independence (one variable vs. two variables)
- Why expected cell counts matter (the rule of 5) and what to do when they are too small
- What Cramér’s V measures and how to interpret it
- Why raw counts — not percentages — go into
chisq.test()
You should be able to do in R:
- Build a contingency table:
table(data$var1, data$var2) - Run chi-square:
chisq.test(table) - Fisher’s exact (small cells):
fisher.test(table) - Effect size:
library(vcd); assocstats(table)for Cramér’s V
Plain-language check: χ²(2, N = 120) = 8.4, p = .015. You have a 3-category variable. What does this result tell you, and what does it not tell you?
The one mistake to avoid: Feeding percentages rather than raw counts to chisq.test(). The test requires actual observed frequencies; percentages will produce incorrect results.
Practice: 60 students are asked whether they prefer morning, afternoon, or evening study sessions. Results: 15, 25, 20. Test whether preferences are equally distributed. Write the two lines of R code and interpret the result.
Key terms from this chapter are defined in the Glossary.
Two Types of Chi-Square Tests (conceptual guide), Chi-Square Practice in R (built-in datasets), Chi-Square Residuals & Cramér’s V (post-hoc exploration).
Four interactive modules cover this chapter, and they are meant to be done in order. Each is worked in R alongside the page, in pairs, and printed to PDF to hand in.
- Why Chi-Square Matters — which variables qualify in the first place, and why a big χ² is not a big effect.
- Goodness-of-Fit Test — one variable against a set of expected proportions, across four worked datasets.
- Test of Independence — contingency tables, standardized residuals, and which χ² your Cramér’s V was built from.
- Assumptions & Troubleshooting — the expected-count rule, Fisher’s exact test, and what Yates’ correction costs you.
Modules open in a new tab.