Code covered by the BSD License  

Highlights from
MyFisher23

4.0

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

MyFisher23

by

 

22 Jun 2007 (Updated )

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

| Watch this File

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)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (11)
05 Dec 2014 Douglas Crawford

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

03 Apr 2014 Giuseppe Cardillo

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

03 Apr 2014 Giuseppe Cardillo

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

03 Apr 2014 Giuseppe Cardillo

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

02 Apr 2014 Alex Anderson

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

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

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

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

09 Dec 2011 Emmanuel

Works well. Thanks.

06 Apr 2011 Mike

Excellent, Accurate.

07 May 2008 Shilpa Gandhi

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

Updates
26 Jun 2007

Bug fixed in 1-tailed P calculation

02 Jul 2007

More compact and vectorized function

01 May 2008

waitbar added

12 Nov 2008

Changes in help section

25 Nov 2008

little improvements in tables enumeration

23 Oct 2009

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

23 Oct 2009

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

23 Dec 2009

Changes in description

12 Feb 2014

reuploading

Contact us