glm用于R中的多个变量

时间:2019-05-07 18:06:07

标签: r loops apply glm

我想为我的snps数组建模。我可以使用以下代码一一完成。

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)))

如何使用循环或应用对所有其他snps进行此操作? snps是rs6693065_D,rs6693065_A和数百个。从上面的代码中,仅“ rs6693065_D”将被所有其他snps替换。 最好的祝福 Zillur

1 个答案:

答案 0 :(得分:0)

考虑开发一种通用方法来处理任何 snps 。然后使用lapplysapply迭代调用每个 snps 列:

# 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)

在出现tryCatch之类的错误时使用relevel

# BUILD LIST OF FUNCTION OUTPUT 
glm_list <- lapply(Data[3:426], function(col) 
                   tryCatch(proc_glm(col), error = function(e) e))

要构建数据框,请先调整方法并调用lapply,再进行do.call + rbind

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)