本文介绍了循环将对所有自变量运行Logistic回归,并显示AUC和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用所有可用变量运行逻辑回归的因变量(在我的数据集中为dat$admit),每个回归均具有其自己的自变量与因变量.我想得到的结果是每个回归摘要的列表:coeff,p-value,AUC.使用下面提交的数据集,应该进行3个回归.

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.

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

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)

我有这个功能,可以显示每个回归和系数的列表,但是我找不到绑定AUC和p值的方法.这是代码:

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

推荐答案

尝试

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

这篇关于循环将对所有自变量运行Logistic回归,并显示AUC和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 07:23