Chi-square test of independence: cross tabulation explained

Updated

A cross tabulation counts how often each combination of two categorical variables occurs, such as defect type by shift or rejection reason by department. The chi-square test of independence then asks whether the two variables are related: whether the mix of one variable changes from one level of the other to the next. It compares each observed count with the count expected if the variables were independent.

When to use it

  • You want to know whether the type of defect depends on the shift, the machine, the supplier or the product.
  • You are comparing the pass/fail rate of three or more groups (a 2 × k table), which extends the 2 proportions test.
  • You have survey or audit data with two categorical answers and want to see whether they go together.
  • For a single categorical variable against fixed proportions, use the chi-square goodness-of-fit test instead.

How it works

Arrange the counts in a table with r rows and c columns. Let Oᵢⱼ be the observed count in row i and column j, Rᵢ the row totals, Cⱼ the column totals and N the grand total.

  • H₀: the row and column variables are independent (every row has the same column percentages).
  • H₁: they are associated.

Under H₀, the expected count in each cell is:

Eᵢⱼ = Rᵢ · Cⱼ / N

The Pearson chi-square statistic adds up the squared differences, scaled by the expected counts:

χ² = Σ (Oᵢⱼ − Eᵢⱼ)² / Eᵢⱼ, df = (r − 1)(c − 1)

The p-value is the upper tail of the chi-square distribution with that many degrees of freedom. The likelihood-ratio statistic G² = 2 Σ Oᵢⱼ ln(Oᵢⱼ/Eᵢⱼ) is an alternative with the same df; the two are usually close.

To see which cells drive the result, look at the standardized (Pearson) residuals (Oᵢⱼ − Eᵢⱼ)/√Eᵢⱼ. Values beyond about ±2 mark cells with notably more or fewer counts than independence predicts.

Assumptions: each unit is counted once, in one cell; units are independent; and expected counts are not too small. A common rule is that all expected counts should be at least 5, or at least 1 with no more than 20% below 5. For a 2 × 2 table with small counts, use Fisher's exact test.

Example

A hospital laboratory rejects specimens that cannot be tested reliably. Over one month it rejected 196 specimens. The lab manager wants to know whether the reasons differ between the places where specimens are collected:

LocationHemolyzedClottedInsufficient volumeLabeling errorTotal
Emergency department58911785
Inpatient wards311416970
Outpatient clinic17681041
Total106293526196

Expected counts under independence, for example for the emergency department and hemolysis: 85 × 106 / 196 = 45.97.

LocationHemolyzedClottedInsufficient volumeLabeling error
Emergency department45.9712.5815.1811.28
Inpatient wards37.8610.3612.509.29
Outpatient clinic22.176.077.325.44

All expected counts are above 5, so the chi-square approximation is fine.

  • Pearson χ² = 15.55 with (3 − 1)(4 − 1) = 6 df, p = 0.016.
  • Likelihood-ratio G² = 15.01, p = 0.020.
  • The largest standardized residuals are +1.77 for hemolysis in the emergency department and +1.96 for labeling errors in the outpatient clinic.
Rejection reasons as a percent of each location's rejections204060Percent of location's rejectionsEmergency departmentInpatient wardsOutpatient clinicHemolyzedClottedInsufficient volumeLabeling errorRejection reason
Rejection reasons as a percent of each location's rejections

How to read the results

  • The p-value of 0.016 says the mix of rejection reasons is not the same everywhere. Location and reason are associated.
  • The residuals and row percents show how. In the emergency department, 68% of rejections are hemolyzed samples, against 54% overall; in the outpatient clinic, 24% are labeling errors, against 13% overall.
  • In practice, these point to two different fixes. Hemolysis in emergency care is often linked to drawing blood through IV catheters and to difficult draws; labeling errors in a clinic point to the labeling step at reception. One lab-wide campaign would miss both.
  • The test does not rank the locations by rejection rate. It only compares the mix of reasons. To compare how often each location's specimens are rejected, divide by the number of specimens each one sends.

Common mistakes

  • Counting units more than once. If one specimen can be both clotted and short, it must be counted once (for example by its main reason), or the test is invalid.
  • Running the test on percentages. The chi-square statistic needs counts. Percentages hide the sample size and give meaningless results.
  • Ignoring small expected counts. With many sparse cells, combine related categories or use an exact test.
  • Reading association as cause. The emergency department handles more difficult patients; the association with hemolysis may come from the patients, the technique or both.

Frequently asked questions

What is the difference between the chi-square test of independence and the goodness-of-fit test?

The test of independence compares two categorical variables in a two-way table, with expected counts computed from the table's own totals. The goodness-of-fit test compares one variable with proportions you specify in advance.

What do the degrees of freedom mean in a contingency table?

Once the row and column totals are fixed, only (r − 1)(c − 1) cells can be filled in freely; the rest follow from the totals. A 3 × 4 table therefore has 6 degrees of freedom.

Should I use the Pearson or the likelihood-ratio chi-square?

Either; they usually agree, as here (p = 0.016 and 0.020). Pearson's is the more common one to report. When they disagree, expected counts are often too small for either to be trusted.

What if my expected counts are below 5?

Combine categories that make sense together (such as two minor rejection reasons), collect more data, or use an exact test. For a 2 × 2 table, Fisher's exact test is the standard choice.

Try it with your own data

StatLab SPC is free, open-source statistics for engineers. It runs in your browser, works offline, and never uploads your data.

Open StatLab