File Exchange

image thumbnail

MyBarnard

version 2.0.0.1 (18.6 KB) by Giuseppe Cardillo
A very compact and fast routine for Barnard's exact test on 2x2 matrix

3 Downloads

Updated 28 Jan 2019

View Version History

GitHub view license on GitHub

There are two fundamentally different exact tests for comparing the equality of two binomial probabilities – Fisher’s exact test (Fisher, 1925), and Barnard’s exact test (Barnard, 1945). Fisher’s exact test (Fisher, 1925) is the more popular of the two. In fact, Fisher was bitterly critical of Barnard’s proposal for esoteric reasons that we will not go into here. For 2 × 2 tables, Barnard’s test is more powerful than Fisher’s, as Barnard noted in his 1945 paper, much to Fisher’s chagrin (see C.R. Mehta and P. Senchaudhuri: Conditional versus unconditional exact tests for comparing two binomials. www.cytel.com/papers/twobinomials.pdf). Anyway, perhaps due to its computational difficulty the Barnard's is not widely used. This function is completely vectorized and without for...end loops, and so, the computation is very fast. In FEX there is only one other function that computes the Barnard's exact test by Antonio Trujillo-Ortiz.
Using this matrix x=[7 12; 8 3]; switching off the input error checks and display results sections in both functions; the performs are:
L=1000; times=zeros(1,L); for I=1:L, tic; mybarnard(x); times(I)=toc; end, median(times)
ans = 0.0028
L=1000; times=zeros(1,L); for I=1:L, tic; Barnardextest(7,12,8,3); times(I)=toc; end, median(times)
ans = 1.2478

So my function is about 450 times faster.

Cite As

Giuseppe Cardillo (2020). MyBarnard (https://github.com/dnafinder/barnard), GitHub. Retrieved .

Comments and Ratings (7)

Natalie Cabrera

Thank you for the updated version

Giuseppe Cardillo

Ok I uploaded a new version. Now the nuisance parameter and p-value are identical.

Natalie Cabrera

Hi Giueseppe, Yes the results are numerically different. For the contingency table you present the test should be invariant to interchanging of the two columns. If you run Barnard's test in SAS using the EXACT procedure, the p-values and nuisance parameter are identical when the two columns are switched.

Giuseppe Cardillo

Sorry but I didn't read with attention.
Anyway...
>> mybarnard([2 2 ; 5 35])
Tables Size Wald_stat Nuisance one_tailed_p_value two_tailed_p_value
______ _______ _________ ________ __________________ __________________

100 8 38 1.9551 0.040496 0.072685 0.14537

>> mybarnard([2 2 ; 35 5])
Tables Size Wald_stat Nuisance one_tailed_p_value two_tailed_p_value
______ ________ _________ ________ __________________ __________________

100 38 8 1.9551 0.9696 0.068779 0.13756

Do you think that they are really different?

Natalie Cabrera

Giueseppe - thank you for providing the function. I noticed that the following 2 contingecy tables produce different results and they shouldn't [20,20;5,35] and [20,20;35,5]. Thoughts?

Giuseppe Cardillo

I dont know. I didnt read the original papers. Maybe now computation power is higher and more accurate and precise

Egon Geerardyn

Shouldn't the P(p) plot (the one shown on the left) be symmetric around p=0.5 as noted in the 1947 paper of Barnard and in the referenced paper?

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired by: Barnardextest

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!