【发布时间】:2014-04-24 01:08:30
【问题描述】:
我有两个矩阵 diff.exp.protein.expr 和 lncRNA.expr 每个矩阵都有 64 列,但行数不同。
>dim(diff.exp.protein.expr)
[1] 14000 64
>dim(lncRNA.expr)
[1] 7600 64
我在两个单独的线性模型中使用这些矩阵作为输入,在其中我将 diff.exp.protein.expr 的每一行与 lncRNA.expr 的所有行进行比较( =2*1.06 亿次测试)。最后,我想使用 anova 来比较这两个线性模型在统计上是否不同,我为此编写了一个函数,如下所示:
lm.anova <- function(diff.exp.protein.expr,lncRNA.expr,colData)
{
tempdff <- data.frame() #create an empty dataframe
for(i in 1:nrow(diff.exp.protein.expr)) #traverse through 1st matrix
{
for(j in 1:nrow(lncRNA.expr)) #traverse through 2nd matrix
{
#model 1
lm1 <- lm(diff.exp.protein.expr[i,]~colData$gender+colData$age+colData$treatment)
#model 2 (has an additional interaction term at the end)
lm2 <- lm(diff.exp.protein.expr[i,]~colData$gender+colData$age+colData$treatment+lncRNA.expr[j,]+colData$treatment*lncRNA.expr[j,])
#get the summary of model 2
lm.model <- summary(lm2)
#get the rownames of ith row of matrix 1 and jth row of matrix 2
#get the pvalue from the anova model
#get the estimate and std. error for last two terms of model 2
#add these values as a row to tempdff
tempdff <- rbind(tempdff,data.frame(rownames(diff.exp.protein.expr)[i],rownames(lncRNA.expr)[j],
anova(lm1,lm2)$"Pr(>F)"[2]),lm.model$coefficients[4,1:2],lm.model$coefficients[6,1:2])
}
}
return(tempdff) #return results
}
lm.anova.res <- lm.anova(diff.exp.protein.expr,lncRNA.expr,colData) #call function
这就是进入函数的数据的样子:
>head(diff.exp.protein.expr)[,1:5] #log transformed values
C00060 C00079 C00135 C00150 C00154
ENSG00000000005.5 5.187977 6.323024 6.022986 5.376513 4.810042
ENSG00000000460.12 7.071433 7.302448 7.499133 7.441582 7.439453
ENSG00000000938.8 8.713285 8.584996 8.982816 9.787420 8.823927
ENSG00000001497.12 9.569952 9.658418 9.392670 9.394250 9.087214
ENSG00000001617.7 9.215362 9.417367 8.949943 8.172713 9.198314
ENSG00000001626.10 6.363638 6.038328 6.698733 5.202132 5.336239
>head(lncRNA.expr)[,1:5] #log transformed values
C00060 C00079 C00135 C00150 C00154
ENSG00000100181.17 7.477983 7.776463 7.950514 8.098205 7.278615
ENSG00000115934.11 4.104380 4.104380 4.104380 4.104380 4.104380
ENSG00000122043.6 4.104380 4.104380 4.104380 4.104380 4.104380
ENSG00000124915.6 4.104380 4.104380 4.104380 4.104380 4.104380
ENSG00000125514.5 4.104380 4.104380 4.104380 4.104380 4.104380
ENSG00000129816.5 4.104380 4.104380 4.104380 4.104380 4.104380
>head(colData)[,1:5] #sample information for each column of the two input matrices
sample_name status age gender site treatment
C00060 NF 48 Female Cleveland case
C00079 NF 26 Female Cleveland case
C00135 NF 55 Female Cleveland case
C00150 NF 61 Male Cleveland ctrl
C00154 NF 50 Male Cleveland case
C00176 NF 60 Female Cleveland ctrl
当我几乎没有要执行的测试 (~50) 时,我编写了这段代码。现在,我已经弄清楚了,除了我不知道如何使我的代码高效,因为在这种必须执行 14000*7600 测试的情况下使用 for 循环没有任何意义。它需要永远运行。我想知道我可以真正加快这段代码的其他替代方案。任何建议将不胜感激。
更新 1:我稍微更新了我的线性模型。现在,anova(lm1,lm2)"Pr(>F)" 给出的值与模型 2 中的交互项的值不同。
更新 2:我更新了我的线性模型,使 lm1 嵌套在 lm2 中。
谢谢,
【问题讨论】:
-
尝试使用data.table 而不是
data.frame。有关如何执行此操作的提示,请参阅注释 here。 -
谢谢@Lenwood 我不太擅长data.tables,但我会试一试。
标签: r loops for-loop data.table