Statistics guide

Chi-Square Test of Independence: Expected Counts and Worked Example

Work through a chi-square test of independence from observed and expected counts to the statistic, degrees of freedom, p-value, and conclusion.

Colored counters organized in a two-row, three-column table beside a distribution curve

A chi-square test of independence asks whether two categorical variables are associated in a population. Put the observed counts into a two-way table, calculate the counts expected if the variables were independent, then add the contributions (O−E)2/E(O-E)^2/E. A large total relative to its chi-square distribution gives evidence against independence.

This guide works through every cell of a 2×32\times3 table. It is about the test of independence, not chi-square goodness of fit or a test about a population variance. OpenStax and Penn State's STAT 500 lesson provide further course-level explanations.

The procedure at a glance

  1. State H0H_0: the two categorical variables are independent. State HaH_a: they are associated.

  2. Check that the data come from a suitable random/representative process, observations are independent, and expected cell counts are large enough for the approximation used by your course. A common conservative introductory rule is at least 5 in every expected cell.

  3. For each cell, calculate its expected count from its row total, column total and grand total:

    E=(row total)(column total)grand total.E=\frac{(\text{row total})(\text{column total})}{\text{grand total}}.
  4. Compute χ2=∑(O−E)2/E\chi^2=\sum (O-E)^2/E and df=(r−1)(c−1)df=(r-1)(c-1).

  5. Find the right-tail p-value from the chi-square distribution with those degrees of freedom, compare it with the prespecified α\alpha, and write a conclusion about association in context.

Use counts, not percentages, in the observed table. The expected counts come from the row and column margins under the independence assumption; they are not simply the average of all cells.

Worked example 1: a complete 2×3 test

Question: In a hypothetical random sample of 120120 students, is study format (solo or group) associated with a three-level preference response (A, B or C)? Suppose the observed table is:

Study format Response A Response B Response C Row total
Solo 30 20 10 60
Group 20 30 10 60
Column total 50 50 20 120

These numbers are an illustrative example, not SolveTap user research. The hypotheses are H0H_0: study format and response category are independent in the population; HaH_a: they are associated. Set α=0.05\alpha=0.05 before calculation.

Calculate every expected count

For the solo/A cell, E=(60)(50)/120=25E=(60)(50)/120=25. The group/A expected count is also 2525. Response B has the same column total, so its two expected counts are 2525. Response C has expected count (60)(20)/120=10(60)(20)/120=10 in each row.

Study format Expected A Expected B Expected C
Solo 25 25 10
Group 25 25 10

Each expected count is at least 55. The expected counts sum to 120120, and each expected row/column total matches its observed margin. Those are useful arithmetic checks. In real data, also verify the sampling and independence assumptions; large expected counts do not fix biased sampling.

Calculate the chi-square statistic

Cell OO EE (O−E)2/E(O-E)^2/E
Solo / A 30 25 1
Solo / B 20 25 1
Solo / C 10 10 0
Group / A 20 25 1
Group / B 30 25 1
Group / C 10 10 0

Add the six contributions: χ2=4\chi^2=4. There are r=2r=2 rows and c=3c=3 response columns, so df=(2−1)(3−1)=2df=(2-1)(3-1)=2. For a chi-square distribution with 22 degrees of freedom, the right-tail area beyond 44 is about p=0.1353p=0.1353.

Because 0.1353>0.050.1353>0.05, fail to reject H0H_0. This sample does not provide sufficient evidence at the 5%5\% level of an association between study format and response category. It does not prove that the variables are independent in every population.

Worked example 2: check expected counts in a 2×2 table

Question: A different illustrative sample records 4040 yes and 1010 no responses in group A, and 2525 yes and 2525 no responses in group B. Are the counts large enough for the usual chi-square approximation?

Each group totals 5050; the yes column totals 6565, the no column totals 3535, and the grand total is 100100. The expected counts for each row are (50)(65)/100=32.5(50)(65)/100=32.5 yes and (50)(35)/100=17.5(50)(35)/100=17.5 no. All four expected counts exceed 55, so they pass that introductory approximation check. The study still needs appropriate sampling and independent observations.

If you continue the test, df=(2−1)(2−1)=1df=(2-1)(2-1)=1. Do not confuse the 2×22\times2 table's four cells with four degrees of freedom.

Worked example 3: a table you should not test this way

Question: An illustrative 2×22\times2 table has row totals of 1010 and 1010, column totals of 1818 and 22, and grand total 2020. Can you use the usual large-sample chi-square approximation under the conservative all-cells-at-least-five rule?

The expected counts in each row are (10)(18)/20=9(10)(18)/20=9 and (10)(2)/20=1(10)(2)/20=1. Two expected cells are only 1, so that rule fails. A 2×22\times2 analysis may use an exact method such as Fisher's exact test when appropriate, or require a different sampling/design approach. Combining response categories is justified only if the categories can meaningfully be combined, not simply to force a favorable result.

Interpret the result accurately

A statistically significant chi-square test suggests an association, but it does not reveal which cells drive it, how strong the association is, or whether one variable causes the other. Follow up with conditional percentages and, when useful, cell residuals and a measure of association. A nonsignificant result is a lack of sufficient evidence under this analysis, not proof of no relationship.

The hypothesis-testing guide explains the general null/alternative and p-value logic. The Statistics Solver can help you parse a word problem, while statistics resources collects related lessons. SolveTap's current z-score and confidence-interval calculators do not calculate chi-square tests.

Practice with answer

Two groups each have 30 students. Group A has 18 yes and 12 no; group B has 12 yes and 18 no. Find the expected counts and χ².

The yes and no columns each total 3030, and the grand total is 6060. Each expected count is (30)(30)/60=15(30)(30)/60=15. Every cell differs from expected by 33, so each contribution is 32/15=0.63^2/15=0.6. Therefore χ2=4(0.6)=2.4\chi^2=4(0.6)=2.4 with df=1df=1. The right-tail p-value is about 0.1210.121, so the test is not significant at α=0.05\alpha=0.05 under the stated assumptions.