Nonparametric Statistics & Clinical Trials
Cochran’s Q Test Formula for Matched Binary Outcomes Across k Conditions
Test whether binary pass/fail proportions differ across three or more matched treatments or raters using Cochran’s (1950) Q statistic and verify unanimous-row invariance.
Verify why unanimous all-0 and all-1 subjects carry zero contrast
When the same N subjects or diagnostic samples are evaluated as 1 (pass/detected) or 0 (fail/missed) across k >= 3 conditions, running ordinary chi-square treats repeated observations on the same subject as independent. Cochran’s (1950) Q generalizes McNemar’s paired binary test to k columns: Q = (k - 1) * (k * SUM(C_j^2) - T^2) / (k * T - SUM(L_i^2)), where C_j is column j’s total, L_i is row i’s total, and T = SUM(C_j) = SUM(L_i).
Enter 8 matched subjects across 3 diagnostic assays in B2:D9 with row sums L_i in E2:E9: column totals are C1 = 2, C2 = 4, C3 = 7 (T = 13). Notice Subject 5 (1, 1, 1 -> L_5 = 3, 3*3 - 9 = 0) and Subject 6 (0, 0, 0 -> L_6 = 0, 3*0 - 0 = 0) contribute zero to the denominator! By hand, Q = 2 * (3 * (4 + 16 + 49) - 169) / (3 * 13 - 27) = 2 * (207 - 169) / 12 = 76 / 12 = 6.3333 on df = 2 (exceeding 5.991 at alpha = 0.05).
| Cochran Component | Algebraic Expression | Sample Output |
|---|---|---|
| Column Totals C1, C2, C3 | SUM(B2:B9), SUM(C2:C9), SUM(D2:D9) | 2, 4, 7 (Total T = 13) |
| Numerator Contrast | (k - 1) * (k * SUM(C_j^2) - T^2) | 2 * (207 - 169) = 76 |
| Discordant Row Denominator | k * T - SUM(L_i^2) | 39 - 27 = 12 |
| Cochran’s Q (df = k - 1 = 2) | 76 / 12 | 6.3333 (p < 0.05) |
Cochran’s Q Statistic Formula
Guard the zero-discordance boundary (when every row is either all 0s or all 1s so k*T - SUM(L_i^2) = 0) before dividing.
=LET(k_col, COLUMNS(B2:D9), c_1, SUM(B2:B9), c_2, SUM(C2:C9), c_3, SUM(D2:D9), t_sum, SUM(E2:E9), l_sq, SUMPRODUCT(E2:E9, E2:E9), denom, k_col * t_sum - l_sq, IF(denom <= 0, "No Within-Subject Contrast", ROUND(((k_col - 1) * (k_col * (c_1^2 + c_2^2 + c_3^2) - t_sum^2)) / denom, 4)))Unanimous-Subject Invariance Check
Flip Subject 6 from unanimous failure (0, 0, 0) to unanimous success (1, 1, 1): total successes T jump from 13 to 16, yet Cochran’s Q stays at 6.3333 because unanimous subjects add zero within-block variance.
=3 * SUM(E2:E9) - SUMPRODUCT(E2:E9, E2:E9)Try the example
Use the original values or change them to test your understanding.