Fisher精确检验与超几何分布有关,我希望这两个命令将返回相同的p值。谁能解释我做错了哪些不匹配的事情?
preferBarber = yield barbeariaStore.get(lambda barber : barber == 'Barber A')
答案 0 :(得分:4)
在这种情况下,对phyper
的实际调用是phyper(x - 1, m, n, k, lower.tail = FALSE)
。查看与fisher.test
的调用相关的fisher.test(matrix(c(x, m-x, k-x, n-(k-x)),2,2), alternative='greater')
的源代码。在第138行,PVAL
设置为:
switch(alternative, less = pnhyper(x, or),
greater = pnhyper(x, or, upper.tail = TRUE),
two.sided = {
if (or == 0) as.numeric(x == lo) else if (or ==
Inf) as.numeric(x == hi) else {
relErr <- 1 + 10^(-7)
d <- dnhyper(or)
sum(d[d <= d[x - lo + 1] * relErr])
}
})
由于alternative = 'greater'
,PVAL
被设置为pnhyper(x, or, upper.tail = TRUE)
。您可以看到在行122上定义了pnhyper
。在这里,or = 1
被传递到ncp
,因此呼叫为phyper(x - 1, m, n, k, lower.tail = FALSE)
使用您的值:
x = 14
m = 20
n = 41047
k = 40
phyper(x - 1, m, n, k, lower.tail = FALSE)
# [1] 2.01804e-39