Method · Statistics Library

Contingency table (crosstab)

A contingency table (crosstab) counts the rows in every combination of two categorical variables, shows row, column or total percentages beside the counts, and tests whether the two variables are associated with a chi-square test (and, on a 2×2 table, Fisher's exact test) and Cramér's V.

Topic indexA reference reading outside the guided chapter sequence

Understand the idea

Picture a grid: one variable down the side (for example island), the other across the top (for example species). Each box holds the number of rows that have that pair of values, and the edges hold the totals.

Counts are hard to compare when the groups are different sizes, so the table also shows percentages. Row percentages split each row into shares that add to 100%: of the penguins on Biscoe, what share is each species? Column percentages do the same down each column: of the Gentoo penguins, what share lives on each island? Total percentages give each box's share of everyone.

The test asks one question: if the two variables had nothing to do with each other, how surprising would this pattern of counts be? A small p-value says a pattern at least this uneven would rarely appear if the two variables had nothing to do with each other; it is not the chance that the pattern is luck. Cramér's V, from 0 to 1, says how strong the link is.

The trap to avoid: a row percentage and a column percentage answer different questions. "74% of Biscoe penguins are Gentoo" is not "74% of Gentoo penguins live on Biscoe"; always say which total the percentage is a share of.

Practitioner explanation

KernelStats builds the r×c table of counts (R's xtabs), with margins, and the percentages chosen in the Percentages option (row by default; column, total, all or none; R's prop.table with margin 1, 2 or none). Blanks in either variable are left out and counted in a note.

The test is Pearson's chi-square test of independence via scipy.stats.chi2_contingency, with Yates' continuity correction on a 2×2 table by default (the Yates' option), matching R's chisq.test. Cramér's V is computed from the uncorrected Pearson X² (Cramér 1946; the SPSS, SAS and DescTools convention); on a 2×2 table the signed phi coefficient (ad − bc)/√(r₁r₂c₁c₂) is also reported, and Fisher's exact test (two-sided p, conditional maximum-likelihood odds ratio as in R's fisher.test). Fisher's test is not offered for larger tables: that is a limit of KernelStats, not of the method (an exact r×c test exists, as R's fisher.test, and so does a Monte Carlo p-value, chisq.test(simulate.p.value = TRUE)); the card says so and suggests combining sparse categories.

Expected counts are shown and checked against Cochran's rule (no more than 20% below 5, none below 1); when it fails the card warns and, on a 2×2, points to Fisher's p. A table with a single row or column is shown without a test. An optional third variable repeats the table and the test within each layer (a summary table of the per-layer tests comes first); the headline reports all layers combined, which can differ from the per-layer picture (Simpson's paradox). No Cochran-Mantel-Haenszel test of the association within layers is given. Display is capped at 30 rows, 20 columns and 12 layers; every statistic uses the whole table. For the cells that drive an association, the Chi-Square Test of Independence card shows Haberman's adjusted residuals.

When to use it

Consider it when

You want to see how two categorical variables combine (a crosstab, two-way table or pivot count), compare groups by their percentages, and test whether the variables are associated.

Use caution when

The two variables are the same people measured twice (paired data: use McNemar's test), or one of the variables is a measurement rather than a category (compare means instead).

Formulas and symbols

Pearson chi-square statistic for an r×c table

χ2=∑i,j(Oij−Eij)2Eij,Eij=RiCjN\chi^2 = \sum_{i,j} \dfrac{(O_{ij} - E_{ij})^2}{E_{ij}}, \quad E_{ij} = \dfrac{R_i C_j}{N}

O the observed count, E the count expected under independence from the row total R, column total C and grand total N.

Cramér's V

V=χ2N(min⁡(r,c)−1)V = \sqrt{\dfrac{\chi^2}{N(\min(r, c) - 1)}}

chi-square is the uncorrected Pearson statistic; r and c are the numbers of rows and columns.

Alternatives

  • Chi-Square Test of Independence (Chi-Square Test of Independence). You want the cell-by-cell adjusted residuals that show which combinations drive an association.
  • Frequency Table (Frequency Table). You have one variable and want its levels, counts and cumulative percentages.

Worked example

Scenario

Penguins by island and species (344 birds).

Result

χ²(4) = 299.55, p < .001, Cramér's V = 0.660; row percentages: Biscoe 26.2% Adelie and 73.8% Gentoo, Dream 45.2% Adelie and 54.8% Chinstrap, Torgersen 100% Adelie.

Interpretation

Species and island are strongly associated: knowing the island almost tells you the species. The row percentages show how: Gentoo appear only on Biscoe and Chinstrap only on Dream.

Dataset: penguins.csv

Common misconceptions

Misconception

A share of a row total and a share of a column total are interchangeable: 74% one way means 74% the other way.

Correction

They are two different percentages. A row percentage is a share of the row's total; a column percentage is a share of the column's total. In the penguins, 74% of the Biscoe penguins are Gentoo (a row percentage), but every Gentoo lives on Biscoe, so that column percentage is 100%. Always name the total a percentage is a share of.

Misconception

A significant chi-square means one variable causes the other.

Correction

The test shows an association in these counts, not a cause. Another variable can drive both, and a layer variable can even reverse the pattern within groups.

Misconception

The chi-square test was not significant, so the two variables are unrelated.

Correction

It means these counts gave no strong evidence of a link; with small groups a real link can be missed. Look at the percentages and Cramér's V.

References

  1. NIST/SEMATECH e-Handbook of Statistical Methods, §7.4.3 Contingency tables (chi-square test of independence), NIST/SEMATECH (2012).