【问题标题】:Fisher test using apply function in R使用 R 中的应用函数进行 Fisher 测试
【发布时间】:2011-06-07 06:30:45
【问题描述】:

以下是代码:问题是计算很慢。

矩阵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)
    }

【问题讨论】:

  • 不是调用fisher 3次,有没有简单的方法来做测试。
  • stats.stackexchange.com 的人可能知道更好的方法

标签: r apply


【解决方案1】:

您可以尝试使用lapply 来循环不同的选择(更少、更大、双面)并将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 三次,只是形式更加紧凑。我不知道有一个函数可以在同一个调用中计算所有三个备选方案的 Fisher 测试,但也许其他人会权衡其中一个。

【讨论】:

  • 嗯,它费雪精确检验。如果您的值很大,那么它会很慢;它必须计算所有可能的配置。
猜你喜欢
  • 1970-01-01
  • 2015-05-12
  • 2013-02-05
  • 2018-05-18
  • 1970-01-01
  • 2018-08-01
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
相关资源
最近更新 更多