R 中 fisher.test 中的差异

discrepancies in fisher.test in R

提问人:Nneka 提问时间:3/17/2023 更新时间:3/17/2023 访问量:69

问:

我虽然运行了两次相同的测试,但结果却不同。

首先,我从我的数据中创建了一个列联表:

> SAtab1 <- table(dt2$ACE_SA_BIN, dt2$IHD_other_healthy)
> SAtab1
   
      0   1   2
  0 345 577  29
  1  10  25   2

然后我运行 Fisher 测试:

> fisher.test(SAtab1, workspace = 2e8, simulate.p.value=TRUE)

    Fisher's Exact Test for Count Data with simulated p-value (based on 2000 replicates)

data:  SAtab1
p-value = 0.2969
alternative hypothesis: two.sided

然后我在某处看到了代码,他们在其中传递了一个矩阵而不是列联表。所以我创建了一个矩阵:

> SAtab2 <- 
+   matrix(c(345,10,577,25,29,2),
+          nrow = 2,
+          dimnames = list(Gender = c("no", "yes"),
+                          Illness =c("no illness","other than IHD", "HD")))
> SAtab2
      Illness
Gender no illness other than IHD HD
   no         345            577 29
   yes         10             25  2

并再次运行 fisher.test:

> fisher.test(SAtab2, workspace = 2e8, simulate.p.value=TRUE)

    Fisher's Exact Test for Count Data with simulated p-value (based on 2000 replicates)

data:  SAtab2
p-value = 0.2829
alternative hypothesis: two.sided

测试怎么可能得不到相同的结果?轻微的偏差从何而来?

R 比较 差分析 卡方

评论


答:

2赞 akrun 3/17/2023 #1

问题不在于(表与矩阵),而是 TRUE,例如,如果我们多次运行相同的数据,它会返回不同的 p 值。根据 ?fisher.testclasssimulate.p.valuesimulate.p.value

simulate.p.value - 指示是否通过蒙特卡罗模拟计算 p 值的逻辑,大于 2 × 2 表

> fisher.test(SAtab2, workspace = 2e8, simulate.p.value=TRUE)

    Fisher's Exact Test for Count Data with simulated p-value (based on 2000 replicates)

data:  SAtab2
p-value = 0.3088
alternative hypothesis: two.sided

> fisher.test(SAtab2, workspace = 2e8, simulate.p.value=TRUE)

    Fisher's Exact Test for Count Data with simulated p-value (based on 2000 replicates)

data:  SAtab2
p-value = 0.3018
alternative hypothesis: two.sided

而否则它将给出相同的输出

> lst1 <- replicate(5, fisher.test(SAtab2, workspace = 2e8, simulate.p.value=FALSE), simplify = FALSE)

> all.equal(lst1[[1]], lst1[[2]])
[1] TRUE
> all.equal(lst1[[1]], lst1[[3]])
[1] TRUE
> all.equal(lst1[[1]], lst1[[5]])
[1] TRUE