Skip to main content

Chi-Square Calculator

Run a chi-square test on a table of counts, up to 8 by 8, to see whether two things are associated, with Fisher's exact test, Yates' correction and the G-test beside it, the cells that stand out, and a 2 × 2 table's odds ratio and relative risk. Or test counts against an expected ratio, such as 3:1 or 9:3:3:1, or find the P value and critical value of a χ² you already have.

Calculator

Chi-square test

Test

Are two things related? Counts in a table, one row per group and one column per outcome: the test of independence, or of homogeneity.

Doll and Hill asked 60 women with lung cancer and 60 women in hospital with other diseases whether they smoked: 41 and 28 did. Is smoking associated with lung cancer?

Doll & Hill, Smoking and carcinoma of the lung, BMJ 1950;2:739–748, Table IV. They found χ² = 5.76 with 1 degree of freedom, 0.01 < P < 0.02.

Counts
Rows2
Columns2
Total
60
60
Column totals: 6951120

Counts of individuals, each counted once. Type over the names, or paste a table from a spreadsheet into any cell, names and all.

Significant

A significant association

χ²
5.763
df
1
P
0.0164

χ² = 5.763 with 1 degree of freedom is above 3.841, the critical value at 0.05, so P = 0.0164: if the rows and columns were independent, counts this far from the expected would be rare. Smokers: 68.3% of Lung cancer against 46.7% of Controls.

For a report: χ²(1, N = 120) = 5.76, p = .016, φ = .22

Counted and expected

▲ more and ▼ fewer than expected, where the adjusted residual passes ±1.96: the cells that make the difference.

SmokersNon-smokersTotal
Lung cancer4134.50, more than expected1925.50, fewer than expected60
Controls2834.50, fewer than expected3225.50, more than expected60
Total6951120
Tests on these counts

Choose one for the finding and the report line.

How strong

Read with the rows as the groups and Smokers as the outcome: Smokers is 68.3% of Lung cancer and 46.7% of Controls, so its risk is 1.46 times and its odds 2.47 times as high in Lung cancer. A case-control study can give only the odds ratio; relative risk needs groups followed up, as in a cohort or a trial. Swap rows and columns above if your table runs the other way.

