STAT5002 Chap.12 Chi-squared Tests of Goodness of Fit and Independence
Chi-squared Tests of Goodness of Fit and Independence
Chi-squared tests answer questions about counts: does a set of categories occur in the proportions someone claims, and are two categorical variables related? The chapter starts with goodness of fit. Categorical data are modelled as draws from a box whose ticket proportions are p, the null hypothesis fixes those proportions at stated values, and each category gets an expected frequency n times its stated proportion.
Pearson's statistic adds up (observed minus expected) squared, divided by expected, over all categories, and under the null it approximately follows a chi-squared curve with k - 1 degrees of freedom. Only large values count against the null, so the p-value is always the upper tail, found with pchisq(..., lower.tail = F) or the one-line chisq.test().
You then learn when the curve can be trusted: the rule of thumb is that every expected frequency should be at least 5, and when it fails you simulate the box under the null or use chisq.test(..., simulate = T). With only two categories the chi-squared test is exactly the two-sided Z-test on one proportion, because the statistic equals Z squared.
Degrees of freedom are counted as free parameters in the full model minus free parameters under the null. The second half applies the same statistic to a two-way table. Independence means each cell probability is a row margin times a column margin; the margins are estimated from the totals, so each expected count is row total times column total over the grand total, and the degrees of freedom become (r - 1)(c - 1).
What this chapter covers
- 01
Box model for a categorical variable
- 02
Hypotheses for goodness of fit: H0 p = p0 against not H0
- 03
Expected frequencies E = n p0
- 04
Pearson's chi-squared statistic
- 05
Chi-squared distribution as a sum of squared standard normals
- 06
Upper-tail p-values with pchisq() and chisq.test()
- 07
Expected frequencies of at least 5 as a rule of thumb
- 08
Two categories: the chi-squared test equals a two-sided Z-test
- 09
Simulation and chisq.test(..., simulate = T) for small expected counts
- 10
Counting df from free parameters
- 11
Test of independence in a two-way table
- 12
Expected counts from row and column totals; df (r - 1)(c - 1)
Worked example · free
Year level and daily use of R
- +1H0: year level and daily R use are independent, p_ij = p_i. p_.j for every cell; H1: they are not independent. Row totals 60, 70, 70; column totals 85, 115; n = 200.
- +1Expected counts are row total x column total / 200: first-year cells 25.50 and 34.50; second-year 29.75 and 40.25; third-year or later 29.75 and 40.25. All exceed 5, so the chi-squared approximation is reasonable.
- +1Pearson's statistic: the six terms are 2.206, 1.630, 0.002, 0.002, 1.767, 1.306, summing to 6.913.
- +1Here df = (3 - 1)(2 - 1) = 2. P-value = pchisq(6.913, df = 2, lower.tail = F) = 0.0315; the 5% cut-off is qchisq(0.95, 2) = 5.991.
- +1Since 0.0315 is below 0.05, reject H0. Daily use rises from 30% of first-year students to 53% of third-year or later students.
Key terms
- Expected frequency
- The count a category or cell would have on average if the null hypothesis were true, n times its null probability.
- Pearson's statistic
- The sum over all categories or cells of (observed minus expected) squared, divided by expected.
- Chi-squared distribution
- The distribution of a sum of d squared independent standard normal draws; it is skewed right and becomes more symmetric as d grows.
- Goodness of fit test
- A test of whether category proportions in a box equal a fully stated set of proportions, using Pearson's statistic on k - 1 degrees of freedom.
- Test of independence
- A chi-squared test of whether the row and column variables of a two-way table are independent, with expected counts built from the table's margins.
- Marginal probability
- The chance of landing in a given row or a given column of a two-way table, estimated by that row or column total divided by n.
- Free parameters
- The probabilities in a model that can vary independently once the constraint that they sum to 1 is taken into account.
Chi-squared Tests of Goodness of Fit and Independence FAQ
Why is the chi-squared test always one-sided?
Pearson's statistic grows with any departure from the null proportions, in either direction for any category. A value near zero means the counts sit close to the claim, which is agreement, so only the upper tail is evidence against the null.
What should I do when chisq.test() warns that the approximation may be incorrect?
The warning appears when some expected frequency is below 5. Rerun with simulate = T, which builds the null distribution by drawing samples from the box the null specifies, and report that simulated p-value instead.
How do I get expected counts for a two-way table in R?
Compute row and column totals with rowSums() and colSums(), then outer(R, C) / n gives each row total times column total over the grand total. The function chisq.test() does the same internally.
Why are the degrees of freedom (r - 1)(c - 1) for independence?
The full table has rc - 1 free probabilities. Under independence only the margins are free, r - 1 for rows and c - 1 for columns, and subtracting leaves rc - r - c + 1, which factorises to (r - 1)(c - 1).
Exam move
Build a blank calculation table with columns for observed, expected, the squared gap, and the term, and fill it by hand for one goodness of fit question and one two-way table until both feel routine. Check every expected column against its totals before adding terms. Memorise the df rule as free parameters in the full model minus free parameters under the null, and test yourself by deriving k - 1 and (r - 1)(c - 1) from it.
Rehearse three R calls until you can write them without notes: chisq.test(O, p = p0), chisq.test(matrix) and the simulate = T version, and know when the small expected count warning means you need the last one. Finally, square qnorm(0.975) and compare it with qchisq(0.95, 1) once yourself, so the link between the Z-test and the two-category chi-squared test sticks.
Working through Chi-squared Tests of Goodness of Fit and Independence in STAT5002? Sia is AskSia’s AI Statistics tutor — ask any STAT5002 Chi-squared Tests of Goodness of Fit and Independence question and get a clear, step-by-step explanation grounded in how STAT5002 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.
STAT5003 Computational Statistical Methods · DATA2002 Data Analytics: Learning from Data · QBUS5001 Foundation in Data Analytics for Business