[英]efficient looping logistic regression in R
I'm trying to run multiple logistic regression analyses for each of ~400k predictor variables. 我正在尝试对每个~400k预测变量进行多个逻辑回归分析。 I would like to capture the outputs of each run into a row/column of an output table.
我想将每次运行的输出捕获到输出表的行/列中。
My data organised in two parts. 我的数据分为两部分。 I have a 400000 x 189 double matrix (
mydatamatrix
) that contains the observations/data for each of my 400000 predictor variables measured in 189 individuals ( P1
). 我有一个400000 x 189双矩阵(
mydatamatrix
),其中包含我在189个人( P1
)中测量的每个400000预测变量的观察/数据。 I also have a second 189 x 20 data frame ( mydataframe
) containing the outcome variable and another predictor variable ( O1
and P2
) plus 18 other variables not used in this particular analysis. 我还有第二个189 x 20数据帧(
mydataframe
),其中包含结果变量和另一个预测变量( O1
和P2
)以及此特定分析中未使用的18个其他变量。
My regression model is O1~ P1+P2
, where O1
is binary. 我的回归模型是
O1~ P1+P2
,其中O1
是二进制的。
I got the following loop to work: 我得到以下循环工作:
create output file for results 为结果创建输出文件
output<-data.frame(matrix(nrow=400000, ncol=4))
names(output)=c("Estimate", " Std. Error", " z value", " Pr(>|z|)")
run logistic regression loop for i
predictors and store output in output file 为输出文件中的
i
预测变量和存储输出运行逻辑回归循环
for (i in c(1:400000)){
result<-(glm(mydataframe$O1 ~ mydatamatrix[,i] + as.factor(mydataframe$P2),
family=binomial))
row.names(output)<-row.names(mydatamatrix)
output[i,1]<-coef(summary(result))[2,1]
output[i,2]<-coef(summary(result))[2,2]
output[i,3]<-coef(summary(result))[2,3]
output[i,4]<-coef(summary(result))[2,4]
}
However, the run time is huge (it took over an hour to output the first 20k tests). 但是,运行时间很长(输出前20k测试需要一个多小时)。 Is there a more efficient way to run this analysis?
有没有更有效的方法来运行此分析?
It will be faster if you use apply
instead of a for
loop: 如果使用
apply
而不是for
循环,它会更快:
t(apply(mydatamatrix, 2,
function(x)
coef(summary(glm(mydataframe$O1 ~ x + as.factor(mydataframe$P2),
family=binomial)))[2, 1:4]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.