我的数据框看起来像这样:
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
....
Run Code Online (Sandbox Code Playgroud)
对于我想做Fisher精确测试的每一组.
table <- matrix(c(595.00000, 984.00200, 18696, 32185), ncol=2, byrow=T)
Group1 <- Fisher.test(table, alternative="greater")
Run Code Online (Sandbox Code Playgroud)
试图循环数据框:
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")
}
Run Code Online (Sandbox Code Playgroud)
但得到了错误信息
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
Run Code Online (Sandbox Code Playgroud)
如何解决此问题或者可能采用其他方式循环数据?
你的第一个错误是: 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)
Run Code Online (Sandbox Code Playgroud)
你应该尝试增加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)
Run Code Online (Sandbox Code Playgroud)
或者,如果您希望用a替换chisq.test(我认为您应该将这些巨大的值用于性能增益,而p值没有显着差异):
apply(as.matrix(df[,1:4]), 1, function(x)
chisq.test(matrix(round(x), ncol=2))$p.value)
Run Code Online (Sandbox Code Playgroud)
这将提取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
Run Code Online (Sandbox Code Playgroud)