【问题标题】:Set cutoff threshold when predicting in R在 R 中进行预测时设置截止阈值
【发布时间】:2020-10-07 18:19:05
【问题描述】:

我正在尝试不同的方法来分类二元问题。

我基本上对每个人都使用“预测”命令,并使用 caret 包中的confusionMatrix 来评估结果,但我找不到指定最佳截止阈值的方法(我已经发现使用roc 并提取坐标)。

例如: 我知道我的最佳截止值是 0.77,但我找不到在 predict 函数中使用它的方法,默认情况下使用 0.5。

有办法吗?

谢谢

【问题讨论】:

  • 欢迎来到 SO!你的意思是说例如 0,7 必须被预测为 1?
  • 是的!我刚刚发现可以通过在 predict(...probability=TRUE) 的结果上使用 ifelse 创建一个向量,用 as.factor 对其进行转换,然后在confusionMatrix 中使用它来“轻松”完成它。我仍然想知道在 predict 函数中是否有更简单的方法可以做到这一点,因为在我看来这将是一个有用的功能......
  • 不幸的是,这是我知道和目前使用的唯一方法,如答案所示。

标签: r classification r-caret


【解决方案1】:

如果我理解得很好,你可以试试:

# a model with a famous dataset
model <- glm(formula= vs ~ wt + disp, data=mtcars, family=binomial)

# let's predict the same data: use type response to have probability as resulthere you decide the cutoff and put as factor, in one line
pred_ <- as.factor(ifelse(predict(model, mtcars, type="response")>0.7,"1","0"))

# here we go!
confusionMatrix(pred_, as.factor(mtcars$vs))

    Confusion Matrix and Statistics

          Reference
Prediction  0  1
         0 16  3
         1  2 11

               Accuracy : 0.8438          
                 95% CI : (0.6721, 0.9472)
    No Information Rate : 0.5625          
    P-Value [Acc > NIR] : 0.000738        

                  Kappa : 0.68            

 Mcnemar's Test P-Value : 1.000000        

            Sensitivity : 0.8889          
            Specificity : 0.7857          
         Pos Pred Value : 0.8421          
         Neg Pred Value : 0.8462          
             Prevalence : 0.5625          
         Detection Rate : 0.5000          
   Detection Prevalence : 0.5938          
      Balanced Accuracy : 0.8373          

       'Positive' Class : 0 

【讨论】:

  • 好吧,我想这就是它的完成方式!我认为将该功能融入预测功能会很好,但毕竟这样做并没有那么麻烦。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-26
相关资源
最近更新 更多