Fisher.test alternative problem in R I found a weird thing about the "alternative" argument in fisher.test in R
fisher.test(matrix(c(10,5,30,60), 2,2), alternative="greater")$p.value
# [1] 0.01587719
fisher.test(matrix(c(10,5,30,60), 2,2))$p.value
# [1] 0.02063292

fisher.test(matrix(c(3, 10, 2, 37), 2, 2),alternative = "greater") $p.value
# [1] 0.09276711
fisher.test(matrix(c(3, 10, 2, 37), 2, 2)) $p.value
# [1] 0.09276711

Why does the latter matrix return the same p-value, while the former return different p-value?
 A: According to Wikipedia:

For a two-tailed test we must also consider tables that are equally extreme, .... An approach used by the fisher.test is to compute the p-value by summing the probabilities for all tables with probabilities less than or equal to that of the observed table.

And the probability of each table can be calculated by hypergeometric distribution.
For the first matrix matrix(c(10,5,30,60), 2,2), we can plot the probability against all possible values of the top-left cell (from 0 to 15):

So the two.sided p-value = the sum of the 8 points below the dash line. The alternative greater p-value = the sum of the right most 6 points.
However, for the second matrix matrix(c(3,10,2,37), 2, 2), the points on the left are above the dash line. So there's no difference in two.sided and greater p-values.

The code for the plot:
hypergeometric <- function(x) {
  a <- 0:min(x[1]+x[2],x[1]+x[3])
  prob <- dhyper(a, x[1]+x[2], x[3]+x[4], x[1]+x[3])

  plot(a, prob, xlab='first cell', main='Hypergeometric')
  abline(h=prob[which(a==x[1])], lty=2)

  two_sided <- sum(prob[prob<=prob[a==x[1]]])
  greater <- sum(prob[a>=x[1]])
  less <- sum(prob[a<=x[1]])

  return(c(two_sided, greater, less))
}
hypergeometric(matrix(c(10,5,30,60),2,2))
# two_sided    greater       less
#0.02063292 0.01587719 0.99670182
fisher.test(matrix(c(10,5,30,60),2,2), alternative='two.sided')$p.value
#0.02063292
fisher.test(matrix(c(10,5,30,60),2,2), alternative='greater')$p.value
#0.01587719
fisher.test(matrix(c(10,5,30,60),2,2), alternative='less')$p.value
#0.9967018

hypergeometric(matrix(c(3,10,2,27),2,2))
# two_sided    greater       less
# 0.1623877  0.1623877  0.9741121

