循环将在所有独立变量上运行Logistic回归并显示AUC和

时间:2014-03-30 10:30:13

标签: r logistic-regression

我想运行逻辑回归的因变量(在我的数据集中它是:dat$admit)包含所有可用变量,每个回归都有自己的独立变量和因变量。我想回来的结果是每个回归总结的列表:coeff,p-value,AUC。使用下面提交的数据集应该有3个回归。

以下是一个示例数据集(其中admit是逻辑回归因变量):

>dat <- read.table(text = " female  apcalc    admit       num
+ 0        0        0         7
+ 0        0        1         1
+ 0        1        0         3
+ 0        1        1         7
+ 1        0        0         5
+ 1        0        1         1
+ 1        1        0         0
+ 1        1        1         6",
+                   header = TRUE)

我有这个函数,它提供了每个回归和coef的列表,但是我找不到绑定AUC和p值的方法。 这是代码:

t(sapply(setdiff(names(dat),"admit"), function(x) coef(glm(reformulate(x,response="admit"), data=dat,family=binomial)))) 

知道如何创建此列表吗? 谢谢, 罗恩

1 个答案:

答案 0 :(得分:2)

尝试

library(caTools)
ResFunc <- function(x) {
  temp <- glm(reformulate(x,response="admit"), data=dat,family=binomial)
  c(summary(temp)$coefficients[,1], 
    summary(temp)$coefficients[,4],
    colAUC(predict(temp, type = "response"), dat$admit))
}

temp <- as.data.frame(t(sapply(setdiff(names(dat),"admit"), ResFunc)))
colnames(temp) <- c("Intercept", "Estimate", "P-Value (Intercept)", "P-Value (Estimate)", "AUC")
temp

#          Intercept      Estimate P-Value (Intercept) P-Value (Estimate) AUC
# female 0.000000e+00  0.000000e+00                   1                  1 0.5
# apcalc 0.000000e+00  0.000000e+00                   1                  1 0.5
# num    5.177403e-16 -1.171295e-16                   1                  1 0.5