使用 R 中的 apply 函数进行 Fisher 测试
下面是代码:问题是计算速度很慢。
矩阵 gene1
、gene2
的长度都不相同 (8000)
pos <- c()
neg <- c()
either <- c()
for(i in 1:ncol(both)){
x <- cbind(both[,i], gene1[,i], gene2[,i], neither[,i])
test <- apply(x, 1, function(s){fisher.test(matrix(s, nrow = 2),
alternative = "greater")$p.value})
pos <- c(test,pos)
test1 <- apply(x, 1, function(s){fisher.test(matrix(s, nrow = 2),
alternative = "less")$p.value})
neg <- c(test1, neg)
test2 <- apply(x, 1, function(s){fisher.test(matrix(s, nrow = 2))$p.value})
either <- c(test2, either)
}
The following is the code: the problem is that the calculation is very slow.
The matrices, gene1
, gene2
and neither are of same length (8000)
pos <- c()
neg <- c()
either <- c()
for(i in 1:ncol(both)){
x <- cbind(both[,i], gene1[,i], gene2[,i], neither[,i])
test <- apply(x, 1, function(s){fisher.test(matrix(s, nrow = 2),
alternative = "greater")$p.value})
pos <- c(test,pos)
test1 <- apply(x, 1, function(s){fisher.test(matrix(s, nrow = 2),
alternative = "less")$p.value})
neg <- c(test1, neg)
test2 <- apply(x, 1, function(s){fisher.test(matrix(s, nrow = 2))$p.value})
either <- c(test2, either)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用
lapply
循环不同的替代方案(less、greater、two.side)并将fisher.test 调用包装在您自己的函数中。也许是这样的:如果没有一些测试数据来检查,我不能向您保证这不会有任何问题,但这个基本策略应该可以满足您的要求。
请注意,这仍然会调用
fisher.test
三次,只是以更紧凑的形式。我不知道有一个函数可以在同一个调用中使用所有三种替代方案来计算费舍尔测试,但也许其他人会权衡其中一个。You can try using
lapply
to loop over the different alternatives (less, greater, two.sided) and wrap the fisher.test call in your own function. Perhaps something like this:Without some test data to check on, I can't assure you there won't be any gotcha's in this, but this basic strategy should do what you want.
Note that this still calls
fisher.test
three times, just in a somewhat more compact form. I don't know of a function that calculates a fisher test with all three alternatives in the same call, but perhaps someone else will weigh in with one.