# Calculating Fisher's exact in Rails?

I've tried to implement the Fishers exact test on a 2x2 table as described in the Wikipedia entry but not sure of my numbers because it gives a different answer than R according to another question on this board.

Wondering if somebody can check. By way of example, I consider a hypotehtical 2x2 as follows:

Are you in favor or opposed to the death penalty?

                  Liberal    Conservative
Favor               30            30
Opposed             70            70


According to my calculations, the exact probability of this arrangement is given by row1! * row2! * col1! * col2! / cell1! & cell2! * cell3! * cell4! * (sum of cells)!

My method gives a result of 12.253%. But the question I want to answer is not this exact probability but whether political view "matter" to the peope's opinion about the death penalty. There example above indicates there clearly is not, so I guess this 12% probability represents only the likelihood of getting an exactly equal result from a random selection of s sets of 100 people

According to Wikipedia, it seems like I'm meant to figure this out by picking an arbitrary "more extreme" result and calculating its fishers exact and adding the two together.

So I tried the following which maintains row and column totals:

                     Liberal    Conservative
Favor               31            29
Opposed             69            71


hich gives a total of 11.69%

Adding the two I get 23.95% which I gather is the one tailed result, multiplying by 2 gives me 47.89% which is pretty close to what I expected, that in this case the results suggest that there is a 50% chance that observed results are same as those I would have gotten from random selection of people, both liberal and conservative.

My question is: a) do the numbers seem correctly calculated, and b) is my interpretation correct?

Thanks.

• You need all the more extreme tables, not just one. Dec 7, 2017 at 16:38
• Wow that's a lot of math. So every more extreme permutation where the row total and column totals are constant? Dec 7, 2017 at 17:01
• In your case I think there are 30 of them in addition to the actual table you already have. There is a reason why people in the old days used extensive tables for this and nowadays use a computer. Dec 7, 2017 at 17:11
• I tried this on the above example, adding 1 to cell 1,1 and 2,2 while subtracting 1 from cells 1,2 and 2,1. until some cell reaches 0. Dec 7, 2017 at 17:41
• I tried this on the above example, adding 1 to cell 1,1 and 2,2 while subtracting 1 from cells 1,2 and 2,1. until some cell reaches 0 (including the 0 case). I get a result of 1.1225, which if its supposed to be a probability doesn't make much sense. Do you happen to know what the right answer is? Dec 7, 2017 at 17:41

To reduce the amount of space needed for an example I will show for a smaller table.

 
 

fisher.test(matrix(c(3,9,6,2), nrow = 2), alternative = "l") Fisher's Exact Test for Count Data data: matrix(c(3, 9, 6, 2), nrow = 2) p-value = 0.03989 alternative hypothesis: true odds ratio is less than 1 95 percent confidence interval: 0.0000000 0.9111673 sample estimates: odds ratio 0.1269701 

So for a table with entries 3, 9, 6 and 2 the one-sided answer should be 0.03989

Now for each table separately. First I calculate the log of the numerator since that is invariant across tables

 
 

lnum <- lfactorial(12) + lfactorial(8) + lfactorial(11) + lfactorial(9) exp(lnum) [1] 2.79754e+26 exp(lnum - (lfactorial(20) + lfactorial(3) + lfactorial(9) + lfactorial(6) + lfactorial(2))) [1] 0.0366754 exp(lnum - (lfactorial(20) + lfactorial(2) + lfactorial(10) + lfactorial(7) + lfactorial(1))) [1] 0.003143606 exp(lnum - (lfactorial(20) + lfactorial(1) + lfactorial(11) + lfactorial(8) + lfactorial(0))) [1] 7.144558e-05  So if you try your method on that table and get something different you may get a clue as to where your calculations are going wrong.