Chi Square How To Calculate | Get The Right Number

A chi-square value is found by comparing observed counts with expected counts, then adding up (Observed − Expected)² ÷ Expected for every category.

Chi-square looks scary the first time you see the symbol. Then you work through it once, and the whole thing clicks. You’re not doing magic. You’re measuring the gap between what happened and what would be expected if nothing unusual was going on.

That makes chi-square handy for two common jobs. One is checking whether one set of category counts fits an expected pattern. The other is checking whether two categorical variables seem related in a table.

The good news is that the hand calculation follows the same rhythm every time. Write the observed counts. Find the expected counts. Subtract. Square. Divide by the expected count. Add the pieces. That’s it.

This article walks through the full process, shows the formulas in plain English, and points out the slipups that throw off the answer. By the end, you should be able to calculate a chi-square statistic on paper and know whether your result looks small, large, or worth a second check.

What Chi-Square Is Actually Measuring

A chi-square test works with counts, not averages. You use it when your data falls into categories such as yes or no, red or blue, male or female, weekday or weekend, brand A or brand B.

The test compares two things: the counts you observed and the counts you would expect under a null hypothesis. If those two sets are close, the chi-square value stays low. If the gaps are wide, the value climbs.

That means the statistic is a distance measure. A bigger number means the data sits farther from the expected pattern. A smaller number means the data sits closer to it.

Two common versions

The goodness-of-fit version uses one variable with several categories. A simple die-roll test is a classic case. If a die is fair, each face should show up about one-sixth of the time.

The test of independence uses a two-way table. You might want to know whether drink choice differs by meal time, or whether pass and fail rates differ by class section.

When chi-square fits the job

Use it when your data is made of counts in separate categories, each observation falls into one category only, and the observations are independent. It also works best when expected counts are not tiny. The Penn State STAT 500 lesson on expected counts lays out that table logic clearly.

If your data is a mean, a time, a weight, or any other continuous measurement, chi-square is not the tool you want. Same story if one person can land in more than one category.

Chi Square How To Calculate For A Simple Table

Let’s start with the hand formula. For each category or cell, use:

χ2 = Σ (O - E)2 / E

Here, O means observed count and E means expected count. The sigma sign means “add all the pieces together.”

So the workflow is:

  1. List the observed counts.
  2. Find the expected counts.
  3. Compute the difference for each category.
  4. Square each difference.
  5. Divide each squared difference by its expected count.
  6. Add all those results.

That final sum is your chi-square statistic. Next comes the p-value step, which uses degrees of freedom. We’ll get there in a minute. First, let’s do a full example by hand.

Worked example: Goodness of fit

Say you rolled a die 60 times and got these counts:

  • 1: 8
  • 2: 12
  • 3: 9
  • 4: 11
  • 5: 7
  • 6: 13

If the die is fair, each face should appear 10 times because 60 ÷ 6 = 10. So every expected count is 10.

Now work cell by cell. For face 1, the piece is (8 − 10)² ÷ 10 = 4 ÷ 10 = 0.4. For face 2, it is (12 − 10)² ÷ 10 = 0.4. Keep doing that until every face is done, then add the pieces.

You can build the full table like this.

Die Face Observed And Expected (O − E)² ÷ E
1 8 vs 10 0.40
2 12 vs 10 0.40
3 9 vs 10 0.10
4 11 vs 10 0.10
5 7 vs 10 0.90
6 13 vs 10 0.90
Total 60 vs 60 2.80

So the chi-square statistic is 2.80. That number, by itself, is not the finish line. You still need degrees of freedom and a p-value to judge whether the result is larger than you’d expect from random variation alone.

Degrees of freedom for goodness of fit

For a plain goodness-of-fit test with k categories, degrees of freedom are often k − 1. With six die faces, that gives 5. The NIST Engineering Statistics Handbook uses the same chi-square structure and also notes that expected counts should not be too small.

Once you have the statistic and the degrees of freedom, you can read a chi-square table or use software to get the p-value. A small p-value points to a poor fit between the observed pattern and the expected one.

How To Find Expected Counts In A Two-Way Table

Now let’s switch to the version used for independence. This is where many people stall, not on the formula itself, but on the expected counts. The fix is simple. Use the row total, the column total, and the grand total.

The expected count for any one cell is:

Expected = (Row Total × Column Total) ÷ Grand Total

That formula shows what each cell would look like if the two variables had no link. The CDC Field Epidemiology Manual points to chi-square as a standard way to test relationships in categorical data, which is exactly what this setup does.

