File Exchange

image thumbnail

MyFisher23

version 2.0.0.0 (21.5 KB) by Giuseppe Cardillo
A very compact routine for Fisher's exact test on 2x3 contingency table

3 Downloads

Updated 06 May 2018

GitHub view license on GitHub

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 fully vectorized to speed up the computation.
Syntax: p=myfisher23(x)

Inputs:
X - 2x3 data matrix
Outputs:
- Three p-values

Created by Giuseppe Cardillo
giuseppe.cardillo-edta@poste.it

To cite this file, this would be an appropriate format: Cardillo G. (2007) MyFisher23: a very compact routine for Fisher's exact test on 2x3 matrix http://www.mathworks.com/matlabcentral/fileexchange/15399

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

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

Excellent, Accurate.

Shilpa Gandhi

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

Updates

2.0.0.0

inputparser; table implementation; github link

1.6.0.0

reuploading

1.5.0.0

Changes in description

1.4.0.0

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

1.3.0.0

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

1.2.0.0

little improvements in tables enumeration

1.1.0.0

Changes in help section

1.0.0.0

Speeding up using gammaln function and vectorization.

waitbar added

More compact and vectorized function

Bug fixed in 1-tailed P calculation

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