New: 6 free SQL practice datasets with 300+ questions — try the SQL Compiler →
Statistics

Chi-Square Tests Explained

Chi-square tests explained: goodness-of-fit versus test of independence, the statistic formula, degrees of freedom, and a full worked contingency table.

Upskly AI Team October 5, 2026 7 min read
Chi-Square Tests Explained

Chi-square tests are the standard tool whenever the data is categorical, counts in buckets rather than measurements on a scale, and the question is whether those counts match what was expected. This works through both chi-square tests, the statistic’s formula, degrees of freedom, and a complete worked contingency table.

In this guide

Goodness-of-fit versus test of independence

One variable against a claim, versus two variables against each other
TestChecksNull hypothesis
Goodness-of-fitWhether observed categorical frequencies match an expected or claimed set of frequenciesThe data follows the claimed distribution
Test of independenceWhether two categorical variables are associated, rather than independentThe two variables are independent

Telling them apart

Rolling a die 60 times to check whether it is fair is a goodness-of-fit test, one categorical variable (the die face) compared to one claimed distribution (equal probabilities). Surveying voters to check whether gender and voting preference are related is a test of independence, two categorical variables examined together for an association.

The chi-square statistic

The chi-square statistic sums, across all categories, the squared difference between observed and expected counts, each divided by the expected count. Squaring keeps every category’s contribution positive; dividing by the expected count scales each gap relative to how many observations were expected there.

Worked example: is the die fair?

A fair six-sided die is rolled 60 times, with observed face counts of 8, 12, 9, 11, 10, 10 against an expected count of 10 for each face. Summing the squared relative differences across all six faces gives a chi-square statistic of 1.0, a very close fit.

Expected counts in a contingency table

For a test of independence, the expected count for any cell is its row total times its column total, divided by the grand total. A row total of 50 and a column total of 45 out of a grand total of 100 gives an expected count of 50 x 45 / 100 = 22.5.

Worked example: gender and preference

A survey of 100 people cross-tabulates gender and a yes/no preference: 30 men say yes, 20 say no; 15 women say yes, 35 say no. Under independence, the expected counts are 22.5 (men-yes), 27.5 (men-no), 22.5 (women-yes) and 27.5 (women-no). Summing the squared relative differences across all four cells gives a chi-square statistic of 9.09.

Degrees of freedom and the distribution’s shape

One fixed total costs 1 df; fixed row and column totals cost (r-1)(c-1)
TestDegrees of freedom
Goodness-of-fit, k categoriesk minus 1
Test of independence, r rows and c columns(r minus 1) x (c minus 1)

The gender-and-preference table above has 2 rows and 2 columns, giving (2 minus 1) x (2 minus 1) = 1 degree of freedom. The chi-square distribution is right-skewed and only takes non-negative values, built from squared differences, it can never go below zero. As degrees of freedom increase, it becomes less skewed and gradually more symmetric, resembling a normal distribution.

The ‘expected count of at least 5’ rule

Statisticians generally recommend every expected cell count be at least 5. The chi-square distribution is only an approximation of the test statistic’s real distribution, and that approximation becomes unreliable when too many cells are expected to have very few observations.

The decision rule

Reject the null hypothesis if the chi-square statistic exceeds the critical value, or equivalently if the p-value is below alpha. A chi-square test is treated as one-tailed: only unusually large statistics count as evidence against the null, since the statistic only grows as observed and expected counts diverge.

Same decision rule, opposite outcomes
ExampleStatisticCritical value / p-valueDecision
Die fairness (5 df)1.0critical 11.07 at alpha=0.05, p = 0.9626Fail to reject (die behaves as expected)
Gender and preference (1 df)9.09critical 3.84 at alpha=0.05Reject (an association is unlikely to be chance)

Significant does not mean strong

A significant chi-square test of independence does not tell you how strong the association is, only that an association likely exists, ruling out pure chance as the explanation. A separate measure, such as Cramer’s V, is needed to say how strong the relationship actually is. This is also why a chi-square test and a correlation coefficient are not interchangeable: chi-square examines categorical association, while a correlation coefficient measures a linear relationship between numeric variables.

Common mistakes

  • Applying a chi-square test to continuous numeric data directly. It compares counts falling into discrete categories, continuous data needs to be grouped into bins first.
  • Using the wrong degrees-of-freedom formula. Goodness-of-fit uses k minus 1; a contingency table uses (r minus 1) x (c minus 1), not r x c.
  • Ignoring expected counts below 5. The chi-square approximation becomes unreliable once too many cells have very few expected observations.
  • Treating a significant result as proof of a strong association. Significance rules out chance; strength needs a separate measure like Cramer’s V.
  • Treating the test as two-tailed. Only the upper tail of the chi-square distribution represents evidence against the null, since the statistic cannot be negative.

Cheat sheet

Quick answers
QuestionAnswer
What does a goodness-of-fit test check?Observed counts vs. one claimed distribution
What does a test of independence check?Whether two categorical variables are associated
What is the chi-square statistic formula?Sum of (observed – expected)^2 / expected
Degrees of freedom for goodness-of-fit (k categories)?k minus 1
Degrees of freedom for independence (r rows, c cols)?(r minus 1) x (c minus 1)
What is the expected-cell-count rule of thumb?At least 5
What shape is the chi-square distribution?Right-skewed, non-negative only
Is a chi-square test one-tailed or two-tailed?One-tailed (only large statistics count as evidence)
Does a significant result show how strong the association is?No, a separate measure like Cramer's V is needed
How does chi-square differ from a correlation coefficient?Chi-square tests categorical association; correlation measures a numeric linear relationship

Try it yourself

Quick check

A factory expects 160 units of output split evenly across 4 shifts (40 expected each), but observes 35, 45, 38, 42. What is the chi-square statistic (2 dp)?

Show answer

1.45. Summing the squared difference between each observed and expected count of 40, divided by 40, across all four shifts: (35-40)^2/40 + (45-40)^2/40 + (38-40)^2/40 + (42-40)^2/40 = 1.45, a close fit with no strong evidence the shifts differ from an even split.

Frequently asked questions

What is the difference between a chi-square goodness-of-fit test and a test of independence?

A goodness-of-fit test compares observed categorical frequencies to one expected or claimed distribution. A test of independence checks whether two categorical variables are associated with each other.

What is the chi-square statistic formula?

It sums, across all categories, the squared difference between observed and expected counts, each divided by the expected count.

How do you calculate the expected count in a contingency table?

The expected count for a cell is its row total times its column total, divided by the table’s grand total, under the assumption the two variables are independent.

Why should every expected cell count be at least 5 in a chi-square test?

The chi-square distribution is only an approximation of the test statistic’s real distribution, and that approximation becomes unreliable when too many cells are expected to have very few observations.

Does a significant chi-square test tell you how strong an association is?

No. It only tells you an association likely exists, ruling out pure chance. A separate measure, such as Cramer’s V, is needed to assess how strong the relationship actually is.

Why is a chi-square test treated as one-tailed?

The chi-square statistic is built from squared differences and can never be negative, so it only grows larger as observed and expected counts diverge. Only unusually large values count as evidence against the null hypothesis.

Test yourself

Timed questions on Chi-Square Tests, with an explanation for every answer.

Take the Quiz
Upskly AI Team
Learning made simple
Scroll to Top