gpt4 book ai didi

R:提高数据框中每一行的 t 检验速度

转载 作者:行者123 更新时间:2023-12-04 10:32:26 24 4
gpt4 key购买 nike

我正在尝试提高数据帧的 t 检验循环的速度。

我有一个大数据框(约 15000 行和 205 列)。每列是一个细胞,每一行是一个基因。我可以根据不同引用表中提供的标识将列分为 2 组。

这是我写的循环:

for (i in 1:nrow(EC)){
ttest_result[i,2] <- rowMeans(EC)[i]
ttest_result[i,3] <- rowMeans(CP)[i]
ttest_result[i,4] <- (ttest_result[i,2] - ttest_result[i,3])
ttest_result[i,5] <- (ttest_result[i,2] / ttest_result[i,3])
ttest_result[i,6]<- t.test(EC[i,],CP[i,], var.equal = TRUE)$p.value
pb$tick()
}

此循环要求我根据列标识将原始数据帧拆分为 2 个数据帧。但是,此循环需要超过 45 分钟才能完成。

我想知道你们是否对我可以做的不同有什么建议?我如何潜在地使用应用函数来提高速度?

非常感谢!

最佳答案

limma 是解决方案。

> #library(BiocInstaller)
> #biocLite("limma")
>
> #create a dataset
> library(limma)
> data <- matrix(rnorm(15000*205),15000,205)
> dim(data)
[1] 15000 205
> rownames(data) <- paste("Gene",1:15000)
> str(data)
num [1:15000, 1:205] -0.55603 -0.45478 -1.76432 0.05198 0.00844 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:15000] "Gene 1" "Gene 2" "Gene 3" "Gene 4" ...
..$ : NULL
> # save the grouping in a factor
> f<-sample(c("ctrl","treat"),size = 205,replace = T)
>
> # perform the comparison gene per grouping
> t<-Sys.time()
> design <- model.matrix(~0+f)
> colnames(design) <- c("ctrl","treat")
> fit2 <- lmFit(data,design)
> contrast.matrix <- makeContrasts("treat-ctrl", levels=design)
> fit2 <- contrasts.fit(fit2, contrast.matrix)
> fit2 <- eBayes(fit2)
> top_table<-topTable(fit2, adjust="BH",coef=1,number =15000)
> dim(top_table)
[1] 15000 6
> head(top_table)
logFC AveExpr t P.Value adj.P.Val B
Gene 12434 -0.6238005 0.07603032 -4.454575 8.459040e-06 0.1268856 -0.5006572
Gene 11609 -0.5827713 0.11178709 -4.156804 3.242956e-05 0.2174629 -1.0670677
Gene 5924 0.5729590 -0.02980352 4.089151 4.349258e-05 0.2174629 -1.1903102
Gene 10460 -0.5274251 -0.07930193 -3.770822 1.632559e-04 0.5747294 -1.7431451
Gene 5950 0.5216678 -0.03304759 3.730682 1.915765e-04 0.5747294 -1.8096840
Gene 14518 0.5053476 -0.05750282 3.612195 3.044821e-04 0.6298752 -2.0019558
> Sys.time()-t
Time difference of 0.3026412 secs

请注意,它还会打印调整后的 p 值(您可以选择方法),这是筛选感兴趣基因的标准之一。

关于R:提高数据框中每一行的 t 检验速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46623173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com