首页 > 解决方案 > 如何在 R 中使用逻辑回归找到 c-ctatistic 或 AUROC?

问题描述

我正在运行逻辑回归以查看这些因素/变量如何影响结果(神经系统并发症)。

如何获得 c 统计量 - 也称为接收器操作特性 (AUROC) 曲线下的面积?

    NeuroLogit2 <- glm(Neurologic Complication? ~ HTN + stroke + Gender + Embol + Drain, data=Tevar.new, family=binomial)
    summary(NeuroLogit2) 

标签: rdatabasestatisticsmedicalstatistical-test

解决方案


好吧,显然我没有你的数据,所以让我们弥补一些。在这里,我们假设我们正在根据年龄和性别对人们在任何给定年份感冒的概率进行建模。我们的结果变量只是“感冒”的 1 和“没有感冒”的 0

set.seed(69)
outcome <- c(rbinom(1000, 1, seq(0.4, 0.6, length.out = 1000)),
             rbinom(1000, 1, seq(0.3, 0.5, length.out = 1000)))
sex     <- rep(c("M", "F"), each = 1000)
age     <- rep((601:1600)/20, 2)

df      <- data.frame(outcome, age, sex)

现在我们将创建模型并查看它:

my_mod  <- glm(outcome ~ age + sex, data = df, family = binomial())

summary(my_mod)
#> 
#> Call:
#> glm(formula = outcome ~ age + sex, family = binomial(), data = df)
#> 
#> Deviance Residuals: 
#>     Min       1Q   Median       3Q      Max  
#> -1.3859  -1.0993  -0.8891   1.1847   1.5319  
#> 
#> Coefficients:
#>             Estimate Std. Error z value Pr(>|z|)    
#> (Intercept) -1.20917    0.18814  -6.427 1.30e-10 ***
#> age          0.01346    0.00317   4.246 2.18e-05 ***
#> sexM         0.61000    0.09122   6.687 2.28e-11 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> (Dispersion parameter for binomial family taken to be 1)
#> 
#>     Null deviance: 2760.1  on 1999  degrees of freedom
#> Residual deviance: 2697.1  on 1997  degrees of freedom
#> AIC: 2703.1
#> 
#> Number of Fisher Scoring iterations: 4

看起来不错。老年人和男性更容易感冒。

现在假设我们想使用这个模型来预测给定年龄和性别的人在明年是否会感冒。如果我们使用predict函数 with type = "response",我们会根据年龄和性别对数据框中的每个人进行概率估计。

predictions <- predict(my_mod, type = "response")

我们可以使用这些概率来构建我们的 ROC。在这里,我将使用 pROC 包来提供帮助:

library(pROC)

roc(outcome, predictions)
#> Setting levels: control = 0, case = 1
#> Setting direction: controls < cases
#> 
#> Call:
#> roc.default(response = outcome, predictor = predictions)
#> 
#> Data: predictions in 1079 controls (outcome 0) < 921 cases (outcome 1).
#> Area under the curve: 0.6027

所以中华民国的面积是60.27%。我们可以绘制 ROC 本身来查看它的样子:

library(ggplot2)

ggroc(roc(outcome, predictions)) +
  theme_minimal() + 
  ggtitle("My ROC curve") + 
  geom_segment(aes(x = 1, xend = 0, y = 0, yend = 1), color="grey", linetype="dashed")
#> Setting levels: control = 0, case = 1
#> Setting direction: controls < cases

reprex 包于 2020-06-07 创建(v0.3.0)


推荐阅读