Correlation, regression and ANOVA work with quantitative variables: meters, euros, satisfaction points. But a good share of NovaMarket's data is categorical: purchase channel (in-store/online), payment method, age bracket, staying in or cancelling a Club Nova membership. With categories there are no means to compare; there are frequencies to count, and the business question becomes "are these two classifications related?". In this lesson we will revisit the contingency tables we used with probabilities in Module 3 — but now with inference —, build the chi-square test of independence step by step (expected frequencies, statistic, degrees of freedom and reading its table), learn the goodness-of-fit test for comparing a set of frequencies against a reference distribution, pinpoint the discrepancies with standardized residuals, measure the strength of the association with Cramér's V, and connect it all with the two-proportion z test from Module 5.
Contents
- From probabilities to inference: the contingency table, second visit
- The logic of the test: observed vs expected frequencies
- Case 1 — Does payment method depend on the channel? Test of independence step by step
- The chi-square distribution and how to read its table
- Validity conditions of the test
- Where is the discrepancy? Standardized residuals
- How strong is the association? Cramér's V
- Goodness-of-fit test: do sales follow the historical pattern?
- Connection with the two-proportion z test
From probabilities to inference: the contingency table, second visit
In Probability Rules we used contingency tables to compute joint and conditional probabilities: there, the frequencies were the starting point and we took them as truth. Now we make the inferential leap of Module 5: the table comes from a sample, and the question is whether the pattern it shows reflects a relationship that is real in the population or is just sampling noise. The test that answers that question is the chi-square test of independence (\( \chi^2 \), "chi-square"):
- \( H_0: \) the two variables are independent (how one splits does not depend on the other)
- \( H_1: \) the two variables are related
The logic of the test: observed vs expected frequencies
The idea is to ask \( H_0 \): "if the variables were independent, what table would we expect to see?", and then measure how far the real table strays from that ideal one. From Module 3 we know that under independence \( P(A \cap B) = P(A) \cdot P(B) \); translated into frequencies, the expected frequency of each cell is
\[ E_{ij} = \frac{(\text{its row total}) \times (\text{its column total})}{n} \]
and the overall discrepancy accumulates cell by cell in the statistic
\[ \chi^2 = \sum_{\text{cells}} \frac{(O_{ij} - E_{ij})^2}{E_{ij}} \]
where \( O_{ij} \) is what was observed. Each term measures how far a cell deviates from what is expected, relative to the expected value (dividing by \( E_{ij} \) keeps large cells from dominating merely by being large). If \( H_0 \) is true, all the deviations are small and \( \chi^2 \) stays near 0; if there is a relationship, some cells deviate a lot and \( \chi^2 \) grows. As with ANOVA, the test is right-tailed: only large values contradict independence.
Case 1 — Does payment method depend on the channel? Test of independence step by step
Marta wants to know whether payment method depends on the channel (a practical decision: which payment gateways to prioritize on the website and which checkouts to install in-store). A random sample of 200 purchases is taken:
Observed frequencies \( O_{ij} \):
| Channel \ Payment | Card | Cash / cash on delivery | Mobile payment | Total |
|---|---|---|---|---|
| In-store | 70 | 35 | 15 | 120 |
| Online | 52 | 4 | 24 | 80 |
| Total | 122 | 39 | 39 | 200 |
Step 1 — Expected frequencies. Under independence, the payment split should be the same in both channels. For example, the In-store–Card cell: \( E = 120 \times 122 / 200 = 73.2 \). The full table:
| Channel \ Payment | Card | Cash | Mobile |
|---|---|---|---|
| In-store | 73.2 | 23.4 | 23.4 |
| Online | 48.8 | 15.6 | 15.6 |
(Check: the expected frequencies reproduce the same row and column totals as the observed ones.)
Step 2 — Contributions to the statistic. Cell by cell, \( (O-E)^2/E \):
| Channel \ Payment | Card | Cash | Mobile |
|---|---|---|---|
| In-store | \( (70-73.2)^2/73.2 = 0.14 \) | \( (35-23.4)^2/23.4 = 5.75 \) | \( (15-23.4)^2/23.4 = 3.02 \) |
| Online | \( (52-48.8)^2/48.8 = 0.21 \) | \( (4-15.6)^2/15.6 = 8.63 \) | \( (24-15.6)^2/15.6 = 4.52 \) |
\[ \chi^2 = 0.14 + 5.75 + 3.02 + 0.21 + 8.63 + 4.52 = 22.27 \]
Step 3 — Degrees of freedom. In a table with \( r \) rows and \( c \) columns:
\[ df = (r-1)(c-1) = (2-1)(3-1) = 2 \]
The intuition: with the row and column totals fixed, you can only fill in 2 cells freely; the rest are determined.
Step 4 — Decision. The 5% critical value with 2 df is \( \chi^2_{0.05;,2} = 5.99 \) (we will see where it comes from shortly). Since \( 22.27 > 5.99 \), we reject \( H_0 \) decisively (p < 0.001): payment method depends on the channel.
The chi-square distribution and how to read its table
Under \( H_0 \), the statistic follows the chi-square distribution, the third and last table-based distribution of the course (after the t and the F):
- It only takes positive values (it is a sum of squares) and is right-skewed.
- It depends on a single parameter: the degrees of freedom. Its mean is exactly the df, which gives a quick mental benchmark: a \( \chi^2 \) far above its df starts to look suspicious for \( H_0 \).
- With more df it shifts right and becomes more symmetric.
How to read its table: just like the t table — one row per degrees of freedom, one column per right-tail area (α); the intersection gives the critical value. An excerpt:
| df \ α | 0.10 | 0.05 | 0.01 |
|---|---|---|---|
| 1 | 2.71 | 3.84 | 6.63 |
| 2 | 4.61 | 5.99 | 9.21 |
| 3 | 6.25 | 7.81 | 11.34 |
| 4 | 7.78 | 9.49 | 13.28 |
Row 2, column 0.05: the 5.99 we used. Our 22.27 exceeds even the 1% critical value (9.21).
Validity conditions of the test
The chi-square is an approximation (frequencies are discrete; the distribution, continuous), and it works well if:
- Expected frequencies ≥ 5 in every cell (the most widespread rule of thumb). Careful: the condition is checked on the expected frequencies, not the observed ones — our Online–Cash cell has \( O = 4 \), but its expected value is 15.6, so there is no problem.
- Observations that are independent of one another, each counted in one cell only (one purchase, one row of the table; never the same customer counted twice).
If any expected frequency drops below 5, the usual fixes are to merge related categories (e.g., fusing two minority payment methods into "other") or to enlarge the sample.
Where is the discrepancy? Standardized residuals
Like ANOVA's F, the global \( \chi^2 \) says "there is a relationship", but not where. To locate it we use each cell's standardized residuals:
\[ r_{ij} = \frac{O_{ij} - E_{ij}}{\sqrt{E_{ij}}} \]
which behave approximately like z-scores: values beyond ±2 flag cells with a notable deviation (the sign indicates an excess or a shortfall relative to independence).
| Channel \ Payment | Card | Cash | Mobile |
|---|---|---|---|
| In-store | −0.37 | +2.40 | −1.74 |
| Online | +0.46 | −2.94 | +2.13 |
Reading for Marta: card behaves the same in both channels (tiny residuals — consistent with its dominant role: the 61% of card-paying customers we estimated in Module 5). The relationship is generated by cash, overrepresented in-store and nearly absent online, and by mobile payment, overrepresented online. Informed decision: strengthen Bizum/wallets on the website and keep the cash checkout in-store.
How strong is the association? Cramér's V
With large samples, even trivial relationships produce significant \( \chi^2 \) values (the eternal lesson: significance ≠ relevance). The standard strength measure is Cramér's V:
\[ V = \sqrt{\frac{\chi^2}{n \cdot \min(r-1,, c-1)}} = \sqrt{\frac{22.27}{200 \times 1}} = \sqrt{0.111} = 0.33 \]
V ranges from 0 (independence) to 1 (perfect association); as a rough guide, ~0.1 is weak, ~0.3 moderate and ~0.5 strong. Our 0.33 indicates a moderate association: real and actionable, without being deterministic. It is the categorical analogue of Pearson's r and ANOVA's η²: the module's complete trio of "effect sizes".
Goodness-of-fit test: do sales follow the historical pattern?
The chi-square's second great application does not cross two variables: it compares one categorical variable against a reference distribution. Historically, NovaMarket's sales split by category like this (the share we already used in Module 3): fresh food 38%, pantry 27%, beverages 14%, household and personal care 12%, other 9%. After the latest assortment overhaul, Marta wants to know whether the pattern has shifted. 400 sales lines from the past week are classified:
| Category | Observed \( O_i \) | Historical % | Expected \( E_i = 400 \times p_i \) | \( (O_i-E_i)^2/E_i \) |
|---|---|---|---|---|
| Fresh food | 132 | 38% | 152 | 2.63 |
| Pantry | 106 | 27% | 108 | 0.04 |
| Beverages | 62 | 14% | 56 | 0.64 |
| Household and personal care | 55 | 12% | 48 | 1.02 |
| Other | 45 | 9% | 36 | 2.25 |
| Total | 400 | 100% | 400 | χ² = 6.58 |
Here \( H_0 \) is "sales follow the historical distribution", the degrees of freedom are \( df = k - 1 = 5 - 1 = 4 \) (k categories; the −1 because the percentages must sum to 100), and the 5% critical value is \( \chi^2_{0.05;,4} = 9.49 \). Since \( 6.58 < 9.49 \), we do not reject \( H_0 \): the observed deviations (less fresh food, more "other") are compatible with sampling chance. A good reminder from Module 5: not rejecting does not prove that nothing has changed — only that 400 lines are not enough to claim it; if the suspicion persists, a larger sample is in order (power).
Connection with the two-proportion z test
An important special case: the 2×2 table. Comparing the proportion of card payments between Madrid-Centro (0.68) and Cuenca (0.49), as we did in Module 5 with the two-proportion z test, is exactly equivalent to building the 2×2 table (store × pays-by-card yes/no) and applying chi-square to it: the identity
\[ \chi^2_{(1,df)} = z^2 \]
holds, and also between critical values: \( 3.84 = 1.96^2 \). The two tests always give the same two-tailed p-value; they are the same inference in two outfits. When should you use each? The z when there are exactly two proportions and you want a one-tailed test or a confidence interval for the difference; the chi-square when there are more than two rows or columns — its great advantage is generalizing effortlessly, just as ANOVA generalized the t test.
Common Mistakes and Tips
- Applying chi-square to percentages or means. The statistic is always computed on counts (absolute frequencies). Converting the table to percentages before computing invalidates everything (n disappears, and n is the strength of the evidence).
- Checking the "≥ 5" condition on the observed frequencies. The rule applies to the expected frequencies. An observed 4 with an expected 15.6 is perfectly valid.
- Forgetting that the test is right-tailed. A tiny \( \chi^2 \) does not "confirm" independence; and a suspiciously perfect fit sometimes betrays doctored data.
- Confusing significance with strength. With n = 20,000, an enormous \( \chi^2 \) can correspond to a Cramér's V of 0.05: a real but irrelevant relationship. Always report both.
- Stopping at the global verdict. Without standardized residuals you don't know which cells generate the relationship, and the business decision lives precisely there.
- Counting the same individual twice (a customer with two purchases in the sample, a survey answered both in-store and online). It breaks independence and biases the statistic.
- Reading the association as causation. That membership churn is associated with age bracket does not explain why; the lurking variables from the module's first lesson stalk contingency tables too.
Exercises
Exercise 1. Club Nova (380,000 members) follows a sample of 300 members for a year to study whether cancelling depends on the age bracket:
| Age \ Status | Cancelled | Still a member | Total |
|---|---|---|---|
| Under 35 | 22 | 78 | 100 |
| 35–55 | 12 | 108 | 120 |
| Over 55 | 8 | 72 | 80 |
| Total | 42 | 258 | 300 |
Test independence at the 5% level (\( \chi^2_{0.05;,2} = 5.99 \)) and, if warranted, locate the discrepancy with the standardized residuals of the "Cancelled" column.
Exercise 2. Carrier audit: out of 200 deliveries, LogiExpress handled 140 (14 late) and RápidoSur 60 (12 late). (a) Build the 2×2 table and test at the 5% level whether being late depends on the carrier (\( \chi^2_{0.05;,1} = 3.84 \)). (b) Cross-check the relationship with the z test: what z is equivalent to your \( \chi^2 \)? (c) Does anything change if Marta's question was, from the outset, "is RápidoSur worse?"
Exercise 3. Customer service logs 100 weekly complaints and wants to know whether they are spread uniformly across the 5 weekdays: Monday 32, Tuesday 18, Wednesday 16, Thursday 12, Friday 22. Test the goodness of fit at the 5% level (\( \chi^2_{0.05;,4} = 9.49 \)) and name the day responsible for the biggest discrepancy.
Solutions
Exercise 1. Expected values for the "Cancelled" column: \( 100 \times 42/300 = 14 \); \( 120 \times 42/300 = 16.8 \); \( 80 \times 42/300 = 11.2 \) (and "Still a member": 86; 103.2; 68.8 — all ≥ 5). Contributions: \( (22-14)^2/14 = 4.57 \); \( (12-16.8)^2/16.8 = 1.37 \); \( (8-11.2)^2/11.2 = 0.91 \); \( (78-86)^2/86 = 0.74 \); \( (108-103.2)^2/103.2 = 0.22 \); \( (72-68.8)^2/68.8 = 0.15 \). Total \( \chi^2 = 7.97 > 5.99 \): we reject — cancelling depends on the age bracket. "Cancelled" residuals: under-35s \( (22-14)/\sqrt{14} = +2.14 \); 35–55: \( -1.17 \); over-55s: \( -0.96 \). The discrepancy is concentrated in the under-35s, who cancel far more than expected (22% observed against the 14% overall): a clear candidate for a youth loyalty plan. Common mistake: computing df as cells − 1 = 5 instead of (3−1)(2−1) = 2.
Exercise 2. (a) Table: LogiExpress 14 late / 126 on time; RápidoSur 12 / 48; totals 26 late, 174 on time. Expected: 18.2 and 7.8 (late); 121.8 and 52.2 (on time) — all ≥ 5. \( \chi^2 = 0.97 + 2.26 + 0.14 + 0.34 = 3.71 < 3.84 \): at the 5% level we do not reject independence, even though the sample rates (10% vs 20%) are double one another — with only 60 RápidoSur shipments, the evidence stops right at the edge (p ≈ 0.054). (b) \( z = \sqrt{3.71} = 1.93 < 1.96 \): same conclusion, as the identity \( \chi^2 = z^2 \) requires. (c) Yes: with a one-tailed hypothesis stated a priori (\( H_1: p_{\text{RS}} > p_{\text{LE}} \)), the critical z is 1.645 and 1.93 does reject. The direction must be decided before looking at the data — and it fits what we already knew about the two carriers (8% vs 15% late historically). Common mistake: switching to one-tailed after seeing the result; that is the p-hacking from Module 5.
Exercise 3. Expected: \( 100/5 = 20 \) per day. \( \chi^2 = \frac{144+4+16+64+4}{20} = \frac{232}{20} = 11.6 > 9.49 \): we reject uniformity — complaints are not evenly spread. Biggest discrepancy: Monday, residual \( (32-20)/\sqrt{20} = +2.68 \) (it accumulates whatever happened over the weekend, most likely); Thursday falls below expectation (\( -1.79 \)). Operational implication: reinforce the customer service team on Mondays. Common mistake: using df = 5 instead of k − 1 = 4, or allocating the expected values according to the observed ones (the expected frequencies come from the H₀ model — here, uniform —, never from the data).
Conclusion
With the chi-square we complete the module's arsenal: the test of independence told us that payment method depends on the channel (χ² = 22.27, with cash anchored to the store and mobile payment to online, V = 0.33), the goodness-of-fit test that the sales mix remains compatible with the historical 38/27/14/12/9 pattern, and the standardized residuals taught us to point at the exact cell where each discrepancy lives — besides discovering that the old two-proportion z test was a chi-square in disguise. The module thus closes with one unifying idea: measure relationships (r and rs), turn them into prediction (regression), compare groups (ANOVA) and cross-tabulate categories (χ²), always with their significance test and their effect size. But all these tools share a silent assumption: that the observations are independent and time plays no part. As soon as the data are weekly sales, one after another — with their trend, their Christmas seasonality and their streaks —, that assumption blows apart and new machinery is needed: the Time Series that opens Module 7.
Statistics Course
Module 1: Introduction to Statistics
Module 2: Describing Data
- Measures of Central Tendency
- Measures of Dispersion
- Measures of Position and Outliers
- Graphical Representation of Data
Module 3: Probability
Module 4: Probability Distributions
- The Binomial Distribution
- The Normal Distribution
- Other Important Distributions
- The Central Limit Theorem
Module 5: Statistical Inference
Module 6: Data Analysis
- Correlation Analysis
- Regression Analysis
- Analysis of Variance (ANOVA)
- Categorical Data Analysis: Chi-Square
