[英]glm for multiple variables in R
I wanted to model my snps array. 我想模拟我的snps数组。 I can do this one by one using the following code.
我可以使用以下代码逐个完成此操作。
Data$DX=as.factor(Data$DX)
univariate=glm(relevel(DX, "CON") ~ relevel(rs6693065_D,"AA"), family = binomial, data = Data)
summary(univariate)
exp(cbind(OR = coef(univariate), confint(univariate)))
How can I do this for all other snps using a loop or apply? 如何使用循环或应用所有其他snps执行此操作? The snps are rs6693065_D, rs6693065_A and hundreds of them.
snps是rs6693065_D,rs6693065_A和数百个。 From the above code only "rs6693065_D" will be replaced by all other snps.
从上面的代码中,只有“rs6693065_D”将被所有其他snps替换。 Best Regards Zillur
最诚挚的问候Zillur
Consider developing a generalized method to handle any snps . 考虑开发一种处理任何snps的通用方法。 Then call it iteratively passing every snps column using
lapply
or sapply
: 然后使用
lapply
或sapply
以迭代方式调用它来传递每个lapply
列:
# GENERALIZED METHOD
proc_glm <- function(snps) {
univariate <- glm(relevel(data$DX, "CON") ~ relevel(snps, "AA"), family = binomial)
return(exp(cbind(OR = coef(univariate), confint(univariate))))
}
# BUILD LIST OF FUNCTION OUTPUT
glm_list <- lapply(Data[3:426], proc_glm)
Use tryCatch
in case of errors like relevel
: 使用
tryCatch
在这样错误的情况下relevel
:
# BUILD LIST OF FUNCTION OUTPUT
glm_list <- lapply(Data[3:426], function(col)
tryCatch(proc_glm(col), error = function(e) e))
For building a data frame, adjust method and lapply
call followed with a do.call
+ rbind
: 要构建数据框,请使用
do.call
+ rbind
调整方法和lapply
调用:
proc_glm <- function(col){
# BUILD FORMULA BY STRING
univariate <- glm(as.formula(paste("y ~", col)), family = binomial, data = Data)
# RETURN DATA FRAME OF COLUMN AND ESTIMATES
cbind.data.frame(COL = col,
exp(cbind(OR = coef(univariate), confint(univariate)))
)
}
# BUILD LIST OF DFs, PASSING COLUMN NAMES
glm_list <- lapply(names(Data)[3:426],
tryCatch(proc_glm(col), error = function(e) NA))
# APPEND ALL DFs FOR SINGLE MASTER DF
final_df <- do.call(rbind, glm_list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.