使用 R 中的 apply 函数进行 Fisher 测试

发布于 2024-11-14 10:29:15 字数 654 浏览 1 评论 0原文

下面是代码:问题是计算速度很慢。

矩阵 gene1gene2 的长度都不相同 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄断弥空 2024-11-21 10:29:15

您可以尝试使用 lapply 循环不同的替代方案(less、greater、two.side)并将fisher.test 调用包装在您自己的函数中。也许是这样的:

myTest <- function(altn,x){
    ft <- apply(x,1,FUN=function(s,alt) {
                        fisher.test(matrix(s,nrow=2),alternative=alt)$p.value},
                        alt=altn)
}

pos <- c()
neg <- c()
either <- c()
for(i in 1:ncol(both)){
    x <- cbind(both[,i], gene1[,i], gene2[,i], neither[,i])
    rs <- lapply(c('two.sided','greater','less'),myTest,x=x)
    pos <- c(rs[[2]],pos)
    neg <- c(rs[[3]],neg)
    either <- c(rs[[1]],either)
}

如果没有一些测试数据来检查,我不能向您保证这不会有任何问题,但这个基本策略应该可以满足您的要求。

请注意,这仍然会调用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:

myTest <- function(altn,x){
    ft <- apply(x,1,FUN=function(s,alt) {
                        fisher.test(matrix(s,nrow=2),alternative=alt)$p.value},
                        alt=altn)
}

pos <- c()
neg <- c()
either <- c()
for(i in 1:ncol(both)){
    x <- cbind(both[,i], gene1[,i], gene2[,i], neither[,i])
    rs <- lapply(c('two.sided','greater','less'),myTest,x=x)
    pos <- c(rs[[2]],pos)
    neg <- c(rs[[3]],neg)
    either <- c(rs[[1]],either)
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文