File Exchange

## MyFisher23

version 1.6 (2.83 KB) by

A very compact routine for Fisher's exact test on 2x3 contingency table

Updated

Fisher's exact test of 2x3 contingency tables permits calculation of precise probabilities in situation where, as a consequence of small cell frequencies, the much more rapid normal approximation and chi-square calculations are liable to be inaccurate. The Fisher's exact test involves the computations of several factorials to obtain the probability of the observed and each of the more extreme tables. Factorials growth quickly, so it's necessary use logarithms of factorials. This computations is very easy in Matlab because x!=gamma(x+1) and log(x!)=gammaln(x+1). This
function is now fully vectorized to speed up the computation.

Douglas Crawford

### Douglas Crawford (view profile)

Hi, Posted the same problem, without looking at the solution posted in April.
"
try to add this line before line 108
[I ~]=find(Tables<0); Tables(I,:)=[];
"
which solved the problem.

dlc

Giuseppe Cardillo

### Giuseppe Cardillo (view profile)

For negative values, gammaln would return INF so that table is not added to computation. Maybe in previous version of gammaln this is not allowed and it calls error. So rows in Tables matrix with negative values must be erased before to give it to gammaln.
[I J]=find(Tables<0); Tables(I,:)=[];
will do this

Giuseppe Cardillo

### Giuseppe Cardillo (view profile)

try to add this line before line 108
[I ~]=find(Tables<0); Tables(I,:)=[];

Giuseppe Cardillo

### Giuseppe Cardillo (view profile)

I checked but on my pc the version is the same and the result is

2x3 matrix Fisher's exact test: 540 tables were evaluated
-----------------------------------------------------------------
p-value (2-tails): 0.3537569193
-----------------------------------------------------------------
Mid-p correction: 0.3472754799
-----------------------------------------------------------------

Alex Anderson

### Alex Anderson (view profile)

I believe I am experiencing the same error and crashing of the script as Matthew had previously described. Certain input matrices cause the program to crash. My input was shown below. You had previously responded that your trial of his input did not crash; however, I am experiencing the same bug. Is it possible that you have a different version on your own computer?

myfisher23([19 13 15; 10 8 17])
Error using gammaln
Input must be nonnegative.

Error in myfisher23 (line 108)
zf=gammaln(Tables+1); %compute log(x!)

Giuseppe Cardillo

### Giuseppe Cardillo (view profile)

Sorry, but it doesn't crash!

myfisher23([ 6 6 9 ; 7 7 9])

2x3 matrix Fisher's exact test: 181 tables were evaluated
-----------------------------------------------------------------
p-value (2-tails): 1.0000000000
-----------------------------------------------------------------
Mid-p correction: 0.9644320759
-----------------------------------------------------------------

Matthew

### Matthew (view profile)

if you add the following lines:

at like, line 108 (immediately before calling "gammaln"), then it does not CRASH.

But just because it does not crash does not mean that it actually computes the correct numbers....

Matthew

### Matthew (view profile)

The following produces an error:

myfisher23([ 6 6 9 ; 7 7 9])

The error is:

Error using gammaln
Input must be nonnegative.
Error in myfisher23 (line 112)
zf=gammaln(Tables+1); %compute log(x!)

Emmanuel

### Emmanuel (view profile)

Works well. Thanks.

Mike

### Mike (view profile)

Excellent, Accurate.

Shilpa Gandhi

Fast, provides good output display. Exactly what I needed. Thanks!