Chi-Square Test Calculation: Formula and Example
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
A chi-square test calculates how far observed category counts differ from the counts expected under a statistical hypothesis. Enter observed and expected counts for each category to calculate the chi-square statistic; the statistic is then compared with a chi-square distribution using the appropriate degrees of freedom to find a p-value.
Formula
For a goodness-of-fit test, calculate a contribution for every category and add them:
χ² = Σ (Oᵢ − Eᵢ)² / Eᵢ
- Oᵢ is the observed count in category i.
- Eᵢ is the expected count in that category under the null hypothesis.
- χ² is the chi-square test statistic, a nonnegative quantity with no measurement unit.
- Σ means to sum the category contributions.
The calculator block shows the contribution for one category, so it is useful for understanding the arithmetic but is not a complete multi-category test unless the data have just one effective category comparison. In a real test, calculate each category’s contribution and sum them. Degrees of freedom depend on the design; for a goodness-of-fit test with k categories and no parameters estimated from the data, df = k − 1. The example uses one category contribution and df = 1 only to demonstrate the calculation, not to establish a valid test design.
Worked Example
Using the calculator’s default values, the observed count is 18 and the expected count is 20:
- Subtract expected from observed: 18 − 20 = −2.
- Square the difference: (−2)² = 4.
- Divide by the expected count: 4 / 20 = 0.2.
Therefore, the displayed single-category contribution is 0.2000, and the displayed degrees of freedom are 1. For a full test, repeat the contribution calculation for every category and add the results. For example, if another category contributes 0.5, the total statistic would be 0.2 + 0.5 = 0.7. The block’s first output is a one-category illustration, not a substitute for supplying all categories in a real analysis.
How the Chi-Square Test Works
The chi-square test compares observed frequencies with expected frequencies. A small difference between observed and expected counts produces a small contribution; a larger difference produces a larger one. Squaring the difference prevents positive and negative discrepancies from cancelling, while dividing by the expected count scales the discrepancy relative to its expected size.
The test statistic alone does not say whether a result is statistically significant. To assess significance, use the statistic together with its degrees of freedom and compare it with the chi-square distribution, or calculate a p-value. The p-value is the probability, assuming the null hypothesis is true and the test assumptions hold, of seeing a statistic at least as large as the observed one. A significance threshold should be chosen as part of the study plan; a p-value is not the probability that the null hypothesis itself is true.
Choosing the Right Chi-Square Test
- Goodness of fit: Tests whether counts in one categorical variable follow specified proportions, such as whether outcomes match a stated distribution. With k categories and no estimated parameters, df = k − 1.
- Independence: Tests whether two categorical variables are associated in a contingency table. For a table with r rows and c columns, df = (r − 1)(c − 1).
- Homogeneity: Compares the distribution of a categorical outcome across groups. It often uses the same contingency-table calculation and degrees-of-freedom formula as a test of independence, but addresses a different study question.
For a contingency table, the expected count in a cell is usually calculated as (row total × column total) / grand total. The test statistic is the sum of (observed − expected)² / expected across all cells. Choose the test based on the question and how observations were collected, not merely on the shape of the table.
Assumptions and Common Problems
Chi-square tests use counts, not averages, percentages, or measurements such as height. Each observation should contribute to one category, and observations should be independent under the study design. Repeated measurements or paired observations may require another method. Categories also need to be defined consistently and should not overlap.
- Small expected counts: The usual chi-square approximation can be unreliable when expected frequencies are sparse. A common guideline is that expected counts should generally be at least 5, though suitability depends on the test and context. For a 2 × 2 table, Fisher’s exact test may be appropriate; for other sparse tables, consider an exact or simulation-based method.
- Zero expected counts: The formula divides by Eᵢ, so an expected count of zero makes the calculation undefined. Reconsider the categories or the model that produced the expectations.
- Rounding too early: Keep full precision for expected counts and category contributions during calculations. Round the final statistic and p-value for reporting.
- Confusing significance with importance: A statistically significant result does not automatically indicate a large or practically meaningful association. Consider the size and context of the differences as well.
Interpreting and Reporting the Result
Report the test statistic, degrees of freedom, and p-value, along with enough information to identify the test and sample. A typical format is “χ²(df, N = sample size) = statistic, p = value.” For a goodness-of-fit test, describe the expected proportions; for a contingency table, provide the relevant group or table context.
A significant result for a goodness-of-fit test indicates that the observed distribution differs from the specified distribution, but does not identify which categories drive the difference. A significant independence test indicates evidence of an association, not proof of causation. Inspect observed and expected counts, residuals, or an appropriate effect-size measure to understand the pattern.
Frequently Asked Questions
How do you calculate a chi-square test by hand?
For each category or table cell, subtract the expected count from the observed count, square the difference, and divide by the expected count. Add all contributions to obtain χ², then determine the degrees of freedom and use the chi-square distribution to find the p-value.
What is the chi-square formula?
The formula is χ² = Σ (O − E)² / E, where O is an observed count and E is its corresponding expected count. Apply it to every category or cell and sum the results.
How do I calculate expected counts in a contingency table?
For each cell, multiply its row total by its column total and divide by the grand total. This gives the count expected if the row and column variables are independent.
Can a chi-square test be used with percentages?
The calculation is based on frequencies, not percentages alone. If you know the sample size, percentages may be converted to counts; otherwise, the information needed to calculate the statistic may be missing.
What does a large chi-square statistic mean?
It means observed counts differ more from expected counts relative to those expected counts. Whether that difference is statistically significant depends on the degrees of freedom and the resulting p-value.
Related
Formula used
in obs | Observed count | - | 18 | 0 in exp | Expected count | - | 20 | 0 out chi | Chi-square statistic | - | (obs - exp)^2 / exp | 4 out df | Degrees of freedom | - | 1 | 0
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.