用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)
}