Search code examples
rlogistic-regression

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


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


Solution

  • 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