φ (Cramér's V)
0.219
Small, by Cohen's guide
Odds ratio
2.47
95% CI 1.17 to 5.19
Relative risk
1.46
95% CI 1.06 to 2.02

Working

  1. 01N = 120; row totals 60, 60; column totals 69, 51
  2. 02E = row total × column total ÷ N
  3. 03Lung cancer, Smokers: E = 60 × 69 ÷ 120 = 34.50; (41 − 34.50)² ÷ 34.50 = 1.2246
  4. 04Lung cancer, Non-smokers: E = 60 × 51 ÷ 120 = 25.50; (19 − 25.50)² ÷ 25.50 = 1.6569
  5. 05Controls, Smokers: E = 60 × 69 ÷ 120 = 34.50; (28 − 34.50)² ÷ 34.50 = 1.2246
  6. 06Controls, Non-smokers: E = 60 × 51 ÷ 120 = 25.50; (32 − 25.50)² ÷ 25.50 = 1.6569
  7. 07χ² = 1.2246 + 1.6569 + 1.2246 + 1.6569 = 5.763
  8. 08df = (rows − 1) × (columns − 1) = (2 − 1) × (2 − 1) = 1
  9. 095.763 > 3.841, the critical value for df 1 at 0.05, so P = 0.0164 < 0.05

The chi-square distribution, df 1

The chi-square distribution with 1 degree of freedomChi-square distribution with 1 degree of freedom. The critical value at 0.05 is 3.841. χ² = 5.763, with P = 0.0164 in the tail beyond it.0246810χ²critical 3.841χ² 5.763
The dark tail beyond your χ² is P = 0.0164, the chance of a χ² at least this large if the rows and columns were independent. Past the dashed line at 3.841 lies the 5% of chance results that would count as significant at 0.05.

Worked example

Smoking and lung cancer: Doll and Hill's women, 1950

  1. 01Smokers: 41 of 60 women with lung cancer, 28 of 60 controls; N = 120
  2. 02Expected smokers in each group: 60 × 69 ÷ 120 = 34.5; non-smokers: 60 × 51 ÷ 120 = 25.5
  3. 03Smokers: (41 − 34.5)² ÷ 34.5 = 1.2246; non-smokers: (19 − 25.5)² ÷ 25.5 = 1.6569; the controls give the same
  4. 04χ² = 1.2246 + 1.6569 + 1.2246 + 1.6569 = 5.763
  5. 05df = (2 − 1) × (2 − 1) = 1; critical value at 0.05 = 3.841

χ² = 5.763 is above 3.841, so P = 0.0164: smoking was associated with lung cancer, as Doll and Hill found (χ² = 5.76, 0.01 < P < 0.02). The odds of having smoked were 2.47 times as high in the women with lung cancer (95% CI 1.17 to 5.19).

How it works

How to do a chi-square test

A chi-square test compares the counts you observed with the counts you would expect if nothing were going on: the null hypothesis. In a table, nothing going on means the rows and columns are independent, so each row splits across the columns in the same proportions as the whole sample, and a cell expects its row total times its column total, divided by N. In a goodness-of-fit test, it means the counts follow the ratio you expect, so each category expects its share of N.

For each cell, square the difference between observed and expected and divide by the expected: (O − E)² ÷ E. Add them up for χ². The bigger it is, the further the counts are from what the null hypothesis predicts. P is the chance of a χ² at least this large if the null hypothesis were true, read from the χ² distribution for your degrees of freedom. Below 0.05, or the α you chose, the result is significant: counts this far from the expected would be rare by chance alone.

Degrees of freedom count the cells that could vary once the totals are fixed: (rows − 1) × (columns − 1) for a table and categories − 1 for goodness of fit. A 2 × 2 table has 1, so its critical value at 0.05 is 3.841; a 3 × 3 table has 4, with 9.488. When the expected ratio was worked out from the same counts, as in a Hardy-Weinberg test, take off one more for each value estimated.

Chi-square is an approximation that holds when the expected counts are large enough. With small counts, Fisher's exact test weighs every table with the same row and column totals and adds up the chance of those no likelier than yours. The G-test, the likelihood ratio, is an alternative to Pearson's χ² with the same df, and gives nearly the same answer in a large sample. A significant result says only that an association exists: the adjusted residuals show which cells drive it, and Cramér's V, or a 2 × 2 table's odds ratio and relative risk, how strong it is.

Questions

Frequently asked

When do you use a chi-square test?

When your data are counts in categories: how many people, plants or cells fall in each group. Use the test of independence to ask whether two categorical variables are associated, such as smoking and lung cancer, or treatment and recovery, and the goodness-of-fit test to ask whether counts match an expected ratio, such as Mendel's 3:1. For measurements, such as weight or blood pressure, use a t-test or ANOVA instead.

How do you calculate degrees of freedom for chi-square?

For a contingency table, (rows − 1) × (columns − 1): 1 for a 2 × 2 table, 2 for a 2 × 3 and 4 for a 3 × 3. For a goodness-of-fit test, the number of categories minus 1: 3 for the four classes of a 9:3:3:1 dihybrid cross. Take off one more for each parameter estimated from the same counts, such as the allele frequency in a Hardy-Weinberg test.

What is the critical value of chi-square at 0.05?

3.841 for 1 degree of freedom, 5.991 for 2, 7.815 for 3, 9.488 for 4 and 11.070 for 5. If your χ² is at least the critical value for its df, P is below 0.05 and the result is significant at that level. The full chi-square table, for every df to 30 and by tens to 100, is at the foot of this page.

What is the difference between the chi-square tests of independence, homogeneity and goodness of fit?

Independence and homogeneity are worked out the same way, on a table of counts, and differ in the design. A test of independence takes one sample and classifies each individual two ways, such as hair and eye colour. A test of homogeneity takes separate samples, such as a vaccine and a placebo group, and asks whether they split across the outcomes in the same proportions. Goodness of fit has one row of counts and asks whether it matches a ratio set in advance.

When should you use Fisher's exact test instead of chi-square?

When the expected counts are small: below 5 in any cell of a 2 × 2 table, or below 5 in more than a fifth of a bigger table's cells, or below 1 anywhere. Chi-square's P is an approximation that fails with small counts, while Fisher's exact test works P out exactly from every table with the same row and column totals. This calculator runs both, and points to Fisher's when the counts are small.

What are the assumptions of the chi-square test?

The data are counts, not percentages or measurements; each individual is counted once, in one cell, so the observations are independent (counts before and after on the same people need McNemar's test instead); the categories don't overlap; and the expected counts are large enough, at least 5 in most cells and none below 1.

Critical values

Chi-square table

Each value is the χ² that leaves the area at the top of its column in the right-hand tail. Find the row for your degrees of freedom: if your χ² is at least the value under 0.05, then P < 0.05. The first five columns, for the left tail, test whether counts fit a ratio too well.

Critical values of the chi-square distribution: rows are degrees of freedom, columns the area to the right
df0.9950.990.9750.950.90.10.050.0250.010.0050.001
10.00003930.0001570.0009820.003930.01582.7063.8415.0246.6357.87910.828
20.010.02010.05060.1030.2114.6055.9917.3789.21010.59713.816
30.07170.1150.2160.3520.5846.2517.8159.34811.34512.83816.266
40.2070.2970.4840.7111.0647.7799.48811.14313.27714.86018.467
50.4120.5540.8311.1451.6109.23611.07012.83315.08616.75020.515
60.6760.8721.2371.6352.20410.64512.59214.44916.81218.54822.458
70.9891.2391.6902.1672.83312.01714.06716.01318.47520.27824.322
81.3441.6462.1802.7333.49013.36215.50717.53520.09021.95526.124
91.7352.0882.7003.3254.16814.68416.91919.02321.66623.58927.877
102.1562.5583.2473.9404.86515.98718.30720.48323.20925.18829.588
112.6033.0533.8164.5755.57817.27519.67521.92024.72526.75731.264
123.0743.5714.4045.2266.30418.54921.02623.33726.21728.30032.909
133.5654.1075.0095.8927.04219.81222.36224.73627.68829.81934.528
144.0754.6605.6296.5717.79021.06423.68526.11929.14131.31936.123
154.6015.2296.2627.2618.54722.30724.99627.48830.57832.80137.697
165.1425.8126.9087.9629.31223.54226.29628.84532.00034.26739.252
175.6976.4087.5648.67210.08524.76927.58730.19133.40935.71840.790
186.2657.0158.2319.39010.86525.98928.86931.52634.80537.15642.312
196.8447.6338.90710.11711.65127.20430.14432.85236.19138.58243.820
207.4348.2609.59110.85112.44328.41231.41034.17037.56639.99745.315
218.0348.89710.28311.59113.24029.61532.67135.47938.93241.40146.797
228.6439.54210.98212.33814.04130.81333.92436.78140.28942.79648.268
239.26010.19611.68913.09114.84832.00735.17238.07641.63844.18149.728
249.88610.85612.40113.84815.65933.19636.41539.36442.98045.55951.179
2510.52011.52413.12014.61116.47334.38237.65240.64644.31446.92852.620
2611.16012.19813.84415.37917.29235.56338.88541.92345.64248.29054.052
2711.80812.87914.57316.15118.11436.74140.11343.19546.96349.64555.476
2812.46113.56515.30816.92818.93937.91641.33744.46148.27850.99356.892
2913.12114.25616.04717.70819.76839.08742.55745.72249.58852.33658.301
3013.78714.95316.79118.49320.59940.25643.77346.97950.89253.67259.703
4020.70722.16424.43326.50929.05151.80555.75859.34263.69166.76673.402
5027.99129.70732.35734.76437.68963.16767.50571.42076.15479.49086.661
6035.53437.48540.48243.18846.45974.39779.08283.29888.37991.95299.607
7043.27545.44248.75851.73955.32985.52790.53195.023100.425104.215112.317
8051.17253.54057.15360.39164.27896.578101.879106.629112.329116.321124.839
9059.19661.75465.64769.12673.291107.565113.145118.136124.116128.299137.208
10067.32870.06574.22277.92982.358118.498124.342129.561135.807140.169149.449

Worked out to full precision and rounded to 3 decimals, or 3 significant figures below 1. For another df or α, use the P value mode above.