Observed counts
Sixty pet owners are asked two things: whether they keep a cat or a dog, and whether they live in a city or on a farm. Of the city owners, 24 keep cats and 6 keep dogs. Of the farm owners, 16 keep cats and 14 keep dogs.
These four numbers are the observed frequencies. The question behind them is whether the kind of pet is linked to the kind of home. To answer it, compare them with the frequencies that would be expected if there were no link.
The observed counts: 24 city owners and 16 farm owners keep cats, and 6 city owners and 14 farm owners keep dogs.
The totals
Add along each row and down each column. The row totals are 24 + 16 = 40 cat owners and 6 + 14 = 20 dog owners. The column totals are 24 + 6 = 30 city owners and 16 + 14 = 30 farm owners. The grand total is 60.
These totals, the margins of the table, are what the expected frequencies are worked out from.
The same table with its totals. The highlighted row total shows that 40 of the 60 owners keep cats.
If home made no difference
Overall, 40 of the 60 owners keep cats, which is . If the kind of home made no difference to the kind of pet, then of the city owners would keep cats, and so would of the farm owners. Each column would split the same way as the whole table.
There are 30 city owners, so the expected number of city cat owners is . The farm also has 30 owners, so it expects 20 cat owners too. The remaining keep dogs: in each column.
Row total times column total over grand total
The working for the city cat owners was , which is the same as 40 × 30 ÷ 60 = 20. In general, the expected frequency of a cell is its row total times its column total, divided by the grand total.
The same formula comes from the multiplication rule for independent events. The chance that an owner keeps a cat is , and the chance that an owner lives in a city is . If the two are independent, the chance of both is , and out of 60 owners that is .
The expected frequencies if the kind of pet does not depend on the kind of home. The rows still total 40 and 20, and the columns 30 and 30.
The margins stay the same
Do the same for all four cells: cats in the city 40 × 30 ÷ 60 = 20, cats on a farm 40 × 30 ÷ 60 = 20, dogs in the city 20 × 30 ÷ 60 = 10, and dogs on a farm 20 × 30 ÷ 60 = 10.
The expected table has the same totals as the observed one. Only the four cells inside have changed, evened out so that both columns split 2 to 1.
Now the two tables can be compared. The city has 24 cat owners against 20 expected, 4 more, and the farm has 16 against 20, 4 fewer. The differences are +4, −4, −4 and +4, and every row and every column of them adds up to 0, because the totals are the same in both tables.
Unequal totals
In a table of 100 people whose rows total 40 and 60 and whose columns total 30 and 70, the expected frequencies are 40 × 30 ÷ 100 = 12, 40 × 70 ÷ 100 = 28, 60 × 30 ÷ 100 = 18 and 60 × 70 ÷ 100 = 42. Check: 12 + 28 = 40 and 12 + 18 = 30.
An expected frequency need not be a whole number. With a row total of 25, a column total of 18 and a grand total of 60, it is 25 × 18 ÷ 60 = 7.5. It is an average over many samples, not a count of people, so it is not rounded.
Before a chi-squared test uses them, every expected frequency should be at least 5. Smaller ones make the test unreliable, and neighboring rows or columns are then combined.
The usual mistakes
Averaging the two totals. (40 + 30) ÷ 2 = 35 is not the expected count of city cat owners; the totals are multiplied, then divided by the grand total.
Splitting the grand total four ways. 60 ÷ 4 = 15 for every cell ignores that there are twice as many cat owners as dog owners.
Working from the observed cells instead of the totals. The expected table depends only on the row and column totals.
Rounding an expected frequency such as 7.5 to a whole number.