费舍尔精确检验在哈兰修正下仍返回无穷大胜算比

编程语言 2026-07-10

我有一些数据集,在某些类别中出现0计数,这导致优势比(odds ratio, OR)为Inf。我尝试用标准的哈代恩校正,即在表格的每个条目上加0.5来纠正这个问题,但R 的Fisher精确检验仍然返回OR = Inf。这里的优势比应该是

(1.5/387.5)/(0.5/500.5) = 3.8748

而我得到的是:

rubbish = c(1,387,0,500)
test_df = data.frame(rbind(rubbish[1:2],rubbish[3:4]))
test_df = test_df + 0.5
fisher.test(test_df)

返回的是:

        Fisher's Exact Test for Count Data

data:  temp_df
p-value = 0.1917
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
 0.2409358       Inf
sample estimates:
odds ratio 
       Inf

为什么表中没有0 值,我仍然得到OR = Inf?

解决方案

我已经尝试重现你的问题,除了Inf之外,我还收到一个警告。问题在于该函数期望输入为整数,并对其进行四舍五入。如果你查看该函数的代码,你会看到:

if (!is.integer(x)) {
      xo <- x
      x <- round(x)

以下是一些测试。

> test_df = as.matrix(data.frame(rbind(rubbish[1:2],rubbish[3:4])))
> fisher.test(test_df)

    Fisher's Exact Test for Count Data

data:  test_df
p-value = 0.4369
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
 0.03304228        Inf
sample estimates:
odds ratio 
       Inf 

> fisher.test(test_df+1)

    Fisher's Exact Test for Count Data

data:  test_df + 1
p-value = 0.584
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
   0.1338413 152.5196768
sample estimates:
odds ratio 
  2.579775 

> fisher.test(test_df+0.5)

    Fisher's Exact Test for Count Data

data:  test_df + 0.5
p-value = 0.1917
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
 0.2409358       Inf
sample estimates:
odds ratio 
       Inf 

Warning message:
In fisher.test(test_df + 0.5) :
  'x' has been rounded to integer: Mean relative difference: 0.002247191


> fisher.test(test_df+0.6)

    Fisher's Exact Test for Count Data

data:  test_df + 0.6
p-value = 0.584
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
   0.1338413 152.5196768
sample estimates:
odds ratio 
  2.579775 

Warning message:
In fisher.test(test_df + 0.6) :
  'x' has been rounded to integer: Mean relative difference: 0.001796945

如果你确实需要严格使用哈代恩校正但又不重新实现该函数,我看不出有什么办法。否则就把0.5改成1 就可以了。你也可以向代码的开发者提交一个issue,请求让哈代恩校正生效。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章