Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用R中的apply函数进行Fisher检验_R_Apply - Fatal编程技术网

用R中的apply函数进行Fisher检验

用R中的apply函数进行Fisher检验,r,apply,R,Apply,下面是代码:问题是计算速度非常慢 矩阵,gene1,gene2,两者的长度都不相同(8000) pos您可以尝试使用lappy循环不同的选项(更少、更大、双面),并将fisher.test调用包装到您自己的函数中。也许是这样的: myTest <- function(altn,x){ ft <- apply(x,1,FUN=function(s,alt) { fisher.test(matrix(s,nrow=2),altern

下面是代码:问题是计算速度非常慢

矩阵,
gene1
gene2
,两者的长度都不相同(8000)


pos您可以尝试使用
lappy
循环不同的选项(更少、更大、双面),并将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)
}

myTest是否有一种简单的方法来进行测试,而不是调用fisher 3次。stats.stackexchange.com上的人员可能知道一种更好的方法,这是一种fisher精确测试。如果你的价值观是大的,它只是将是缓慢的;它必须计算每个可能的配置。
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)
}