[Home](https://learningfolio.de5.net/) / [Math](https://learningfolio.de5.net/math/) / [Math glossary](https://learningfolio.de5.net/math/glossary/) / Chi-Square Test: Formulas, Assumptions, and Examples

[Math glossary](https://learningfolio.de5.net/math/glossary/)

# Chi-Square Test: Formulas, Assumptions, and Examples

Choose a chi-square test for goodness of fit, independence, or homogeneity. Calculate expected counts, degrees of freedom, and p-values.

Written by **Learningfolio Team**

We’re subject-matter experts in math, science, parent support, and education, with 100 years of combined experience. We believe in learning, experimenting, and growing, and we review one another’s work.

## On this page

* [TL;DR](https://learningfolio.de5.net/math/chi-square-test/#tldr)
* [Key Points](https://learningfolio.de5.net/math/chi-square-test/#key-points)
* [What is a chi-square test?](https://learningfolio.de5.net/math/chi-square-test/#what-is-a-chi-square-test)
* [Which chi-square test should you choose?](https://learningfolio.de5.net/math/chi-square-test/#which-chi-square-test-should-you-choose)
* [How do you calculate expected counts and degrees of freedom?](https://learningfolio.de5.net/math/chi-square-test/#how-do-you-calculate-expected-counts-and-degrees-of-freedom)
* [How do you interpret the statistic and p-value?](https://learningfolio.de5.net/math/chi-square-test/#how-do-you-interpret-the-statistic-and-p-value)
* [When is the chi-square approximation unreliable?](https://learningfolio.de5.net/math/chi-square-test/#when-is-the-chi-square-approximation-unreliable)
* [Examples of Chi-Square Tests](https://learningfolio.de5.net/math/chi-square-test/#examples-of-chi-square-tests)
* [What mistakes can change the conclusion?](https://learningfolio.de5.net/math/chi-square-test/#what-mistakes-can-change-the-conclusion)
* [Try It Yourself](https://learningfolio.de5.net/math/chi-square-test/#try-it-yourself)
* [Questions About Chi-Square Tests](https://learningfolio.de5.net/math/chi-square-test/#questions-about-chi-square-tests)
* [Conclusion](https://learningfolio.de5.net/math/chi-square-test/#conclusion)
* [Read More](https://learningfolio.de5.net/math/chi-square-test/#read-more)
* [Research Behind This Article](https://learningfolio.de5.net/math/chi-square-test/#research-behind-this-article)

## TL;DR

**A chi-square test compares observed counts with counts expected under a null hypothesis.** Use goodness of fit for one category distribution, independence for two variables, and homogeneity to compare distributions across groups.

## Key Points

* Pearson’s chi-square statistic adds $(O-E)^2/E$ across the categories or cells.
* The expected counts depend on the null hypothesis and the study design.
* For an $r\times c$ table, the independence and homogeneity tests use $(r-1)(c-1)$ degrees of freedom.
* Very small expected counts can make the chi-square approximation unreliable.
* A p-value measures evidence against the null model. Use percentages and an effect size to describe the difference itself.

## What is a chi-square test?

A **Pearson chi-square test** uses differences between observed counts and counts predicted by a null hypothesis. In each category or table cell, call the observed count $O$ and the expected count $E$. Square the difference and divide by $E$. Add these contributions over the relevant cells.

$$
\chi^2=\sum\frac{(O-E)^2}{E}.
$$

The squared differences make the contributions nonnegative. Dividing by $E$ makes a difference of 5 count for more when the expected value is 10 than when it is 100. OpenStax’s formula review connects the same statistic to goodness of fit, independence, and homogeneity.

![Observed counts 30, 10, 20 and 20 compared with expected counts 25, 15, 25 and 15](https://learningfolio.de5.net/assets/versioned/chi-square-observed-expected.4287fe53eb3b.webp)


Each observed count has a corresponding expected count under the null model. The test combines the four discrepancies.

## Which chi-square test should you choose?

Start with how the data were collected and what the question asks. The OpenIntro Statistics textbook distinguishes designs that lead to the same arithmetic but different interpretations.

| Test | Use it for | Null hypothesis | Expected count |
| --- | --- | --- | --- |
| Goodness of fit | One categorical variable compared with specified probabilities | The population follows the stated category probabilities. | $E_i=np_i$ |
| Independence | Two categorical variables recorded on the same sampled units | The two variables are independent in the population. | $E_{ij}=\text{row total}\cdot\text{column total}/n$ |
| Homogeneity | A categorical response compared across separately sampled groups | The groups have the same response distribution. | $E_{ij}=\text{row total}\cdot\text{column total}/n$ |

For example, comparing all students’ commute mode with their grade in a single school sample is an independence question. Drawing separate samples from two schools and comparing the distribution of commute modes is a homogeneity question. Their expected-count calculations look the same.

The chi-square distribution also appears in a separate test for a single population variance under normality. That test uses quantitative values summarized by a sample variance. The [variance guide](https://learningfolio.de5.net/math/variance/) explains the underlying measure of spread.

## How do you calculate expected counts and degrees of freedom?

For goodness of fit, multiply the sample size $n$ by each null probability $p_i$. When the null specifies all $k$ category probabilities in advance, there are $k-1$ degrees of freedom because the counts must add to $n$. The NIST handbook explains how estimating model parameters from the same data can reduce the degrees of freedom further. Use the fitted-model rule documented for the test you are running.

For a table of $r$ rows and $c$ columns, multiply each row total by a column total and divide by the grand total $n$. This corresponds to multiplying the row’s count by the null model’s common column share. OpenStax’s independence chapter uses this expected frequency and gives the degrees of freedom.

$$
\operatorname{df}=(r-1)(c-1).
$$

Check that the expected counts add to the observed margins. A 2-by-2 table has one degree of freedom. Once the row and column totals are fixed, specifying one of its cells fixes the other three.

## How do you interpret the statistic and p-value?

When the test conditions hold, the p-value is the upper-tail area from the observed $\chi^2$ statistic under the appropriate chi-square distribution.

$$
p=P\left(\chi^2_{\operatorname{df}}\ge\chi^2_{\mathrm{observed}}\mid H_0\right).
$$

Choose the significance level before looking at the result. Reject the null hypothesis when the p-value is at or below that level. Otherwise, report that the data did not provide enough evidence to reject it.

To describe a table’s association, look at the proportions on the relevant denominator. If rows represent groups, row percentages show each group’s outcome distribution. The University of New Mexico’s notes describe **Cramér’s $V$**, a common effect size for contingency tables.

$$
V=\sqrt{\frac{\chi^2}{n\min(r-1,c-1)}}.
$$

A small p-value can accompany a modest effect, especially in a large sample. The sign of $(O-E)/\sqrt E$ shows which cells are above or below their expected counts. Larger absolute values identify cells contributing more to the test statistic. If using those cells for formal follow-up tests, account for multiple comparisons.

## When is the chi-square approximation unreliable?

* **Dependent observations.** A standard test treats each sampled unit as contributing one independent observation. Repeated measures or matched pairs need a method that models that dependence. McNemar’s test fits a common paired binary case.
* **Low expected counts.** OpenStax’s introductory guidance asks for expected counts of at least 5. Some methods offer other rules in larger tables. Inspect the expected counts before using the chi-square approximation. For a sparse $2\times2$ table of independent observations, Fisher’s exact test is an alternative.
* **Percentages in place of counts.** Use the original counts. The same percentages can come from 20 observations or 2,000, which give different evidence.
* **Undefined expectations.** A zero expected count leaves division by zero. Investigate whether that combination is structurally impossible or inconsistent with the stated model.

If software reports a continuity correction, note which version it used. The R help page documents that `chisq.test` defaults to a correction for a $2\times2$ contingency table. Set `correct = FALSE` in that call when your assignment asks for the uncorrected Pearson statistic shown here.

## Examples of Chi-Square Tests

### Example 1

A coin lands heads 47 times and tails 33 times in 80 trials. Compute the goodness-of-fit statistic for a fair coin.

1. The null model expects $80/2=40$ of each outcome.
2. The two contributions are $49/40$ and $49/40$.
3. The statistic is 2.45 with one degree of freedom. Its p-value is about 0.118. At the 5% level, the results do not establish that the coin is unfair.

### Example 2

A spinner has four categories with observed counts $14,22,18,26$. Test the model of four equally likely outcomes.

1. The total is 80, so the null gives $E=20$ per category.
2. The statistic is $(36+4+4+36)/20=4$.
3. With three degrees of freedom, the p-value is about 0.261. The sample gives insufficient evidence to reject equal probabilities at the 5% level.

### Example 3

Eighty independently sampled students answer two yes-or-no questions. Use the table to test independence.

| Reads weekly | Uses a library card | Does not use a library card | Row total |
| --- | --- | --- | --- |
| Yes | 30 | 10 | 40 |
| No | 20 | 20 | 40 |
| Column total | 50 | 30 | 80 |

1. Each row has expectations $40(50)/80=25$ and $40(30)/80=15$.
2. The four differences have magnitude 5. Both rows contribute $25/25+25/15=8/3$, so the statistic is $16/3$.
3. With one degree of freedom, $p\approx0.021$. At the 5% level, the data provide evidence of association.

The row percentages show that 75% of weekly readers in the sample use a card, compared with 50% of the other group. The association does not establish which behavior caused the other.

### Example 4

Two independent samples of 80 students each report walking, cycling, or taking the bus. Their counts are $(40,20,20)$ and $(20,20,40)$. Test whether the travel distributions agree.

1. The combined counts are $(60,40,60)$. Each equally sized sample has expected counts $(30,20,30)$.
2. Four cells contribute $100/30$ each. The two cycling cells contribute zero.
3. The statistic is $40/3$ with two degrees of freedom, giving $p\approx0.00127$. The data provide evidence that the distributions differ.

### Example 5

For the library-card table, report a magnitude along with the test result.

1. Take $\chi^2=16/3$, $n=80$, and $\min(r-1,c-1)=1$.
2. Compute $V=\sqrt{(16/3)/80}\approx0.258$.
3. A report could read “Pearson’s $\chi^2(1,N=80)=5.33$, $p=.021$, $V=.258$. Card use was 75% among weekly readers and 50% among other students in this sample.”

### Example 6

A small table has rows $(7,1)$ and $(2,2)$. Is the usual approximation suitable?

1. Its row totals are 8 and 4, column totals 9 and 3, and grand total 12.
2. The expected rows are $(6,2)$ and $(3,1)$.
3. Three expected counts fall below 5. For independent observations, use a small-sample method such as Fisher’s exact test rather than depending on the uncorrected Pearson approximation.

## What mistakes can change the conclusion?

### Mistake 1: Comparing joint counts as if they were percentages

If one group is much larger, it can contribute more observed cases even when its rate is lower. Use row or column percentages to communicate the pattern. Use counts for the test itself.

### Mistake 2: Setting degrees of freedom to the number of cells minus one

A $2\times3$ independence table has six cells, but the row and column totals impose constraints. Its degrees of freedom are $(2-1)(3-1)=2$.

### Mistake 3: Reporting failure to reject as proof

A large p-value can occur when effects are small, when samples are noisy, or when the sample provides limited information. Say that you did not find sufficient evidence against the null at the chosen level.

## Try It Yourself

1. How many degrees of freedom does a goodness-of-fit test have for six pre-specified face probabilities of a die?
2. A table has grand total 90, a row total of 30, and a column total of 45. Find their cell’s expected count.
3. Is a table comparing before and after responses from the same people suitable for a standard independence test?

### Answers and Reasoning

1. $6-1=5$.
2. $30\cdot45/90=15$.
3. The before and after observations are paired. Use a method suitable for paired data.

## Questions About Chi-Square Tests

### Can a chi-square test compare averages?

Pearson’s count tests compare category counts. To compare means, choose a method designed for measurements, such as a suitable t-test. Turning continuous data into categories changes the question and can discard information.

### What does a chi-square statistic of zero mean?

Every observed count equals its corresponding expected count, so each term is zero. Check that the fitted test and its assumptions are meaningful before interpreting that exact agreement.

### Why can software produce a different value for the same table?

The calculation may use a continuity correction, an exact method, or simulation. Check the test settings and compare the expected counts first.

## Conclusion

* Choose the test by how you sampled and which categorical question you ask.
* Check expected counts and degrees of freedom before reading the p-value.
* Report associations with the percentages and effect sizes that explain the pattern.

## Read More

* [Which number summarizes a sample or population](https://learningfolio.de5.net/math/parameters-and-statistics/)
* [How to organize category counts in frequency tables](https://learningfolio.de5.net/math/frequency-and-frequency-tables/)

## Research Behind This Article

1. OpenStax, *Introductory Statistics: Chapter 11 Formula Review*. [OpenStax](https://openstax.org/books/introductory-statistics/pages/11-formula-review)
2. OpenIntro, *OpenIntro Statistics: Inference for Categorical Data*. [Duke University](https://www2.stat.duke.edu/courses/Spring21/sta199.003/books/openintro-statistics.pdf)
3. NIST/SEMATECH, *Engineering Statistics Handbook: Chi-Square Goodness-of-Fit Test*. [NIST](https://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm)
4. OpenStax, *Introductory Statistics: Test of Independence*. [OpenStax](https://openstax.org/books/introductory-statistics/pages/11-3-test-of-independence)
5. University of New Mexico, *Two-Dimensional Tables and Simple Logistic Regression: Measures of Association*. [University of New Mexico](https://www.stat.unm.edu/~fletcher/R-LOGLIN3.pdf)
6. R Documentation, *Pearson’s Chi-Squared Test for Count Data*. [ETH Zürich](https://stat.ethz.ch/R-manual/R-devel/library/stats/html/chisq.test.html)