Let’s use a small example. Say a café tracked hot and iced drink orders across lunch and dinner.

  • Lunch: 30 hot, 20 iced
  • Dinner: 20 hot, 30 iced

Put that into a two-way table. The row totals are 50 and 50. The column totals are 50 and 50. The grand total is 100.

So the expected count in every cell is (50 × 50) ÷ 100 = 25. That gives observed counts of 30, 20, 20, and 30, with expected counts of 25 in each cell.

Now compute each piece:

  • (30 − 25)² ÷ 25 = 1
  • (20 − 25)² ÷ 25 = 1
  • (20 − 25)² ÷ 25 = 1
  • (30 − 25)² ÷ 25 = 1

Add them up and you get a chi-square statistic of 4.

Degrees of freedom for independence

For a two-way table, use:

(Number of Rows − 1) × (Number of Columns − 1)

With a 2 × 2 table, that gives 1 degree of freedom. Then you match the statistic and degrees of freedom to a chi-square distribution to get the p-value.

Step Goodness Of Fit Independence Table
Data shape One variable, many categories Two categorical variables
Expected count rule From the claimed pattern (Row × Column) ÷ Total
Main question Does the pattern fit? Are the variables related?
Degrees of freedom k − 1 (r − 1)(c − 1)

Calculating Chi-Square Step By Step Without Missing Anything

When you’re doing this under time pressure, the cleanest move is to follow one fixed checklist. That cuts down on arithmetic mistakes and helps you catch bad setup before you waste time on the last line.

Step 1: Confirm that the data is made of counts

Chi-square needs category counts. If your table shows percentages only, convert them back to counts first if you can. If the data shows averages or measurements, stop and pick another test.

Step 2: Write the null idea in plain words

For goodness of fit, the null idea is that the observed counts match the expected pattern. For independence, the null idea is that the two variables are not related.

Step 3: Get the expected counts right

This is where most wrong answers are born. In a goodness-of-fit problem, expected counts come from the claimed proportions. In a contingency table, each expected cell uses row total × column total ÷ grand total.

Step 4: Use the full formula on every cell

Do not add observed counts and expected counts together. Do not divide by the observed count. Do not forget the square. Each term must be (O − E)² ÷ E.

Step 5: Add the terms and then find degrees of freedom

Leave the final sum as your chi-square statistic. After that, work out the degrees of freedom. That tells you which chi-square distribution applies.

Step 6: Read the p-value or use a trusted calculator

Hand calculation is great for learning. For a fast cross-check, a tool like the GraphPad chi-square calculator can confirm whether your arithmetic matches the software output.

Common Mistakes That Skew The Answer

One bad cell can distort the whole result, so it helps to know the usual traps.

Mixing up observed and expected counts

Observed counts come from your data. Expected counts come from the null idea. They are not interchangeable. If your expected values match your observed values exactly, your chi-square statistic would be zero. That almost never happens in real data.

Using percentages as if they were counts

Percentages feel tidy, but chi-square is built for counts. You can start from percentages only if you can rebuild the underlying frequency counts.

Ignoring tiny expected counts

If expected counts are too small, the approximation gets shaky. In that case, you may need to combine categories or use a different method. That warning appears in standard teaching material because it matters.

Forgetting that chi-square says nothing about direction

A chi-square value tells you that the gap between observed and expected counts is small or large. It does not tell you which category caused the gap all by itself. For that, you inspect the cell contributions or the residuals after the test.

How To Read The Result After You Calculate It

Once you have the chi-square statistic, pair it with degrees of freedom and get the p-value. If the p-value is small enough for your chosen cutoff, you reject the null hypothesis. If it is not small, you do not reject it.

That wording matters. “Do not reject” does not mean “prove the variables are unrelated” or “prove the pattern is exact.” It means the sample does not give strong enough evidence against the null idea.

In practice, the full write-up often sounds like this: “A chi-square test of independence found a relation between meal time and drink choice, χ2(1) = 4.00, p < .05.” For goodness of fit, the sentence changes a bit, but the structure stays similar.

When Hand Calculation Helps Most

Software can get the answer in seconds. Still, doing one or two problems by hand pays off. You see where expected counts come from. You catch when a table is set up wrong. You stop treating the output like a black box.

That skill matters in homework, exams, research reading, and day-to-day work with dashboards or survey tables. If a result looks odd, you can rebuild the calculation yourself and see where things drifted.

Once you know the rhythm, chi-square stops being a jumble of symbols. It turns into a repeatable count comparison with one clean formula and one clean question: are these gaps small enough to shrug off, or large enough to take seriously?

References & Sources