# barnardtest

Barnard's unconditional test.

## Syntax

• pval=barnardtest(N)example
• pval=barnardtest(N,Name,Value)example

## Description

This function computes the Barnard's unconditional test.

The Barnard test is a powerful alternative of Fisher's exact test for 2x2 contingency tables.

 pval =barnardtest(N) Barnard test with all the default options.

 pval =barnardtest(N, Name, Value) Resolution option.

## Examples

expand all

### Barnard test with all the default options.

N= 2x2 Input contingency table

    N=[8,14; 1,3];
pval=barnardtest(N);
disp(['The p-value of the test is: ' num2str(pval)])

The p-value of the test is: 0.41598


### Resolution option.

N= 2x2 Input contingency table

    N=[20,14; 10,13];
% pvalue with the default resolution (0.001)
pval001=barnardtest(N);
% p value with a resolution of 0.01
pval01=barnardtest(N,'resolution',0.01);
disp(['The p-value with a resolution 0.01 is: ' num2str(pval01)])
disp(['The p-value a resolution 0.001 is: ' num2str(pval001)])

The p-value with a resolution 0.01 is: 0.15259
The p-value a resolution 0.001 is: 0.1528


### An example when the input is a MATLAB table.

    rownam={'OutcomeI', 'OutcomeII'};
colnam={'TreatmentI' 'TreatmentII'};
N=[40,14;10,30];
if verLessThan('matlab','8.2.0') ==0
Ntable=array2table(N,'RowNames',rownam,'VariableNames',colnam);
else
Ntable=N;
end
pval=barnardtest(Ntable);


### An example when the input is a datamatrix.

    N=[40,14;10,30];
% Recreate the orginal data matrix
X=crosstab2datamatrix(N);
% barnardtest when input is a datamatrix
pval=barnardtest(X,'datamatrix',true);


### Comparison with other existing implementations.

Using the example below

    N=[16 40; 1 2];
pval=barnardtest(N);
% our p-value is 0.456054
% This value coincides with the R implementation (package barnard)
% based on a C routine. On the other hand the vectorized implementation
% of Barnard test http://www.mathworks.com/matlabcentral/fileexchange/25760
% called using mybarnard(N,1000) gives a p-value of   0.456051


## Input Arguments

### N — Contingency table (default) or n-by-2 input dataset. Matrix or Table.

Matrix or table which contains the input contingency table (say of size I-by-J) or the original data matrix.

In this last case N=crosstab(N(:,1),N(:,2)). As default procedure assumes that the input is a contingency table.

Data Types: single| double

### Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as  Name1,Value1,...,NameN,ValueN.

Example:  'resolution',0.01 , 'datamatrix',true 

### resolution —The resolution of the search space for the nuisance parameter.scalar.

Small number which defines the resolution. See the More About section for more details.

The default value of the resolution is 0.001.

Example:  'resolution',0.01 

Data Types: single | double

### datamatrix —Data matrix or contingency table.boolean.

If datamatrix is true the first input argument N is forced to be interpreted as a data matrix, else if the input argument is false N is treated as a contingency table. The default value of datamatrix is false, that is the procedure automatically considers N as a contingency table.

Example:  'datamatrix',true 

Data Types: logical

## Output Arguments

### pval —p-value of the test. Scalar

pval is the p-value, i.e. the probability of observing the given result, or one more extreme, by chance if the null hypothesis of independence between rows and columns is true. Small values of pval cast doubt on the validity of the null hypothesis.

For a 2x2 contingency table, such as $N=[n_{11},n_{12};n_{21},n_{22}]$, the normalized difference in proportions between the two categories, given in each column, can be written with pooled variance (Score statistic) as $T(X)=\frac{\hat{p}_2-\hat{p}_1}{\sqrt{\hat{p}(1-\hat{p})(\frac{1}{c_1}+\frac{1}{c_2})}},$

where $\hat{p}=(n_{11}+n_{21})/n$ , $\hat{p}_2=n_{12}/(n_{12}+n_{22})$, $\hat{p}_1=n_{11}/(n_{11}+n_{21})$, $c_1=n_{11}+n_{21}$ and $c_2=n_{12}+n_{22}$.

The probability of observing $N$ (the input contingency table) is

$P(N)=\frac{c_1!c_2!}{n_{11}!n_{12}!n_{21}!n_{22}!} p^{n_{11}+n_{12}}(1-p)^{n_{21}+n_{22}},$

where $p$ is the unknown nuisance parameter.

Barnard's test considers all tables with category sizes $c_1$ and $c_2$ for a given $p$. The p-value is the sum of probabilities of the tables having a score in the rejection region, e.g. having significantly large difference in proportions for a two-sided test. The p-value of the test is the maximum p-value calculated over all $p$ between 0 and 1. The input resolution parameter controls the resolution to search for.

## References

Barnard, G.A. (1945), A new test for 2x2 tables. Nature, pp. 156-177.

Barnard, G.A. (1947), Significance tests for 2x2 tables, Biometrika, 34, pp. 123-138.

Suissa, S. and Shuster, J. J. (1985), Exact Unconditional Sample Sizes for the 2x2 Binomial Trial, Journal of the Royal Statistical Society, Ser. A, 148, pp. 317-327.

Lin C.Y., Yang M.C. (2009), Improved p-value tests for comparing two independent binomial proportions. Communications in Statistics-Simulation and Computation, 38(1):78-91.

## Acknowledgements

This file was inspired by Trujillo-Ortiz, A., R. Hernandez-Walls, A. Castro-Perez, L.

Rodriguez-Cardozo N.A. Ramos-Delgado and R. Garcia-Sanchez. (2004).

Barnardextest:Barnard's Exact Probability Test.

and by Cardillo G. (2009) MyBarnard: a very compact routine for Barnard's exact test on 2x2 matrix.

A comparison with the the current implementation is given in the last example.

The FSDA team wishes to thank Dr. Ivano Azzini for the current implementation of the Barnard test.