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.
 A: 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.

