MerQuR+Statistics Glossary

Statistics Glossary → Crosstab

Crosstabanalysis

A table showing, in counts and percentages, how two categorical variables are distributed together, summarising the association between them with tests and measures of association.

Concept

A crosstab (contingency table) shows how many observations fall into each combination of the levels of two categorical variables. It is the most basic summary of categorical data: row and column percentages show directly how the distribution of one variable changes across the levels of the other.

The next question is whether the differences seen can be explained by chance. For this, Pearson's chi-square (χ² = Σ (O − E)² / E), the likelihood-ratio G², or in small samples Fisher's test is used; the expected counts are E = row total × column total / n. When the levels are ordered, the linear-by-linear test looks for a steady upward or downward trend. The strength of the association is expressed with measures such as Cramér's V, gamma, Kendall's tau, Somers' d or lambda; in 2x2 tables its direction and size are expressed with the odds ratio, relative risk and risk difference.

When interpreting, pay attention to the direction in which percentages are computed: if the explanatory variable is in the rows, compare row percentages. The tests require independent observations and expected counts that are not too small. A crosstab shows an association, not causation; a third variable may be creating or hiding the association, so layered tables should be examined where needed.

When to use it

  • To describe the joint distribution of two categorical variables and see in one place whether they are associated.
  • With small expected counts use Fisher's exact test or a Monte Carlo p; when two measurements on the same people are compared, use the McNemar test.
  • To control a third variable as a confounder use the Cochran-Mantel-Haenszel test; to model the association structure of three or more variables, a log-linear model; to summarise the association in a visual map, correspondence analysis.

An everyday example

A supermarket chain records the age group (18-34, 35-54, 55 and over) and payment method (cash, card, mobile) of 900 customers. The crosstab might show that 45% of the youngest group pay by mobile while only 8% of those over 55 do, and that cash use rises with age. The chi-square test says whether these differences are too large to be explained by chance, and Cramér's V how strong the association is. Since payment method has no natural order, nominal measures such as Cramér's V and lambda are used here rather than ordinal ones such as gamma.

Parameters in MerQur

Column 1 and Column 2 (row and column variables)

Concept: The two categorical variables forming the rows and columns of the table. The usual rule is to put the explanatory variable in the rows and the outcome in the columns, so that row percentages read as 'the distribution of the outcome in each group'. Chi-square tests are unaffected by this choice; directional measures and the relative risk are not.

Everyday example: In the supermarket example, age group goes in the rows and payment method in the columns. Row percentages then answer 'what share of each age group pays by mobile?'; the other way round, they would answer 'what share of mobile payers are young?'

What it does: These are the row and column variables of the crosstab; the explanatory variable is placed in the rows and the outcome variable in the columns.

Effect on the result: Counts, expected values, residuals, Pearson/G²/linear-by-linear/Fisher and Cramér V are not affected by swapping roles. Directional measures (Somers d, lambda, uncertainty coefficient), the direction of relative risk and the column-proportion tests change when roles are swapped.

On the validation data: For female x ses and ses x female, χ² = 4.5765 and V = 0.1513 are the same. In FatComp, swapping roles changes RR/RD and 22 direction-dependent outputs; symmetric statistics stay the same.

Compare column proportions (z test, Bonferroni)

Concept: The overall test only says 'there is a difference somewhere'; this option compares the proportions of the column groups pair by pair within each row category. Because several comparisons are made, the Bonferroni correction limits the risk of false alarms; columns that do not differ share a letter.

Everyday example: In the supermarket example, the cash proportions of the three age groups are compared: if the over-55s differ from both other groups while 18-34 and 35-54 do not differ from each other, those two groups share a letter.

What it does: Compares column percentages within each row category with pairwise pooled z tests; p-values are Bonferroni-corrected by the number of pairs within the row and a letter display is given.

Effect on the result: An additional table (z, p, p_Bonf, letters); other outputs do not change.

On the validation data: In 129 pairwise comparisons z, p and p_Bonf (p x number of pairs) are computed with the two-proportion z test without continuity correction; the decisions are shown with column letters (46 significant, 83 not). hsb2 ses = 3, prog 1-2: p_Bonf = 0.0534.

→ Column proportions z-test

Confidence Level (%90, %95, %99, Other (custom))

Concept: Sets the width of the confidence intervals for the odds ratio, relative risk, risk difference, proportions and measures of association, and the decision threshold (α). The estimates themselves do not change.

