Academy › Topic library › Going Deeper: Chi-Square Goodness-of-Fit and Contingency Tables
Q-BOT ACADEMY · FREE VIDEO LESSON · 12 MIN
Going Deeper: Chi-Square Goodness-of-Fit and Contingency Tables
A lot of quality data isn't a measurement. It's a count of items in categories: which leg, which defect type, which shift, which supplier. You can't average a defect type. What you can do is compare the counts you saw to the counts you'd expect if nothing special was going on. That comparison is the chi-square test.
In short
Chi-square adds up observed minus expected, squared, over expected, across every cell. Goodness of fit tests one variable against claimed shares, with k minus one degrees of freedom. A contingency table tests whether two variables are linked, with expected counts from row times column over the total, and rows minus one times columns minus one degrees of freedom. The test is right-tailed, needs counts, and needs expected counts of five or more. And always read the contributions!
Check yourself
Ellen's slat supplier says its slats ship as seventy percent grade A, twenty percent grade B, and ten percent grade C. Priya grades two hundred slats: a hundred twenty eight A, forty four B, and twenty eight C. At alpha point oh five, what's chi-square, and what do you conclude? A: four point six three, fail to reject. B: four point six three, reject. C: three point seven seven, fail to reject. Or D: eighty six point five, reject.
Show the answer
The answer is A. The expected counts are a hundred forty, forty and twenty. Grade A: twelve squared over a hundred forty, one point oh three. Grade B: four squared over forty, point four. Grade C: eight squared over twenty, three point two. Chi-square is four point six three. Three grades, two degrees of freedom, critical value five point nine nine one. Four point six three is below it, so fail to reject; the p-value is about point one. B used one degree of freedom. C divided by the observed counts. And D assumed equal shares. That's chi-square goodness of fit and contingency tables. Questions? Ask Q-Bot right here in the Academy. And one for the road: why did the contingency table never win at cards? Everyone could see its expected counts!
Where this topic is taught
This lesson is part of every free Academy course whose Body of Knowledge covers it. Each course puts it in context, with exercises and checks.
Full lesson transcript
The narration from the video, by chapter. Brindlewood Outdoor Furniture and its people are a fictional company used in every Q-Bot Academy lesson.
Intro
Brindlewood, a Wednesday morning. Priya has a tally sheet from the flat plate. Eighty Harbor chairs rocked last month, and for each one she wrote down which leg was short. Dwayne shrugs. Legs are legs, it's random. But thirty one of the eighty short legs were the front right. Is that a pattern, or just luck? These aren't measurements. They're counts. And counts have their own test!
Hey there, I'm Q-Bot. Welcome to the Q-Bot Academy. Today we're going deeper: chi-square goodness of fit, and contingency tables! We'll build the chi-square statistic one piece at a time, test counts against a claim, test whether two categories are linked, check the rules, and read the result. If hypothesis testing is new to you, start with the hypothesis testing video.
Why a special test?
Why a special test? A lot of quality data isn't a measurement. It's a count of items in categories: which leg, which defect type, which shift, which supplier. You can't average a defect type. What you can do is compare the counts you saw to the counts you'd expect if nothing special was going on. That comparison is the chi-square test.
The chi-square statistic
Here's the one formula behind both tests. For every category, or cell, take the observed count minus the expected count, square it, and divide by the expected count. Add that up over every cell. That's chi-square. Each piece is a cell's contribution. If the observed counts sit close to the expected ones, chi-square is small. If they're far off, it's big.
Two details matter. First, use counts, never percentages. Chi-square grows with the number of items, so percentages pretend you had a hundred, and change the answer. Second, divide by the expected count, not the observed one. Bad: feed it percentages, or divide by what you saw. Good: real counts, divided by what you expected.
The chi-square test
To decide, compare chi-square to the chi-square distribution. Like F, this test is one-tailed on the right, because only a big chi-square is evidence against the null. The critical value comes from a chi-square table, or the spreadsheet's right-tail chi-square inverse. The p-value comes from the right-tail chi-square distribution. What changes between the two tests is the degrees of freedom, and where the expected counts come from.
Goodness of fit
First test: goodness of fit. One variable, sorted into k categories, tested against a claim about the shares. The null says the counts follow the claimed shares. The alternative says they don't. Each expected count is the total times the claimed share. The degrees of freedom are k minus one. If you estimated something from the data to get the shares, like a mean, take one more off for each thing you estimated.
Worked example 1: which leg is short?
Worked example one: Priya's short legs. If a short leg is just random, each of the four legs should be short a quarter of the time. Eighty chairs, so the expected count is twenty for every leg. Here's what she saw. Front left, fourteen. Front right, thirty one. Back left, seventeen. Back right, eighteen.
Now the contributions. Front left: fourteen minus twenty is minus six, squared is thirty six, over twenty is one point eight. Front right: eleven squared is a hundred twenty one, over twenty is six point oh five. Back left: nine over twenty, point four five. Back right: four over twenty, point two. Add them up: chi-square is eight point five.
Four legs, so three degrees of freedom. The critical value at alpha point oh five is seven point eight one five. Eight point five is bigger, and the p-value is about point oh three seven. Reject the null: the short legs don't follow the equal shares! And look where the chi-square came from. The front right leg alone gave six point oh five of the eight point five. That's where to look.
The test says a pattern this strong would be unlikely from luck alone. It doesn't say why. So Lou went and looked. On the frame welding fixture, the front right leg locator was worn, so that leg sat a hair high when it was welded. He replaced the locator. And Priya keeps tallying, so the next month's counts can show whether the fix worked!
Contingency tables
Second test: the contingency table. Now there are two categorical variables, like shift and defect type. Put one in the rows and one in the columns, and count the items in each cell. The null says the two are independent: the defect mix is the same on every shift. The alternative says they're associated. The degrees of freedom are rows minus one, times columns minus one.
Where do the expected counts come from? From the table itself. Each cell's expected count is its row total, times its column total, divided by the grand total. That's what the cell would hold if the rows and the columns had nothing to do with each other. Bad: assume every cell should be equal. Good: build each expected count from its own row and column totals.
Worked example 2: defect type by shift
Worked example two. Marcus pulls the last month of coating rejects, sixty from each shift, sorted into three types. First shift: thirty thin coat, twenty two orange peel, and eight runs. Second shift: twenty thin, eighteen orange peel, and twenty two runs. Column totals: fifty thin, forty orange peel, thirty runs. A hundred twenty in all.
Now the expected counts. Thin coat on first shift: row total sixty, times column total fifty, over a hundred twenty: twenty five. Orange peel: sixty times forty over a hundred twenty, twenty. Runs: sixty times thirty over a hundred twenty, fifteen. Both shifts have sixty rejects, so second shift's expected counts are the same: twenty five, twenty and fifteen. Every one is five or more, so the test is fair.
The contributions. Thin coat: thirty against twenty five, five squared over twenty five, one point oh, and the same on second shift. Orange peel: two squared over twenty, point two, twice. Runs: seven squared, forty nine, over fifteen, three point two seven, twice. Add all six cells: chi-square is eight point nine three. Degrees of freedom: two minus one, times three minus one, two. The critical value is five point nine nine one, and the p-value is about point oh one one. Reject the null: the defect mix depends on the shift!
Where's the difference? The runs cells gave six and a half of the eight point nine three. Second shift had twenty two runs against fifteen expected; first shift had eight. Runs come from too much powder in one spot. Marcus took the table to second shift's coating crew, and they found their gun-to-part distance set closer than first shift's. One setting on a setup sheet, and both shifts now match.
The rules
Before you trust any chi-square test, check the rules. One: counts of items, not percentages or measurements. Two: independent items, each one counted in exactly one cell. Three: expected counts of at least five in every cell. If a cell's expected count is too small, combine categories that make sense together, collect more data, or use an exact test like Fisher's for a small two by two table.
Independence or homogeneity?
One more idea. The same table math answers two questions. If you take one sample and sort each item two ways, it's a test of independence. If you take a separate sample from each group, like sixty rejects from each shift, it's a test of homogeneity: is the mix the same in every group? Same expected counts, same chi-square, same degrees of freedom. Only the wording of the question changes.
A link to the two-proportion test
And here's a nice link. Remember the rocking check by shift, nine rockers out of fifteen hundred on first shift, and twenty one on second? The two-proportion z test gave z of two point two oh. Put the same counts in a two by two table, and chi-square comes out four point eight five. That's two point two oh squared! For a two by two table, the two tests are the same test.
Reading the result
So how do you read the result? A big chi-square and a small p-value mean the counts don't fit the claim, or the two variables are linked. Then look at the contributions to see which cells drove it. A small chi-square means no evidence of a difference, not proof of a perfect fit. And an association in a table is not a cause. Shift didn't cause the runs; a setting on that shift did, and someone had to go find it.
In a spreadsheet and in software
In a spreadsheet, the chi-square test function takes the observed range and the expected range, and returns the p-value directly. The right-tail chi-square distribution and inverse do the pieces by hand. Statistical software builds the expected counts for you and shows each cell's contribution, which is the part worth reading.
Common mistakes
Common mistakes. One: running it on percentages instead of counts. Two: dividing by the observed count. Three: assuming equal expected counts in a contingency table. Four: the wrong degrees of freedom, like the number of cells minus one for a table. Five: expected counts below five. Six: stopping at reject, without reading which cells drove it. And seven: calling an association a cause.
Recap
Let's recap. Chi-square adds up observed minus expected, squared, over expected, across every cell. Goodness of fit tests one variable against claimed shares, with k minus one degrees of freedom. A contingency table tests whether two variables are linked, with expected counts from row times column over the total, and rows minus one times columns minus one degrees of freedom. The test is right-tailed, needs counts, and needs expected counts of five or more. And always read the contributions!
Check yourself
Now, check yourself. Ellen's slat supplier says its slats ship as seventy percent grade A, twenty percent grade B, and ten percent grade C. Priya grades two hundred slats: a hundred twenty eight A, forty four B, and twenty eight C. At alpha point oh five, what's chi-square, and what do you conclude? A: four point six three, fail to reject. B: four point six three, reject. C: three point seven seven, fail to reject. Or D: eighty six point five, reject. Pause the video if you want to think about it.
The answer is A. The expected counts are a hundred forty, forty and twenty. Grade A: twelve squared over a hundred forty, one point oh three. Grade B: four squared over forty, point four. Grade C: eight squared over twenty, three point two. Chi-square is four point six three. Three grades, two degrees of freedom, critical value five point nine nine one. Four point six three is below it, so fail to reject; the p-value is about point one. B used one degree of freedom. C divided by the observed counts. And D assumed equal shares.
That's chi-square goodness of fit and contingency tables. Questions? Ask Q-Bot right here in the Academy. And one for the road: why did the contingency table never win at cards? Everyone could see its expected counts!
Independent study material; not affiliated with or endorsed by ASQ. Body of Knowledge codes identify where the topic appears in each certification's published outline.