在R中进行预测时设置截止阈值

问题描述 投票:-1回答:1

我正在尝试使用不同的方法对二进制问题进行分类。

我基本上对每个命令都使用“ predict”命令,并使用脱字符号包中的confusionMatrix来评估结果,但是我只是找不到一种方法来指定最佳截止阈值(我已经使用roc找到了该方法)并提取坐标)。

例如:我知道最好的临界值为0.77,但是我找不到在预测函数中使用它的方法,该函数默认使用0.5。

有办法吗?

谢谢

r classification r-caret
1个回答
0
投票

如果我了解得很好,可以尝试以下方法:

# 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 
© www.soinside.com 2019 - 2024. All rights reserved.