Skip to content

EProportions · Guide test 067

Bhapkar test

Provides an alternative large sample test of marginal homogeneity in paired multicategory tables.

Runs in CodeSplashPython (calls R) example not runR example not run

Do it in CodeSplash

In the app, set it up in Analyze with this method:

Do it in CodeSplash Run this test on your own data, in Python or R.

Bench

Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.

Bench · Bhapkar test

150
0.50
0.08
0.050

Your data

Edit the counts
Rater 2: lowmidhighTotal
Rater 1: low
27E = 11.2
8E = 15.0
6E = 14.8
41
Rater 1: mid
11E = 16.4
35E = 22.0
14E = 21.6
60
Rater 1: high
3E = 13.4
12E = 18.0
34E = 17.6
49
Total415554150

E is the count expected if rows and columns were unrelated.

If H₀ were true

χ² = 0.78
  1. The reference: how χ² varies across samples when H₀ is true.
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .676).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

χB2=χSM21−χSM2/N\chi^2_B = \dfrac{\chi^2_{SM}}{1-\chi^2_{SM}/N}

With your numbers

χB2=0.78,ν=2\chi^2_B = 0.78,\quad \nu = 2
χSM2\chi^2_{SM}
Stuart–Maxwell statistic
NN
total count

The verdict

χ²(2) = 0.78, p = .676

p > α = .050: compatible with H₀

Asymptotically equivalent to Stuart–Maxwell but slightly more powerful; the two differ only in small samples.

Statistic
0.783
p-value
0.6761
Bhapkar test on a 3 × 3 table of 150 counts: χ²(2) = 0.78, p = .676. The p-value is above α = .050, so the data are compatible with H0.

Bhapkar test on a 3 × 3 table of 150 counts: χ²(2) = 0.78, p = .676. The p-value is above α = .050, so the data are compatible with H₀.

Question and hypotheses

H0: paired categorical margins are homogeneous.

Assumptions

Independent paired observations with matching categories and large enough discordant counts.

Formula

QB=dT(V−ddT/N)−1dQ_B=d^T(V-dd^T/N)^{-1}d

Parameters and notation

d,V are the marginal difference vector and Stuart–Maxwell covariance from entry 66; N is paired sample size; remove one redundant category.

Structure of the method

Use the same paired marginal difference vector as Stuart–Maxwell but subtract the outer-product correction dd-transpose/N from its covariance estimate. The resulting quadratic form tests marginal homogeneity with an asymptotic chi-square reference in the estimable subspace.

Estimated and nuisance parameters

Estimate paired marginal differences, total pairs, and the corrected covariance matrix. The correction uses observed departure from the null, so finite-sample statistics differ even when both tests share the same large-sample target. Category-specific transition probabilities remain nuisance quantities.

Settings and design choices

Prespecify Bhapkar rather than choosing between it and Stuart–Maxwell after seeing p-values. Record the treatment of redundant categories, numerical rank, and any zero-transition categories. The conventional k-1 degrees of freedom require a full-rank reduced covariance matrix.

Fitting and adaptation

Recompute the corrected covariance after every new sample or bootstrap replicate. A constrained null simulation should preserve the paired multinomial model and marginal-homogeneity constraints. Refitting and recalibration replace any notion of fine-tuning; the procedure does not learn a reusable prediction function.

Sensitivity and robustness

Near-singular covariance can inflate numerical instability, particularly with sparse off-diagonal transitions or very strong marginal changes. Asymptotic equivalence does not promise equal small-sample error or universal power dominance. Review the unaggregated transition table as well as the omnibus p-value.

How to use and calibrate

Use χ²k−1 asymptotically. The empirical covariance correction distinguishes Bhapkar from Stuart–Maxwell.

Example application

Compare pre/post distributions of an ordered or unordered categorical assessment.

Interpretation and pitfalls

Watch out

Asymptotic equivalence does not imply identical small sample p values. Covariance singularity needs explicit handling.

What changed in edition 2

  • New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.

Python and R

Square table of paired categorical observations. Marginal homogeneity and full symmetry are different null hypotheses.

Python · invokes R

Not run — Python launcher syntax checked; R backend runtime not verified · needs Python standard library + Rscript; R packages: DescTools

# Python launcher for the complete R implementation below.
# Requires Rscript on PATH and the listed R packages.
import shutil
import subprocess
rscript = shutil.which("Rscript")
if rscript is None:
    raise RuntimeError("Install R and make Rscript available on PATH.")
r_code = r"""
tab <- matrix(c(20,5,3,7,25,4,2,6,22),3,byrow=TRUE)
print(DescTools::BhapkarTest(tab))
"""
# No shell interpolation. A failed R execution raises an error in Python.
result = subprocess.run([rscript, "--vanilla", "-"], input=r_code,
                        text=True, capture_output=True)
print(result.stdout)
if result.stderr:
    print(result.stderr)
result.check_returncode()
R

Not run — Syntax checked; runtime not verified in the final environment · needs DescTools

tab <- matrix(c(20,5,3,7,25,4,2,6,22),3,byrow=TRUE)
print(DescTools::BhapkarTest(tab))

Sources

Statistics guide v4 (historical):
Python: Python launcher syntax checked; R backend runtime not verified · R: Syntax checked; runtime not verified in the final environment
Reference's record:
Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Python-to-R bridge; Rscript is unavailable. R code was not executed. Python: Not run in the reference's review: Python-to-R bridge; Rscript is unavailable. R code was not executed. R: Reviewed but not run: R wasn't available to the reference's authors.

From the CodeSplash statistics guide (v4), entry 067, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT067.