繁体   English   中英

加快R中的许多GLM

[英]Speeding up lots of GLMs in R

首先,对冗长的帖子深表歉意。 认为最好提供上下文以获得良好答案(我希望!)。 前一段时间,我编写了一个R函数,该函数将获取数据帧中变量的所有成对交互。 当时效果不错,但现在一位同事希望我使用更大的数据集来完成此任务。 他们不知道最终会有多少个变量,但他们估计大约有2,500-3,000。 我下面的功能对此太慢了(100个变量需要4分钟)。 在这篇文章的底部,我介绍了一些用于各种变量和交互总数的时间。 我对函数的100个变量运行调用Rprof()的结果,所以如果有人想看看它,请告诉我。 我不想把超长的时间超过需要的时间。

我想知道的是我是否可以做些什么来加快此功能。 我尝试直接寻找glm.fit,但据我了解,为了使设计矩阵以及我坦率不理解的所有其他内容的计算有用,每个模型都必须相同,尽管我对此有误,但我的分析情况并非如此。

任何有关如何使此运行速度更快的想法将不胜感激。 我计划最后使用并行化来运行分析,但我不知道我将可以访问多少个CPU,但我想它不会超过8个。

预先感谢,干杯
戴维

getInteractions2 = function(data, fSNPcol, ccCol)
{
#fSNPcol is the number of the column that contains the first SNP
#ccCol is the number of the column that contains the outcome variable
  require(lmtest)
  a = data.frame()
  snps =  names(data)[-1:-(fSNPcol-1)]
  names(data)[ccCol] = "PHENOTYPE"
  terms = as.data.frame(t(combn(snps,2)))
  attach(data)

  fit1 = c()
  fit2 = c()
  pval  = c()

  for(i in 1:length(terms$V1))
  {
    fit1 = glm(PHENOTYPE~get(as.character(terms$V1[i]))+get(as.character(terms$V2[i])),family="binomial")
    fit2 = glm(PHENOTYPE~get(as.character(terms$V1[i]))+get(as.character(terms$V2[i]))+I(get(as.character(terms$V1[i]))*get(as.character(terms$V2[i]))),family="binomial")
    a  = lrtest(fit1, fit2)
    pval = c(pval, a[2,"Pr(>Chisq)"])
  }

  detach(data)
  results = cbind(terms,pval) 
  return(results)
}

下表是通过函数传递的变量数量增加时的system.time结果。 n是数量,而Ints是由该数量的变量给定的成对交互的数量。

      n   Ints     user.self sys.self elapsed
time  10   45      1.20     0.00    1.30
time  15  105      3.40     0.00    3.43
time  20  190      6.62     0.00    6.85
... 
time  90 4005    178.04     0.07  195.84
time  95 4465    199.97     0.13  218.30
time 100 4950    221.15     0.08  242.18

如果您想查看时序或Rprof()结果,可以使用一些代码来重现数据帧。 除非您的计算机非常快,或者准备等待约15至20分钟,否则请不要运行此程序。

df = data.frame(paste("sid",1:2000,sep=""),rbinom(2000,1,.5))
gtypes = matrix(nrow=2000, ncol=3000)
gtypes = apply(gtypes,2,function(x){x=sample(0:2, 2000, replace=T);x})
snps = paste("rs", 1000:3999,sep="")
df = cbind(df,gtypes)
names(df) = c("sid", "status", snps)

times = c()
for(i in seq(10,100, by=5)){
  if(i==100){Rprof()}
  time = system.time((pvals = getInteractions2(df[,1:i], 3, 2)))
  print(time)
  times = rbind(times, time)
  if(i==100){Rprof(NULL)}
}
numI = function(n){return(((n^2)-n)/2)}
timings = cbind(seq(10,100,by=5), sapply(seq(10,100,by=5), numI),times)

因此,我已经解决了这个问题(在R邮件列表的帮助下),并且将其发布,以防对任何人有用。

基本上,如果SNP或变量是独立的(即不在LD中,不相关),则可以将每个SNP / Variable居中,其含义如下:

rs1cent <- rs1-mean(rs1)
rs2cent <- rs2 -mean(rs2)

然后,您可以在筛选步骤中测试表型和相互作用之间的相关性:

rs12interaction <- rs1cent*rs2cent
cor(PHENOTYPE, rs12interaction)

然后使用完整的glm全面调查似乎相关的任何内容。 截止选择仍然是任意的。

其他建议是使用RAO评分测试,该测试仅涉及拟合原假设模型,这将此步骤的计算时间减半,但我并不十分了解其工作原理(但还需要更多阅读。)

无论如何,你去了。 也许有一天对某人有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM