What a chi-square test does and when you need it

A chi-square test measures whether the difference between what you observed and what you expected to see by random chance is real or just noise. You use it when you have counts of things in categories — like how many people in a survey chose option A versus option B, or how many defective parts came from Machine 1 versus Machine 2 — and you want to know whether the pattern is statistically meaningful.

The test produces a single number, called the chi-square statistic, that you compare to a threshold. If your number is large enough, the pattern is unlikely to be random. If it is small, the difference could easily have happened by chance. The test works for any number of categories and any sample size, which is why it appears in quality control, survey analysis, genetics, and social science.

You do not need special software to run a chi-square test. A calculator and a spreadsheet, or even pencil and paper, will work. The math is straightforward: subtract expected from observed, square the result, divide by expected, and add them all up.

Key Takeaways

  • The chi-square statistic is calculated by taking the difference between observed and expected counts in each category, squaring it, dividing by the expected count, and summing across all categories.
  • Expected counts are what you would predict if there were no real difference — usually an even split, or a split matching some known proportion.
  • You can calculate chi-square in a spreadsheet using basic formulas, or by hand if you have fewer than five or six categories.
  • After you calculate the statistic, you compare it to a chi-square table using the degrees of freedom (number of categories minus one) to decide whether the result is statistically significant.
  • The test assumes each observation is independent and that expected counts in each category are at least 5; if they are not, the result may be unreliable.

Setting up observed and expected counts

Before you calculate anything, you need two numbers for each category: the observed count (what actually happened) and the expected count (what you would predict if there were no real pattern).

Observed counts come from your data. If you surveyed 100 people and 60 chose red and 40 chose blue, those are your observed counts. Expected counts depend on your hypothesis. If you expect an even split, you would expect 50 red and 50 blue. If you expect red to be chosen twice as often as blue, you would expect 67 red and 33 blue. The expected counts must add up to the same total as the observed counts.

Write these in two columns or rows so you can see them side by side. For example:

CategoryObservedExpected
Red6050
Blue4050

The four-step calculation for each category

For each category, follow the same four steps. The result for each category is called the chi-square component.

Step 1: Subtract expected from observed. In the red example, 60 − 50 = 10. In the blue example, 40 − 50 = −10. The sign (positive or negative) does not matter for the next step.

Step 2: Square the result. For red, 10² = 100. For blue, (−10)² = 100. Squaring makes both results positive and penalizes larger differences more heavily.

Step 3: Divide by the expected count. For red, 100 ÷ 50 = 2. For blue, 100 ÷ 50 = 2. This step scales each component by how large the expected count was — a difference of 10 matters more when you expected 20 than when you expected 500.

Step 4: Add all the components together. In this example, 2 + 2 = 4. That is your chi-square statistic.

Calculating chi-square in a spreadsheet

If you have more than a few categories, a spreadsheet saves time and reduces arithmetic errors. Open a blank spreadsheet and set up four columns: Category, Observed, Expected, and Component.

In the Component column, enter the formula =(B2-C2)^2/C2 (assuming Observed is in column B and Expected is in column C, and you are in row 2). Copy this formula down for every category. Then, in a cell below, use =SUM(D2:D5) (or however many rows you have) to add all the components. That sum is your chi-square statistic.

Most spreadsheets also have a built-in chi-square function. In Excel or Google Sheets, you can use =CHISQ.TEST(observed_range, expected_range), which returns the p-value directly — the probability that you would see a difference this large by random chance. If the p-value is less than 0.05, the result is usually considered statistically significant.

Understanding degrees of freedom and the chi-square table

After you calculate the chi-square statistic, you need to know whether it is large enough to be meaningful. That depends on how many categories you have. The number of categories minus one is called the degrees of freedom.

If you have 2 categories (like red and blue), degrees of freedom = 1. If you have 5 categories, degrees of freedom = 4. A chi-square table lists critical values for each degree of freedom. You find your degrees of freedom in the left column, then look across to the column labeled 0.05 (or 0.01 for stricter standards). If your calculated chi-square statistic is larger than that critical value, your result is statistically significant at that level.

For the red-and-blue example with chi-square = 4 and degrees of freedom = 1, the critical value at the 0.05 level is 3.84. Since 4 is larger than 3.84, the difference between red and blue is statistically significant — it is unlikely to have happened by random chance alone.

Chi-square tables are available free online and in most statistics textbooks. You can also use the spreadsheet function =CHISQ.INV.RT(0.05, degrees_of_freedom) to find the critical value directly.

Common mistakes and when the test is not reliable

The chi-square test assumes that each observation is independent — that one person's choice does not influence another's, and that you counted each item only once. If you surveyed the same person twice, or if your categories overlap, the test will give a misleading result.

The test also becomes unreliable when expected counts are very small. If any expected count is below 5, the p-value may not be trustworthy. If you have a category with an expected count of 2 or 3, consider combining it with a nearby category, or collecting more data.

Another common error is confusing observed and expected. Double-check that you have them in the right columns before you calculate. A third mistake is forgetting to square the difference in Step 2 — if you skip that step, you will get a number that is too small and may incorrectly conclude that the pattern is not significant.

When to use chi-square versus other tests

Chi-square is for categorical data — counts in distinct groups. If your data is continuous (like heights or temperatures), use a different test, such as a t-test or ANOVA. If you have only two categories and small sample sizes, the Fisher exact test is more reliable than chi-square.

If you are comparing observed counts to a known theoretical distribution (like whether a die is fair), chi-square works well. If you are comparing two groups on a single yes-or-no question, chi-square also applies. But if you have many categories with very few observations in each, or if your categories are not truly independent, consider whether chi-square is the right tool or whether you need informed guidance.

Frequently Asked Questions

Can I use chi-square if my expected counts are unequal?

Yes. Expected counts do not have to be equal — they just have to reflect your hypothesis. If you expect a 2:1 ratio instead of a 1:1 ratio, use those proportions to calculate expected counts. The test works the same way.

What does a p-value of 0.05 mean in a chi-square test?

A p-value of 0.05 means there is a 5 percent chance you would see a difference this large or larger if there were actually no real pattern — if the null hypothesis were true. By convention, 0.05 is often used as the threshold for "statistically significant," but the choice of threshold depends on your field and your question.

Do I need to round the chi-square statistic?

You can round to two or three decimal places for readability, but keep more decimal places during calculation if you are doing it by hand. Rounding too early can introduce small errors. Spreadsheets handle this automatically.

What if I have only two categories and one observed count is zero?

Chi-square can still work, but the result may be less reliable if your sample is small. If one category has zero observations and a small expected count, consider whether you have enough data to draw a conclusion, or whether the categories should be combined or redefined.

Can chi-square tell me which category is driving the difference?

Chi-square tells you whether an overall pattern exists, but not which specific category is responsible. To find out, look at which category has the largest component value (the result of Step 4 for that category alone). That category is contributing most to the overall chi-square statistic.