Loop that will run a Logistic regression across all Independent variables and present AUC and

a 夏天 提交于 2020-01-15 12:24:06

问题


I would like to run the dependent variable of a logistic regression (in my data set it's : dat$admit) with all available variables, each regression with its own Independent variable vs dependent variable. The outcome that I wanted to get back is a list of each regression summary : coeff,p-value ,AUC. Using the data set submitted below there should be 3 regressions.

Here is a sample data set (where admit is the logistic regression dependent variable) :

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

I have this function that present a list of each regression and the coef but I don't find a way to bind also AUC and p value. Here is the code:

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

Any idea how to create this list? Thanks, Ron


回答1:


Try

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


来源:https://stackoverflow.com/questions/22742887/loop-that-will-run-a-logistic-regression-across-all-independent-variables-and-pr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!