File Exchange

image thumbnail

MyFisher23

version 1.6 (2.83 KB) by

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

4 Downloads

Updated

View License

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.

Comments and Ratings (11)

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

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

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

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
-----------------------------------------------------------------

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

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

if you add the following lines:

% filter out bad stuff
bad_rows = logical(sum(Tables < 0,2));
Tables = Tables(~bad_rows,:);

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

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

Works well. Thanks.

Mike

Mike (view profile)

Excellent, Accurate.

Shilpa Gandhi

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

Updates

1.6

reuploading

1.5

Changes in description

1.4

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

1.3

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

1.2

little improvements in tables enumeration

1.1

Changes in help section

waitbar added

More compact and vectorized function

Bug fixed in 1-tailed P calculation

MATLAB Release
MATLAB 7.3 (R2006b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video