Lesson 108 — Chi-squared test: goodness of fit and independence
Chi-squared statistics: asymptotic distribution, degrees of freedom, goodness-of-fit test and independence test in contingency tables. Yates correction, Cramér's V.
Used in: 3rd year High School · Stochastik LK German · H2 Statistics Singapore · Math B Japanese — inferential statistics
The chi-squared statistic measures the total discrepancy between observed counts and expected counts under . The larger , the more evidence against the null hypothesis. For goodness of fit, it tests whether data follow a prescribed distribution; for independence, it tests whether two categorical variables are independent in an table.
Rigorous notation, full derivation, hypotheses
Rigorous definition
Chi-squared distribution
"Chi-squared distributions have an additivity property: if and are independent, then ." — OpenStax Statistics, §11.1
Goodness-of-fit test
Independence test in table
"Expected frequencies for an independence test are calculated assuming that the row proportions are equal across all columns. If the null hypothesis is true (variables independent), this assumption is satisfied." — OpenIntro Statistics, §6.4
Assumptions for validity (Cochran's rule)
Yates correction ( table)
Effect size: Cramér's V
Chi-squared curve with df = 5. The yellow region to the right of the critical value is the rejection region for H0 at level alpha = 5%.
Worked examples
Exercise list
42 exercises · 10 with worked solution (25%)
- Ex. 108.1Application
A six-sided die is rolled 60 times. What is the number of degrees of freedom in the goodness-of-fit test for the uniform distribution?
Show solution
. For 6 categories with no estimated parameters, we lose 1 degree of freedom due to the constraint . - Ex. 108.2Application
For the die from the previous exercise rolled 60 times, what is the expected frequency per face?
Show solution
for each face.Show step-by-step (with the why)
- Identify and for each face. Why: under of a fair die, each face has equal probability.
- Calculate .
Trick: in discrete uniform distributions, is simply .
- Ex. 108.3Application
A die is rolled 60 times: observing 12, 8, 11, 9, 13, 7 for faces 1 to 6. Calculate and conclude at 5%.
Show solution
. Critical value at 5% with : 11.07. Do not reject . - Ex. 108.4Application
Calculate the degrees of freedom for the independence test in a contingency table.
Show solution
Table $3 \times 4$: . - Ex. 108.5Application
In a contingency table with , , , calculate .
Show solution
. Under independence, the expected frequency is the product of the marginals divided by the total.Show step-by-step (with the why)
- Identify the margins: row total , column total , grand total .
- Apply the formula: . Why: under independence, , and .
Trick: .
- Ex. 108.6Application
In an independence test, we obtained with . The critical value at 5% is 5.99. What is the conclusion?
Show solution
. We reject : there is significant association between the variables at the 5% level. - Ex. 108.7ApplicationAnswer key
Calculate Cramér's V: , , table (so ).
Show solution
. Small to medium effect. - Ex. 108.8ApplicationAnswer key
In which situations should the Yates correction be applied to the chi-squared test?
Show solution
Yates correction applies to $2 \times 2$ tables when expected frequencies are small (close to 5). It subtracts $0,5$ from each before squaring, making the test more conservative. - Ex. 108.9ApplicationAnswer key
A researcher has in two of five cells in a table. Is the chi-squared test appropriate? Justify.
Show solution
See the referenced source for the detailed solution. - Ex. 108.10Application
Observe in observations with expected proportions . Calculate .
Show solution
, since and . .Show step-by-step (with the why)
- Calculate expected: ; ; .
- Calculate each term: ; ; .
- Sum: . . Critical at 5%: 5.99. Do not reject .
Trivia: the third category contributes nothing to since .
- Ex. 108.11Application
For the previous exercise (3 categories, fully specified distribution), what is the number of degrees of freedom?
Show solution
. - Ex. 108.12Application
A study measures blood pressure (high/normal) in the same group of patients before and after an exercise program. Why is the standard independence chi-squared test inadequate?
Show solution
See the referenced source for the detailed solution. - Ex. 108.13Application
What is the critical value (chi-squared with 1 degree of freedom at the 5% level)?
Show solution
The critical value . This value is fundamental: any in a $2 \times 2$ table (with ) is significant at 5%. - Ex. 108.14Application
Calculate the expected frequencies for the table with cells , , , .
Show solution
Table $2 \times 2$: . , , , , . ; ; ; . - Ex. 108.15Application
With the expected values from the previous exercise, calculate and conclude at 5%.
Show solution
Using the expected values from the previous exercise: . . Critical 5%: 3.84. Reject .Show step-by-step (with the why)
- Use calculated in the previous exercise: 70, 30, 70, 30.
- Calculate each term: ; ; same for row 2.
- Sum: . Reject .
Trick: in $2 \times 2$ table, the four contributions are symmetric in pairs.
- Ex. 108.16ApplicationAnswer key
Why is in every contingency table? Explain geometrically or algebraically.
Show solution
. In a $2 \times 2$ table, there are 4 cells but 3 constraints (row 1 total, column 1 total, and grand total — but the grand total is a consequence of the other two, so only independent constraints, leaving degree of freedom). By formula: . - Ex. 108.17Application
What are the mean and variance of ? For , is the distribution approximately symmetric?
Show solution
The distribution has mean and variance . For $k$ large, by CLT, approximates . The distribution is right-skewed (positively skewed), not symmetric. - Ex. 108.18Application
In a goodness-of-fit test with categories, how do the degrees of freedom change when we estimate parameters of the distribution from the data itself?
Show solution
When estimating parameters from the data, we lose additional degrees of freedom. Therefore . Example: goodness of fit to Poisson with estimated: . - Ex. 108.19Application
Show that the chi-squared statistic is always non-negative.
Show solution
The chi-squared statistic is always non-negative: each term because it is a square divided by a positive number. Therefore . - Ex. 108.20ApplicationAnswer key
Is the goodness-of-fit chi-squared test one-tailed (right tail) or two-tailed? Why?
Show solution
Chi-squared is one-tailed (right tail): we reject only when is large. Discrepancies both positive and negative contribute positive terms to the sum — there is no direction distinction in the test statistic. - Ex. 108.21Application
in with expected uniform distribution. Calculate and conclude at 1%.
Show solution
, each (uniform, ). . . Critical 1%: 11.34. Reject .Show step-by-step (with the why)
- for each category.
- ; ; ; .
- . . Critical value at 1%: 11.34. Reject .
Trivia: the observed distribution is increasing, while posits uniformity — the extremes contribute much more to .
- Ex. 108.22Application
What is the conceptual difference between homogeneity test and independence test? Does the formula for change?
Show solution
Homogeneity test checks whether populations have the same categorical distribution (independent samples drawn from each population). Independence test checks whether two variables are independent in a single random sample. The mathematics is identical (, , ), but the interpretation and design differ. - Ex. 108.23Application
with . What is the conclusion at 5% and at 1%? (Critical values: 11.07 and 15.09 respectively.)
Show solution
For and : critical value at 5% is 11.07. Since , we reject at 5%. At 1%, critical is 15.09; since , we do not reject at 1%. The p-value is between 1% and 5%. - Ex. 108.24Understanding
What would it mean to obtain in a goodness-of-fit test? Is this possible in real data?
Show solution
When , all exactly — the data fit the null hypothesis perfectly. This is rare in real data (sampling variation always produces some discrepancy). An exact indicates data entry error or fabricated data. - Ex. 108.25Understanding
Why do very large samples make a problematic measure? What alternative should be used?
Show solution
See the referenced source for the detailed solution. - Ex. 108.26UnderstandingAnswer key
Describe the shape of the chi-squared curve for small (e.g. ) vs. large (e.g. ). How does this relate to its origin as a sum of squares?
Show solution
The chi-squared distribution is right-skewed and always positive. It is the distribution of sums of squares of independent standard normals. The right skew decreases as grows (for large $df$ it approaches normal). This justifies using larger critical values for more degrees of freedom. - Ex. 108.27UnderstandingAnswer key
Which formula below is Pearson's chi-squared statistic?
Show solution
See the referenced source for the detailed solution. - Ex. 108.28Understanding
Explain why Cochran's rule () is necessary for the validity of the chi-squared test.
Show solution
See the referenced source for the detailed solution. - Ex. 108.29ModelingAnswer key
Dihybrid cross in peas predicts phenotypes in ratio 9:3:3:1. In 160 offspring observe 95, 30, 27, 8. Test goodness of fit at 5%.
Show solution
Expected proportions: Smooth-yellow , Smooth-green , Wrinkled-yellow , Wrinkled-green . In : , , , . . . Critical 5%: 7.81. Do not reject — compatible with Mendel.Show step-by-step (with the why)
See the referenced source for the step-by-step walkthrough. - Ex. 108.30Modeling
Survey of 400 university students (200 men, 200 women) tabulates opinion on quotas (Favorable/Neutral/Against): men 70/60/70, women 110/50/40. Test independence at 5%.
Show solution
Table $2 \times 3$: . Calculate with the provided margins, then . If (critical 5%), there is significant association between gender and opinion. - Ex. 108.31Modeling
A sample of 200 M&M's from a package shows: 30 red, 35 orange, 22 yellow, 40 green, 55 blue, 18 brown. According to the manufacturer, proportions are 13%, 20%, 14%, 16%, 24%, 13%. Test goodness of fit at 5%.
Show solution
See the referenced source for the detailed solution. - Ex. 108.32Modeling
A/B/C test on landing page: 200 visitors per variation. Conversions: A = 24, B = 30, C = 40. Test homogeneity of conversion rates at 5%.
Show solution
Homogeneity table $3 \times 2$: each group (A, B, C) has 200 visitors, conversions (24, 30, 40) and non-conversions (176, 170, 160). . . Calculate and compare with 5.99. - Ex. 108.33Modeling
Four machines produce defects: 30, 40, 25, 35 defects respectively (total 130). Test whether the defect rate is uniform among machines at the 5% level.
Show solution
4 machines, defect counts: 30, 40, 25, 35 (total 130). Under of uniformity: . . . Critical 5%: 7.81. Do not reject — no significant difference between machines. - Ex. 108.34ModelingAnswer key
Clinical trial with 50 patients (25 per group): vaccine resulted in 18 cures, placebo in 12 cures. Build the table and apply the chi-squared test with Yates correction at 5%.
Show solution
Table $2 \times 2$. Apply Yates correction if any is close to 5. Calculate and compare with . If we reject, we conclude that treatment significantly alters the proportion of cures. - Ex. 108.35Modeling
Do DNIT highway accident data follow a Poisson distribution? Describe the complete flowchart for the goodness-of-fit test, including how to handle the unknown parameter.
Show solution
Monthly highway accident data by state is categorized into ranges (0-2, 3-5, 6-10, 11+). Fit mean (Poisson) from data, calculate , check (merge categories if necessary), compute with (estimated ). Compare with table. - Ex. 108.36Understanding
Which condition below is necessary for the validity of the independence chi-squared test?
Show solution
See the referenced source for the detailed solution. - Ex. 108.37Understanding
In a before-after study, the same 80 patients are classified as hypertensive or normal before and after intervention. Why use McNemar instead of standard chi-squared?
Show solution
McNemar tests whether in a $2 \times 2$ table of paired data (where and are the discordances). The statistic is with . Standard chi-squared would ignore the correlation between repeated measures, violating the independence assumption. - Ex. 108.38Understanding
A survey of 500 Brazilians records region (North, Southeast, South) and payment preference (cash vs. installment). Which test is most appropriate to verify whether preference and region are independent?
Show solution
Two categorical variables (region: 3 categories × preference: 2 categories) in a single random sample → independence test with . Not goodness of fit (no prescribed distribution). Not paired t-test (no pairing). Not linear regression (categorical response). - Ex. 108.39Challenge
An emergency room recorded 210 visits in one week (30 per day expected). Observed: Sun=18, Mon=40, Tue=28, Wed=25, Thu=29, Fri=42, Sat=28. Is the flow uniform across days? Test at 5%.
Show solution
: weekdays are equally requested. , , each. . Depending on observed counts, compare with . If the ER prefers Monday and Friday (common care pattern), expect high and rejection of . - Ex. 108.40Challenge
Electoral survey in 3 Brazilian states (SP, RJ, MG) with 600 voters (200 per state) records candidate preference (A, B, C). Data: SP=(80,70,50), RJ=(60,90,50), MG=(70,60,70). Test independence between state and candidate at 5% and calculate Cramér's V.
Show solution
Table $3 \times 3$ (3 candidates × 3 states). . Calculate for each cell. Compute . If , conclude that candidate preference varies by state. Report as effect size.Show step-by-step (with the why)
- Organize the $3 \times 3$ table with counts by state × candidate.
- Calculate row margins () and column margins ().
- Calculate each . Check .
- Calculate . .
- Compare with 9.49 and calculate to report the effect.
Trick: in large $r \times c$ tables, identify which cells have standardized residuals greater than 1.96 to pinpoint where association is strongest.
- Ex. 108.41ProofAnswer key
Show that for categories, where is the bilateral statistic for proportion test. This explains why .
Show solution
For categories with probabilities and : , , . . Since , each term is . This is exactly where is the test statistic for binomial proportion — showing equivalence between chi-squared with and bilateral test for proportion. - Ex. 108.42Proof
Prove the formula for the independence test in an table, explaining how many independent constraints the margins impose on the count vector.
Show solution
For $r \times c$ table: there are cells, with row constraints () and column constraints (), but the row and column constraints share the global constraint , totaling independent constraints. Therefore . Each constraint "costs" one degree of freedom because it fixes a dimension in the count vector space.
Sources
- OpenStax Statistics — Illowsky, Dean · CC-BY · Chapter 11 (§11.1–11.5). Primary source for exercises and examples.
- OpenIntro Statistics (4th ed) — Diez, Çetinkaya-Rundel, Barr · CC-BY-SA · §6.3–6.4. Conceptual approach and context-based exercises.
- Introduction to Modern Statistics — Çetinkaya-Rundel, Hardin · CC-BY-SA · §18–19. Perspective via simulation and modern inference.