Code covered by the BSD License

### Highlights from MyFisher23

4.0
4.0 | 4 ratings Rate this file 9 Downloads (last 30 days) File Size: 2.83 KB File ID: #15399 Version: 1.6

# MyFisher23

### Giuseppe Cardillo (view profile)

22 Jun 2007 (Updated )

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

File Information
Description

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.

MATLAB release MATLAB 7.3 (R2006b)
05 Dec 2014 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

Comment only
03 Apr 2014 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

Comment only
03 Apr 2014 Giuseppe Cardillo

### Giuseppe Cardillo (view profile)

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

Comment only
03 Apr 2014 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
-----------------------------------------------------------------

Comment only
02 Apr 2014 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!)

Comment only
12 Feb 2014 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
-----------------------------------------------------------------

Comment only
11 Feb 2014 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....

Comment only
11 Feb 2014 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!)

09 Dec 2011 Emmanuel

### Emmanuel (view profile)

Works well. Thanks.

06 Apr 2011 Mike

### Mike (view profile)

Excellent, Accurate.

07 May 2008 Shilpa Gandhi

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

26 Jun 2007

Bug fixed in 1-tailed P calculation

02 Jul 2007

More compact and vectorized function

01 May 2008

12 Nov 2008 1.1

Changes in help section

25 Nov 2008 1.2

little improvements in tables enumeration

23 Oct 2009 1.3

Actually, the function also computes the mid-P correction to make the test less conservative.

23 Oct 2009 1.4

Actually, the function also computes the mid-P correction to make the test less conservative.

23 Dec 2009 1.5

Changes in description

12 Feb 2014 1.6