来自 fisher.test() 的 p 值与 phyper() 不匹配
p-value from fisher.test() does not match phyper()
Fisher 精确检验与超几何分布有关,我希望这两个命令会 return 相同的 p 值。谁能解释我做错了什么,他们不匹配?
#data (variable names chosen to match dhyper() argument names)
x = 14
m = 20
n = 41047
k = 40
#Fisher test, alternative = 'greater'
(fisher.test(matrix(c(x, m-x, k-x, n-(k-x)),2,2), alternative='greater'))$p.value
#returns 2.01804e-39
#geometric distribution, lower.tail = F, i.e. P[X > x]
phyper(x, m, n, k, lower.tail = F, log.p = F)
#returns 5.115862e-43
在这种情况下,对 phyper
的实际调用是 phyper(x - 1, m, n, k, lower.tail = FALSE)
。查看与您调用 fisher.test(matrix(c(x, m-x, k-x, n-(k-x)),2,2), alternative='greater')
相关的 fisher.test
的源代码。在第 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
Fisher 精确检验与超几何分布有关,我希望这两个命令会 return 相同的 p 值。谁能解释我做错了什么,他们不匹配?
#data (variable names chosen to match dhyper() argument names)
x = 14
m = 20
n = 41047
k = 40
#Fisher test, alternative = 'greater'
(fisher.test(matrix(c(x, m-x, k-x, n-(k-x)),2,2), alternative='greater'))$p.value
#returns 2.01804e-39
#geometric distribution, lower.tail = F, i.e. P[X > x]
phyper(x, m, n, k, lower.tail = F, log.p = F)
#returns 5.115862e-43
在这种情况下,对 phyper
的实际调用是 phyper(x - 1, m, n, k, lower.tail = FALSE)
。查看与您调用 fisher.test(matrix(c(x, m-x, k-x, n-(k-x)),2,2), alternative='greater')
相关的 fisher.test
的源代码。在第 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