Demo
dat3 <- read.table(header = TRUE, text = "
Y group count
1 1 7
1 0 8
0 1 12
0 0 2
")
(xtab3 <- xtabs(count ~ Y + group, dat3)[2:1,2:1])
## group
## Y 1 0
## 1 7 8
## 0 12 2
fisher.test(xtab3)
##
## Fisher's Exact Test for Count Data
##
## data: xtab3
## p-value = 0.05017
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 0.01275056 1.09516333
## sample estimates:
## odds ratio
## 0.1565941
fisher.test(xtab3, alternative = "less") # This is the one asked for
##
## Fisher's Exact Test for Count Data
##
## data: xtab3
## p-value = 0.03288
## alternative hypothesis: true odds ratio is less than 1
## 95 percent confidence interval:
## 0.00000 0.86222
## sample estimates:
## odds ratio
## 0.1565941
fisher.test(xtab3, alternative = "greater")
##
## Fisher's Exact Test for Count Data
##
## data: xtab3
## p-value = 0.9964
## alternative hypothesis: true odds ratio is greater than 1
## 95 percent confidence interval:
## 0.01899869 Inf
## sample estimates:
## odds ratio
## 0.1565941
## Function to calculate chi-squared value and hypergeometric probability
ExactProb <- function(i) {
Prob <- dhyper(x = i, m = 15, n = 14, k = 19)
c(a = i, Prob = Prob)
}
datGraph <- lapply(0:20, ExactProb) %>% do.call(rbind, .) %>% as.data.frame
datGraph # a = 5,6,7 are the possible ones
## a Prob
## 1 0 0.00000000000
## 2 1 0.00000000000
## 3 2 0.00000000000
## 4 3 0.00000000000
## 5 4 0.00000000000
## 6 5 0.00014992504
## 7 6 0.00349825087
## 8 7 0.02923538231
## 9 8 0.11694152924
## 10 9 0.25012493753
## 11 10 0.30014992504
## 12 11 0.20464767616
## 13 12 0.07796101949
## 14 13 0.01574212894
## 15 14 0.00149925037
## 16 15 0.00004997501
## 17 16 0.00000000000
## 18 17 0.00000000000
## 19 18 0.00000000000
## 20 19 0.00000000000
## 21 20 0.00000000000