How to Calculate Chi-Square Test

Learn how to perform a chi-square test of independence and goodness-of-fit. This guide explains the chi-square formula, how to build a contingency table, and how to interpret results using degrees of freedom.

What Is the Chi-Square Test?

The chi-square (χ²) test is a non-parametric statistical test used with categorical data. The chi-square test of independence determines whether two categorical variables are associated. The chi-square goodness-of-fit test determines whether an observed frequency distribution matches a hypothesized one. Both tests use the same χ² statistic but differ in how expected frequencies are computed and how degrees of freedom are defined.

The Chi-Square Formula

The chi-square test statistic is: χ² = Σ [(Oᵢ − Eᵢ)² / Eᵢ], where Oᵢ is the observed frequency in category i and Eᵢ is the expected frequency under the null hypothesis. The sum is taken over all categories or all cells in the contingency table. Larger χ² values indicate greater discrepancy between observed and expected counts, providing evidence against the null hypothesis.

Chi-Square Test of Independence

To test whether two categorical variables are independent, organize data into a contingency table with r rows and c columns. The expected frequency for each cell is: Eᵢⱼ = (Row total × Column total) / Grand total. Degrees of freedom are df = (r − 1)(c − 1). If the computed χ² exceeds the critical value from the chi-square table at the chosen significance level and df, reject the null hypothesis of independence.

Worked Example — Test of Independence

Suppose 200 people are surveyed on coffee preference (Yes/No) by gender (Male/Female). Observed: Male-Yes=60, Male-No=40, Female-Yes=50, Female-No=50. Row totals: Male=100, Female=100. Column totals: Yes=110, No=90. Grand total=200. Expected: Male-Yes = 100×110/200 = 55, Male-No = 45, Female-Yes = 55, Female-No = 45. χ² = (60−55)²/55 + (40−45)²/45 + (50−55)²/55 + (50−45)²/45 ≈ 0.455 + 0.556 + 0.455 + 0.556 ≈ 2.02. With df = 1, the critical value at α=0.05 is 3.841, so we fail to reject independence.

Goodness-of-Fit Test

The goodness-of-fit test compares one observed categorical distribution against a theoretical one. For example, testing whether a die is fair: roll it 60 times and compare observed counts in each of 6 faces against the expected 10 each. Degrees of freedom = number of categories − 1 − number of parameters estimated from data. If no parameters are estimated from data, df = k − 1 where k is the number of categories.

Assumptions and Requirements

Chi-square tests require that observations are independent and that each expected cell frequency is at least 5 (the "rule of five"). When expected counts fall below 5, the chi-square approximation becomes unreliable; in such cases, use Fisher's exact test for 2×2 tables or combine sparse categories. The test is two-sided by nature — a large χ² in either direction (more or fewer counts than expected) contributes equally to the statistic.

Effect Size — Cramér's V

Statistical significance alone does not convey the strength of association. Cramér's V provides an effect size measure for chi-square tests: V = √[χ² / (n × min(r−1, c−1))], where n is the total sample size. V ranges from 0 (no association) to 1 (perfect association). Benchmarks: V ≈ 0.1 is small, 0.3 is medium, 0.5 is large. Always report Cramér's V alongside the chi-square result to convey practical significance.

Try These Calculators

Put what you learned into practice with these free calculators.