Everyday example: If 45% of young customers pay by mobile, the 95% confidence interval might say the true share lies between about 40% and 50%; at 99% the interval widens.

What it does: Sets the confidence level of the intervals for OR, Wilson, RR/RD and association measures; alpha = 1 - confidence level is also the H₀ decision and Bonferroni threshold.

Effect on the result: Interval limits and decision rows change; estimates do not change.

On the validation data: FatComp OR interval 90% [1.5827, 43.0037], 95% [1.1535, 59.0029], 99% [0.6217, 109.4843]; the gamma upper limit at 90% is clipped to 1.0.

→ Confidence level and confidence interval

Layer (control)

Concept: Builds a separate crosstab for each level of a third variable. It shows whether the association between the two variables stays the same across the levels of this third variable, or even reverses direction (Simpson's paradox).

Everyday example: If the supermarket selects branch type (city centre / rural) as the layer, it can see whether the age-payment association appears in both kinds of branch. If mobile payment hardly exists in rural branches, much of the association may come from the city branches.

What it does: Gives a separate crosstab with Pearson χ², G² p, V and, for 2x2, Fisher p at each level of a third variable; it helps to see Simpson's-paradox-type situations (a CMH analysis is needed separately for a common OR).

Effect on the result: An additional layer block; overall table outputs do not change. Measures, risk, Monte Carlo and column-proportion tests apply only to the overall table.

On the validation data: hsb2 female x ses, layer schtyp: layer 1 χ² = 4.1060, layer 2 χ² = 2.4084. For the China smoking data (8 centers) and UCBAdmissions (6 departments), χ², G², Fisher, V and counts are given separately for each layer.

→ Stratified analysis (stratum variable)

Additional outputs: Association measures

Concept: Coefficients that sum up the strength of the association in one number. When both variables are ordered, gamma, Kendall's tau and Somers' d also show its direction (between −1 and 1); for nominal variables, lambda and the uncertainty coefficient show by what proportion knowing one variable reduces the error in predicting the other.

Everyday example: Payment method is nominal, so the supermarket looks at lambda: lambda = 0.15 means that knowing a customer's age group cuts errors in predicting their payment method by 15%. Had age group been crossed with an ordered variable such as monthly shopping frequency (low, medium, high), gamma could also be interpreted.

What it does: Gamma, Kendall tau-b/tau-c, Somers d, lambda, uncertainty coefficient and contingency coefficient C; interval with ASE1, T and p with ASE0. Ordinal measures are used for ordinal variables; lambda and the uncertainty coefficient for nominal ones.

Effect on the result: An additional table; other outputs do not change. Ordinal measures depend on the level order.

On the validation data: Pain gamma 0.5313, ASE 0.0935, 95% CI [0.3480, 0.7146], T = 5.151. JobSat with natural order: gamma 0.2211, ASE 0.1172; string levels in code point order give 0.1303.

→ Ordinal association measures (gamma, tau-b, tau-c, Somers' d)

Additional outputs: Risk (2×2)

Concept: In 2x2 tables, compares two groups' risk of experiencing an outcome: the relative risk gives how many times one group's risk is the other's, and the risk difference gives the gap in percentage points. These are meaningful in designs where groups are followed and the outcome recorded (cohort); in a case-control design the odds ratio is interpreted.

Everyday example: Had the supermarket built a 2x2 table of 'loyalty card yes / no' and 'came back within the month yes / no', with 80% of card holders returning against 50% of others, the relative risk would be 1.6 and the risk difference 30 percentage points.

What it does: In a 2x2 table, gives the cohort relative risk for both column levels (Katz log interval) and the risk difference (Wald interval, clipped to [-1, 1]). It is intended for cohort designs; in case-control studies the OR is interpreted.

Effect on the result: An additional block in 2x2; in RxC tables the risk measures are not computed and the note "Risk statistics are defined only for a 2×2 table" is written.

On the validation data: FatComp RR(0) = 2.8125 [1.1100, 7.1261], RR(1) = 0.3409 [0.0988, 1.1762], RD = 0.4833 [0.1090, 0.8577]. Aspirin RR 1.8178 [1.4330, 2.3059].

→ Relative risk (RR) and risk difference (RD)

Additional outputs: Monte Carlo p

Concept: When expected counts are small, computes the p value by generating thousands of random tables with the same row and column totals and counting how many are as extreme as the observed one, instead of using the chi-square approximation. It is also used for large tables where exact calculation takes too long.

Everyday example: If the supermarket builds the same table at a small new branch with only 40 customers, some cells have expected counts below 5. The Monte Carlo p gives a more reliable decision than the chi-square approximation in such a small sample.

What it does: In tables with low expected frequencies, computes a Monte Carlo p-value for Pearson χ² and Fisher from 10 000 random tables with fixed margins (seed 42); used when the exact p cannot be computed.

Effect on the result: An additional row; the asymptotic p and the decision do not change.

On the validation data: Eyes x Hair (n = 762): asymptotic p = 0.0073, Monte Carlo Pearson χ² p = 0.0075, Fisher p = 0.0026 (B = 10000, seed 42). The exact p is 0.4857 in the tea data (n = 8) and 0.0393 in FatComp; the Monte Carlo p approaches these values within Monte Carlo error.

→ Monte Carlo p-value

Weight (frequency) column

Concept: When the data are a summary table with one row per cell rather than raw data with one row per person, this column gives how many observations each cell holds. If it is not chosen, each row counts as a single observation, which makes every result wrong for a summary table.

Everyday example: Instead of entering 900 customers one by one, the supermarket can write a 9-row summary table such as '18-34, mobile, 135'. If the count column is not chosen as the weight, the calculation proceeds as if there were only 9 customers.

What it does: If the data are in summary (count) table form, this is the column that gives the frequency of each row. If empty, each row is one observation.

Effect on the result: All count-based outputs. If a count table is given without a weight, each row is counted as 1 observation; this is a user choice and the header shows the observation count (e.g. 4).

On the validation data: FatComp (weight = Count): χ² = 4.9597, OR = 8.25 [1.1535, 59.0029]; without a weight the same data give χ² = 0, OR = 1.0. Color counts + Region layer match the raw-row run in all 162 outputs.

→ Frequency weight (summary-table data)

Column 1 / Column 2 level order

Concept: Because ordinal measures of association and the linear-by-linear test depend on the order of the categories, this lets the low-to-high order of the levels be stated explicitly. With text labels, alphabetical order often does not match the meaningful order.

Everyday example: If shopping frequency is sorted alphabetically as 'high', 'low', 'medium', the order is scrambled and gamma is computed wrongly. Giving the order as 'low, medium, high' lets the ordinal measures capture the right trend.

What it does: Gives the low-to-high order of levels, comma-separated, for ordinal measures (γ, τ, Somers d) and the linear-by-linear test; if empty, numeric levels are ordered numerically and string levels in code point order.

Effect on the result: Only order-dependent measures and the linear-by-linear test change.

On the validation data: In JobSat, with the natural order gamma is 0.2211009. Without an order it is 0.1303 with code point ordering.

Example on real data

Data: SAS/STAT PROC FREQ example (Color: eye and hair colour), SAS/STAT 15.1 Example 44.6 (FREQEX2)

Variables: row Eyes (eye colour), column Hair (hair colour), weight (frequency) column Count

  • Question: Are eye colour (blue, brown, green) and hair colour (black, dark, fair, medium, red) independent?
  • Assumption: No cell has an expected frequency below 5 (smallest 5.75); the Pearson χ² approximation is reliable. Because the data are a count table, the Count column is used as the weight (N = 762).
  • Result: χ²(8, N = 762) = 20.92, p = .007; likelihood ratio G²(8) = 25.97, p = .001. The independence hypothesis is rejected: eye and hair colour are associated.
  • Effect size: Cramér V = .12; the association is statistically significant but weak (a large sample detects a small association).
  • Cell level: By the adjusted residuals, brown eyes x black hair (+2.68), brown eyes x dark hair (+2.15) and green eyes x red hair (+1.97) are more frequent than expected, green eyes x black hair (-2.83) and green eyes x dark hair (-2.04) less frequent.
  • APA: χ²(8, N = 762) = 20.92, p = .007, V = .12.

Related concepts

Monte Carlo p-valueOrdinal association measures (gamma, tau-b, tau-c, Somers' d)Relative risk (RR) and risk difference (RD)Column proportions z-testG-test (likelihood-ratio chi-square, G²)Frequency weight (summary-table data)Odds ratio (OR)Cramér's V and Cohen's w

The numbers in the examples are MerQur's output on independent, publicly available validation data; see the MerQur Technical Note for details.