2

我有一个逻辑回归模型,我想绘制 ROC 曲线。所有变量都有一些缺失的数据。这是摘要:

X<-cbind(outcome, var1, var2)
summary(X)
#    outcome            var1             var2      
# Min.   :0.0000   Min.   : 0.100   Min.   : 65.1  
# 1st Qu.:0.0000   1st Qu.: 0.600   1st Qu.: 91.9  
# Median :0.0000   Median : 1.000   Median :101.0  
# Mean   :0.2643   Mean   : 2.421   Mean   :110.3  
# 3rd Qu.:1.0000   3rd Qu.: 2.200   3rd Qu.:114.5  
# Max.   :1.0000   Max.   :34.800   Max.   :388.4  
# NA's   :165      NA's   :80       NA's   :30    

该模型似乎有效:

model <- glm(outcome~var1+var2,family=binomial)
summary(model)
# Call:
# glm(formula = outcome ~ var1 + var2, family = binomial)
# 
# Deviance Residuals: 
#      Min        1Q    Median        3Q       Max  
# -1.63470  -0.67079  -0.56255   0.01727   2.07577  
# 
# Coefficients:
#              Estimate Std. Error z value Pr(>|z|)    
# (Intercept) -3.652208   0.973013  -3.754 0.000174 ***
# var1         0.386811   0.147054   2.630 0.008528 ** 
# var2         0.016165   0.008075   2.002 0.045316 *  
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# (Dispersion parameter for binomial family taken to be 1)
# 
#     Null deviance: 135.91  on 117  degrees of freedom
# Residual deviance: 108.84  on 115  degrees of freedom
#   (187 observations deleted due to missingness)
# AIC: 114.84
# 
# Number of Fisher Scoring iterations: 6

但是当我尝试计算 ROC 曲线时,出现错误:

library(pROC)
roc(model)
# Error in roc.default(model) : No valid data provided.

我认为这可能是由于缺少数据,我尝试添加 na.action = na.exclude 选项,但问题仍然存在:

model2 <- glm(outcome~var1+var2,family=binomial, na.action = na.exclude)
roc(model2)
# Error in roc.default(model2) : No valid data provided.

我也尝试使用 lrm 而不是 glm,但仍然无法正常工作:

model.lrm<-lrm(outcome~var1+var2, options(na.action="na.delete"), x=TRUE, y=TRUE)
model.lrm
# Frequencies of Missing Values Due to Each Variable
# outcome    var1    var2 
#     165      80      30 
# 
# Logistic Regression Model
#  
#  lrm(formula = outcome ~ var1 + var2, data = options(na.action = "na.delete"), 
#      x = TRUE, y = TRUE)
#  
#  
#                         Model Likelihood    Discrimination    Rank Discrim.    
#                               Ratio Test           Indexes          Indexes    
#  Obs           118    LR chi2      27.07    R2       0.300    C       0.782    
#   0             87    d.f.             2    g        1.377    Dxy     0.565    
#   1             31    Pr(> chi2) <0.0001    gr       3.964    gamma   0.565    
#  max |deriv| 7e-05                          gp       0.189    tau-a   0.221    
#                                             Brier    0.150                     
 
#            Coef    S.E.   Wald Z Pr(>|Z|)
#  Intercept -3.6522 0.9730 -3.75  0.0002  
#  var1       0.3868 0.1471  2.63  0.0085  
#  var2       0.0162 0.0081  2.00  0.0453  
#  
roc(model.lrm)
# Error in roc.default(model.lrm) : No valid data provided.

以下是前 20 个观察结果:

> dput(head (dati[, c(2,3,4)], 20))
structure(list(outcome = c(NA, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 
0, 0, 1, 0, 1, 1, 0, NA, 0), var1 = c(NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 0.3, 0.5, 1.5, 4.5, 2, 2.2, 0.7, NA, NA, 0.3), 
    var2 = c(117, 84, NA, 90, 91, 113, 88, NA, 108, 178, 100, 
    86, 86, 95, 92, 111, 103, 81, NA, 95)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

问题是什么?

4

2 回答 2

1

ROC 曲线不是建立在模型上,而是建立在从模型得出的预测上。因此,您需要使用该predict函数来获得对数据的预测。它看起来像这样:

predictions <- predict(model)

然后你可以roc用这些调用函数:

roc(outcome, predictions)

缺失值将被自动忽略。

如果您使用的是测试集,这将使其变得简单且非常相似:

test_predictions <- predict(model, newdata = test_data)
roc(test_data$outcome, test_predictions)
于 2021-01-06T10:26:36.573 回答
0

我找到了一个修改代码的解决方案,如下所示:

roc(outcome, as.vector(fitted.values(model)),plot=TRUE)
于 2021-01-05T15:12:03.187 回答