Chi-Square Calculator
Calculate chi-square (χ²) statistics for goodness of fit and tests of independence. Enter observed and expected frequencies, or fill in a contingency table, to instantly compute χ², degrees of freedom, p-value, and per-cell contributions.
χ² = ∑ (Oᵢ − Eᵢ)² / Eᵢ
Space, comma, or tab separated values
Must have the same count as observed frequencies
Frequently Asked Questions
What is a chi-square test?
A chi-square test is a statistical hypothesis test that evaluates whether observed frequencies differ significantly from expected frequencies. It is used for categorical data and comes in two main forms: the goodness of fit test (one categorical variable compared to an expected distribution) and the test of independence (two categorical variables in a contingency table).
How do you calculate the chi-square statistic?
The chi-square statistic is calculated as χ² = Σ (O − E)² / E, where O is the observed frequency and E is the expected frequency for each category or cell. Sum this value across all categories or cells to get the total χ² statistic.
What is the difference between goodness of fit and test of independence?
The goodness of fit test checks whether a single set of observed counts matches a specified expected distribution (e.g., is a die fair?). The test of independence uses a contingency table to determine whether two categorical variables are related (e.g., does gender affect product preference?). The degrees of freedom formula differs: k − 1 for goodness of fit, (rows − 1) × (columns − 1) for independence.
What are degrees of freedom in a chi-square test?
Degrees of freedom (df) determine which chi-square distribution to use. For goodness of fit, df = k − 1 (k = number of categories). For a test of independence, df = (rows − 1) × (columns − 1). For example, a 3×4 contingency table has df = (3−1)×(4−1) = 6.
What p-value is statistically significant for chi-square?
The standard significance threshold is α = 0.05. If the p-value is less than 0.05, the result is statistically significant and you reject the null hypothesis. You can also use α = 0.10 for exploratory analysis or α = 0.01 for stricter standards. The p-value represents the probability of observing a χ² value as large or larger if the null hypothesis were true.
What is the minimum expected frequency for a chi-square test?
The chi-square approximation is reliable when all expected cell frequencies are at least 5. If some cells have expected frequencies below 5, consider combining categories, collecting more data, or using Fisher's exact test (for 2×2 tables). Very small expected frequencies can inflate the χ² statistic and produce misleadingly small p-values.
How do I interpret the per-cell contributions?
Each cell's contribution to χ² is (O − E)² / E. Large contributions indicate cells where observed counts deviate most from expected. Examining individual contributions helps identify which categories or variable combinations drive the overall association. A contribution above 3.84 (df = 1 at α = 0.05) in a single cell suggests a particularly large discrepancy.
Can chi-square test be used for continuous data?
No. Chi-square tests apply only to count data (frequencies of categorical variables). For continuous data, use t-tests (comparing two means), ANOVA (comparing multiple group means), or Pearson/Spearman correlation (measuring association between continuous variables). To use chi-square with continuous data, you must first bin the values into categories.
Chi-Square Formula
The chi-square statistic (χ²) measures how much the observed frequencies differ from what we expect under the null hypothesis. The core formula is:
Chi-Square Statistic
χ² = ∑ (Oᵢ − Eᵢ)² / Eᵢ
Oᵢ = Observed frequency for category i
Eᵢ = Expected frequency for category i
∑ = Sum across all categories or cells
The larger the χ² value, the greater the discrepancy between observed and expected frequencies. To determine whether this discrepancy is statistically significant, we compare the χ² statistic to a critical value from the chi-square distribution with the appropriate degrees of freedom — or equivalently, compute the p-value.
Goodness of Fit vs Test of Independence
There are two primary types of chi-square tests, each answering a different question:
| Feature | Goodness of Fit | Test of Independence |
|---|---|---|
| Question | Does the distribution match a specific expected distribution? | Are two categorical variables independent? |
| Input | One set of observed frequencies + one set of expected | 2D contingency table (rows × columns) |
| df formula | k − 1 (k = number of categories) | (rows − 1) × (cols − 1) |
| Example | Is a die fair? Does survey data follow a known distribution? | Is smoking status related to lung disease? Does gender affect preference? |
Goodness of Fit
Use the Goodness of Fit test when you have a single categorical variable and want to compare observed counts to theoretically expected counts. For example, if you roll a die 100 times, the expected frequency for each face is 100/6 ≈ 16.67. The goodness of fit test tells you whether the observed roll counts deviate significantly from this expectation.
Test of Independence
Use the Test of Independence (also called the chi-square contingency test) when you have two categorical variables and want to determine whether they are statistically related. The expected frequency for each cell is calculated as:
Eᵢⱼ = (Row Totalᵢ × Column Totalⱼ) / Grand Total
Degrees of Freedom
Degrees of freedom (df) determine which chi-square distribution to use when computing the p-value. The df reflects how many independent pieces of information you have after accounting for constraints.
- Goodness of Fit:df = k − 1, where k is the number of categories. We lose 1 degree of freedom because the observed frequencies must sum to the total.
- Test of Independence:df = (rows − 1) × (columns − 1). For a 2×2 table, df = 1. For a 3×4 table, df = 6.
A higher df shifts the chi-square distribution to the right, requiring a larger χ² value to achieve statistical significance at the same alpha level.
Chi-Square Critical Values Table
The table below shows critical χ² values for common degrees of freedom and significance levels. If your calculated χ² exceeds the critical value, the result is statistically significant.
| df | α = 0.10 | α = 0.05 | α = 0.025 | α = 0.01 |
|---|---|---|---|---|
| 1 | 2.706 | 3.841 | 5.024 | 6.635 |
| 2 | 4.605 | 5.991 | 7.378 | 9.21 |
| 3 | 6.251 | 7.815 | 9.348 | 11.345 |
| 4 | 7.779 | 9.488 | 11.143 | 13.277 |
| 5 | 9.236 | 11.07 | 12.832 | 15.086 |
| 6 | 10.645 | 12.592 | 14.449 | 16.812 |
| 8 | 13.362 | 15.507 | 17.535 | 20.09 |
| 10 | 15.987 | 18.307 | 20.483 | 23.209 |
| 15 | 22.307 | 24.996 | 27.488 | 30.578 |
| 20 | 28.412 | 31.41 | 34.17 | 37.566 |
Chi-Square Calculation Examples
Example 1: Goodness of Fit — Fair Die
You roll a die 100 times and observe: 16, 18, 16, 14, 12, 24. Is the die fair? Expected frequency for each face: 100 / 6 ≈ 16.67.
χ² = (16 − 16.67)² / 16.67 + (18 − 16.67)² / 16.67 + ...
χ² ≈ 4.68, df = 5
p-value ≈ 0.456
Result: Not significant (α = 0.05). No evidence the die is unfair.
Example 2: Test of Independence — Gender vs. Preference
Survey of 100 people: Do men and women prefer different products? Contingency table: Men — 20 prefer A, 30 prefer B; Women — 35 prefer A, 15 prefer B.
Row totals: Men = 50, Women = 50
Col totals: A = 55, B = 45, Grand = 100
Expected (Men, A) = 50 × 55 / 100 = 27.5
χ² ≈ 8.08, df = 1
p-value ≈ 0.004
Result: Significant (α = 0.05). Gender and preference are related.
Assumptions & Limitations
- Independence: Each observation must be independent of others. Repeated measures or clustered data violate this assumption.
- Expected frequency rule:Each expected cell frequency should be at least 5. If not, consider combining categories or using Fisher's exact test (for 2×2 tables).
- Categorical data: Chi-square tests apply only to counts, not means or proportions expressed as decimals. For continuous data, use t-tests or ANOVA.
- Sample size: The chi-square approximation improves with larger samples. Very small samples may produce unreliable p-values.
- Two-sided only: Chi-square tests are inherently non-directional. They detect any departure from the expected distribution, not a specific direction.