Code covered by the BSD License  

Highlights from
Fisher's exact test with n x m contingency table

2.625

2.6 | 8 ratings Rate this file 12 Downloads (last 30 days) File Size: 3.66 KB File ID: #24379

Fisher's exact test with n x m contingency table

by

 

09 Jun 2009 (Updated )

Do you have problem with Fisher's exact test where the contingency table is more than 2*2?

| Watch this File

File Information
Description

    Fisher's exact test is a statistical test used to determine if there are nonrandom associations between two categorical variables[1].
     The job of Fisher's exact test with 2 x 2 or 2x 3 contingency table is already easily done by others. However, the one with n x m contingency table hasn't found , or with bad computation.
     This function efficiently deals with Fisher's exact test with n x m contingency table.
     As for the function :
 [ Sig,PValue,ContigenMatrix ] = FisherExactTest( XVector,YVector )
Input : the data of two variables X,Y as XVector and YVector
             Or you can just input the contingency table.
Output: "Sig" returns 1 if X and Y associate,otherwise 0
               "PValue" returns the computed p-value
              "ContigenMatrix" returns the n x m contingency table
  
    Please start with "Controlcenter.m", there are two simple example for explanation. If you open the algorithm, clear description of the algorithm will be illustrated.
    If there is anything wrong, or your personal need, please let me know, I will help you as soon as possible :-)

Reference:
[1] http://mathworld.wolfram.com/FishersExactTest.html

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (13)
21 Apr 2014 Jian Zhang

Please address the p-value issue as questioned in earlier posts, otherwise this test is useless.

17 Jan 2013 Simon

As has been addressed before, this script does not calculate the p-value. Please see Metha and Patel (1986) for improving your code.

09 Dec 2011 Emmanuel

You have not addressed the comment from Mike above, which is crucial. If Mike is correct, which to me appears to be the case, your function does not compute the Pvalue, but the Pcutoff (equation (2) in the mathworld link). Please address this issue as this is not a minor distinction at all.

20 Sep 2011 karin Zachinelly

You are the best, Guangdi!

20 Sep 2011 Guangdi Li

Thanks for report, Karin.

The infinite problem is solved and a new version based on matlab-R is provided(for those who are familiar with fisher test in R).

14 Sep 2011 karin Zachinelly

Hello Guangdi, there seems to be a bug on this function, if you try, for example, with these two vectors, the function enters into an infinite loop:

X = [1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,2,1,2,1,2,1,1,1,1,1,1,1,2,2,1,1,1,2,1,1,2,1,1,1,2,2,2,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1];

Y = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,2,2,2,2,1,2,2,2,1,2,2,1,1,2,2,2,2,1,2,1,2,1,2,2,1,1,2,2,2,2,2,1,2,2,2,2,1,2,2,1,1,2,2,2,1,2,2,1];

Cheers

11 Apr 2011 Mike

I think you only implemented equation (2) from the link you posted. If you read what's written on that page after equation (2), you will find this:

"Now find all possible matrices of nonnegative integers consistent with the row and column sums Ri and Cj. For each one, calculate the associated conditional probability using (2), where the sum of these probabilities must be 1.

To compute the P-value of the test, the tables must then be ordered by some criterion that measures dependence, and those tables that represent equal or greater deviation from independence than the observed table are the ones whose probabilities are added together."

06 Apr 2011 Guangdi Li

I only did the programming based on the mathworld website. Indeed, there may be some fast and efficient way for n * m table, if so, please let me know the reference for further improvement.Thanks for comments.

06 Apr 2011 Mike

This doesn't correctly compute a fisher exact test p-value. It only computes the probability of exactly getting the observed contingency table. To compute a p-value, one must compute the probability of obtaining the observed results *OR SOMETHING MORE EXTREME*. In this case, one has to sum the probabilities over all possible "More extreme" contingency tables; this sum is the value of the Fisher Exact Test.

28 Jan 2011 Guangdi Li

Thanks for Dubuis's scrutiny. Indeed, If Sig=1, then variables in XVector and YVector are significantly associate. Concerning Nick's question about help section, i will learn how to do it soon. For the 2nd question about two sides p-value, I am sorry that I wrote the code learning from the Wolfram website. If you can show me the equations for two side, I would be very happy to improve the code. Thanks for your comments.

28 Jan 2011 Nick Mariette

The help section of the function code should be prepared properly to show the usage (including inputs and outputs) when you type:
help FisherExactTest

It should not be necessary to inspect the code, or the code in a secondary function (i.e ControlCentor.m [sic]).

Secondly, the functionality seems correct but somewhat incomplete.

When I use the example from the Wolfram website, it gives the same result as the website and the other two Fisher test functions on FileExchange (myfisher and Fisherextest). However it only gives one sided p-value, not both tails, which are provided by those other two functions.

18 Jan 2011 S├ębastien Dubuis

I think there just a mistake in the comments:

% If Sig=1, then variables in XVector and YVector are independent.
% If Sig=0, then variables in XVector and YVector are dependent.

Wouldn't it be the opposite since a significant p-value will get to (as said in Wolfram Mathworld) a "statistically significant association" ?

11 Nov 2010 Claudia Lerma

The function works very well, and the example provided was really helpful!

Updates
28 Jan 2010

make the function simpler than last version.

28 Jan 2011

correct the description

20 Sep 2011

infinite problem is fixed and a new version based on R is also provided

Contact us