22 / Inference
Chi-square tests for categorical relationships
A chi-square independence test compares observed category counts with the counts expected if two categorical variables were unrelated. It is useful for finding a pattern worth investigating, with the strength and operational meaning assessed separately from the p-value.
The business question
Are complaint, return or purchasing categories related to channel, location or customer group?
- Compare return categories across sales channels.
- Investigate whether complaint type varies across branches or product families.
- Check category distributions as part of an operational or marketing diagnostic.
How the method works
Arrange the observations in a contingency table: one variable defines the rows and another defines the columns. Each observation belongs to one row and one column. Under independence, the expected count in a cell equals its row total multiplied by its column total, divided by the overall total. These expected counts reflect the margins already present in the dataset.
The chi-square statistic adds the squared observed-minus-expected discrepancies divided by expected counts. Large contributions identify cells that drive the overall discrepancy, but the omnibus test itself does not identify a causal mechanism or authorise unadjusted testing of every cell. With sufficient expected counts, the statistic is compared with a chi-square distribution using (rows−1)(columns−1) degrees of freedom.
Sample size influences how small a discrepancy can become statistically detectable. Report actual category rates and an association measure such as Cramer’s V as well as the p-value. In a service business, a low-frequency severe complaint may matter more than a larger routine category. For thin tables, an exact or resampling approach may be more defensible than the usual approximation.
The calculation
| Symbol | Meaning |
|---|---|
| O_ij | Observed count in row i and column j. |
| E_ij | Expected count under independence. |
| N | Total independent observations. |
| r, c | Number of rows and columns. |
A worked business example
ILLUSTRATIVE SCENARIO
Illustrative retailer comparing 30-day returns for online and in-store purchases.
Inputs
- Online: 30 returned and 270 not returned, total 300.
- In-store: 10 returned and 190 not returned, total 200.
- Use Pearson’s chi-square test without the optional 2×2 continuity correction.
Work through the numbers
- The overall return rate is 40/500 = 8%.
- Expected online counts are 24 and 276; expected in-store counts are 16 and 184.
- Adding the four contributions gives χ²≈4.076 with 1 degree of freedom and p≈0.043494.
- Cramer’s V = √(4.076/500) ≈0.090; observed return rates are 10% and 5%.
The association is not proof that online selling causes returns. Product mix, customer selection or fulfilment differences could explain it. The return-rate difference and avoidable cost are more decision-relevant than the significance label alone.
Explore the decision
Test association in a count table. The live explorer has its own example inputs; it is separate from the worked calculation above.
Enable JavaScript to use this calculator. The worked example and executable Python remain available.
Apply it at work
- Define mutually exclusive category labels and the population and period being analysed.
- Build the count table, verify totals and inspect expected counts before choosing the approximation.
- Calculate the overall test, association size and descriptive cell rates.
- Investigate plausible causes with product, customer and process context; pre-plan any follow-up comparisons.
Check these assumptions
- Observations are independent; repeated purchases from one customer may require customer-level or clustered analysis.
- Categories are consistently recorded, mutually exclusive and collectively suitable for the business question.
- Expected cell counts are adequate for the chosen chi-square approximation.
- The observed sample represents the comparison population; different sampling designs need appropriate adjustment.
Common mistakes and how to avoid them
- Feeding percentages into a count test discards the sample-size information the calculation needs.
- A small p-value does not show which operational factor caused the categorical pattern.
- Collapsing or relabelling categories after seeing the result can hide important problems or bias the analysis.
How this can help the business
- Quality teams can direct investigation towards combinations of channel and outcome with unusual counts.
- Management can quantify the cost of an observed pattern before commissioning an experiment or process audit.
Investigate a meaningful categorical association when the recording quality and test approximation are sound, but base corrective action on confirmed causes and business impact.
When to use another approach
Use Fisher’s exact test for appropriate small 2×2 tables, resampling for sparse larger tables, or logistic models when several covariates need adjustment.
Reproduce the worked example in Python
The code is self-contained for this example. Dependencies and run instructions are included with the downloadable notebook.
Show executable code and recorded output
import numpy as np
from scipy import stats
observed = np.array([[30,270],[10,190]])
test = stats.chi2_contingency(observed, correction=False)
v = np.sqrt(test.statistic / observed.sum())
print("Expected counts:", test.expected_freq.tolist())
print("Chi-square, df, p:", round(test.statistic, 3), test.dof, round(test.pvalue, 6))
print("Cramer's V:", round(v, 3))Recorded output
Expected counts: [[24.0, 276.0], [16.0, 184.0]] Chi-square, df, p: 4.076 1 0.043494 Cramer's V: 0.09
Sources and further reading
Concepts are linked to primary teaching or technical sources. The business examples are illustrative applications, not reported client results.