[英]How to perform a Fisher's exact test `fisher.test()` when a warning is generated by the Chi square test in R?
[英]R For loop to perform Fisher's test - Error message
我的數據框看起來像這樣:
595.00000 18696 984.00200 32185 Group1
935.00000 18356 1589.00000 31580 Group2
40.00010 19251 73.00000 33096 Group3
1058.00000 18233 1930.00000 31239 Group4
19.00000 19272 27.00000 33142 Group5
1225.00000 18066 2149.00000 31020 Group6
....
對於我想做Fisher精確測試的每一組。
table <- matrix(c(595.00000, 984.00200, 18696, 32185), ncol=2, byrow=T)
Group1 <- Fisher.test(table, alternative="greater")
試圖循環數據框:
for (i in 1:nrow(data.frame))
{
table= matrix(c(data.frame$V1, data.frame$V2, data.frame$V3, data.frame$V4), ncol=2, byrow=T)
fisher.test(table, alternative="greater")
}
但得到了錯誤信息
Error in fisher.test(table, alternative = "greater") :
FEXACT error 40.
Out of workspace.
In addition: Warning message:
In fisher.test(table, alternative = "greater") :
'x' has been rounded to integer: Mean relative difference: 2.123828e-06
如何解決此問題或者可能采用其他方式循環數據?
您的第一個錯誤是: Out of workspace
?fisher.test
fisher.test(x, y = NULL, workspace = 200000, hybrid = FALSE,
control = list(), or = 1, alternative = "two.sided",
conf.int = TRUE, conf.level = 0.95,
simulate.p.value = FALSE, B = 2000)
您應該嘗試增加workspace
(默認值= 2e5)。
但是,這種情況發生在您的情況下,因為您確實有巨大的價值。 根據經驗,如果矩陣的所有元素都大於5(或者在你的情況下是10,因為df = 1),那么你可以使用chisq.test
通過卡方檢驗的獨立性來安全地近似它。 對於你的情況,我認為你應該使用chisq.test
。
並且出現warning message
是因為您的值不是整數(595.000)等。因此,如果您真的想以遞歸方式使用fisher.test
,請執行此操作(假設您的數據位於df
並且是data.frame
:
# fisher.test with bigger workspace
apply(as.matrix(df[,1:4]), 1, function(x)
fisher.test(matrix(round(x), ncol=2), workspace=1e9)$p.value)
或者,如果您希望用chisq.test
(我認為您應該將這些巨大的值用於性能增益,而p值沒有顯着差異):
apply(as.matrix(df[,1:4]), 1, function(x)
chisq.test(matrix(round(x), ncol=2))$p.value)
這將提取p值。
編輯1:我剛注意到你使用one-sided Fisher's exact test
。 也許你應該繼續使用Fisher測試更大的工作空間,因為我不確定是否有單側卡方檢驗的獨立性,因為它已經right-tail
概率計算出來了(你不能將p值除以2作為其不對稱)。
編輯2:由於您需要具有p值的組名,並且您已經有data.frame,我建議您使用data.table
包,如下所示:
# example data
set.seed(45)
df <- as.data.frame(matrix(sample(10:200, 20), ncol=4))
df$grp <- paste0("group", 1:nrow(df))
# load package
require(data.table)
dt <- data.table(df, key="grp")
dt[, p.val := fisher.test(matrix(c(V1, V2, V3, V4), ncol=2),
workspace=1e9)$p.value, by=grp]
> dt
# V1 V2 V3 V4 grp p.val
# 1: 130 65 76 82 group1 5.086256e-04
# 2: 70 52 168 178 group2 1.139934e-01
# 3: 55 112 195 34 group3 7.161604e-27
# 4: 81 43 91 80 group4 4.229546e-02
# 5: 75 10 86 50 group5 4.212769e-05
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.