简体   繁体   English

如何通过小标题插入插入符号:: confusionmatrix()?

[英]how to pass a tibble to caret::confusionmatrix()?

Consider this simple example: 考虑以下简单示例:

data_frame(truth = c(1,1,0,0),
           prediction = c(1,0,1,0),
           n_obs = c(100,10,90,50))
# A tibble: 4 x 3
  truth prediction n_obs
  <dbl>      <dbl> <dbl>
1     1          1   100
2     1          0    10
3     0          1    90
4     0          0    50

I would like to pass this tibble to caret::confusionMatrix so that I have all the metrics I need at once ( accuracy , recall , etc). 我想将此tibble传递给caret::confusionMatrix以便同时获得我需要的所有指标( accuracyrecall等)。

As you can see, the tibble contains all the information required to compute performance statistics. 如您所见, tibble包含计算性能统计信息所需的所有信息。 For instance, you can see that in the test dataset (not available here), there are 100 observations where the predicted label 1 matched the true label 1 . 例如,您可以看到在测试数据集中(此处不可用),有100个观察值,其中预测标签1与真实标签1相匹配。 However, 90 observations with a predicted value of 1 were actually false positives. 但是,预测值为1 90观察实际上是假阳性。

I do not want to compute all the metrics by hand, and would like to resort to caret::confusionMatrix() 我不想手动计算所有指标,而是想使用caret::confusionMatrix()

However, this has proven to be suprisingly difficult. 然而,事实证明这是非常困难的。 Calling confusionMatrix(.) on the tibble above does not work. 在上面的tibble上调用confusionMatrix(.)无效。 Is there any solution here? 这里有什么解决办法吗?

Thanks! 谢谢!

You could use the following. 您可以使用以下内容。 You have to set the positive class to 1 otherwise 0 will be taken as the positive class. 您必须将正类设置为1,否则将0用作正类。

confusionMatrix(xtabs(n_obs ~ prediction + truth , df), positive = "1")

Confusion Matrix and Statistics

          truth
prediction   0   1
         0  50  10
         1  90 100

               Accuracy : 0.6             
                 95% CI : (0.5364, 0.6612)
    No Information Rate : 0.56            
    P-Value [Acc > NIR] : 0.1128          

                  Kappa : 0.247           
 Mcnemar's Test P-Value : 2.789e-15       

            Sensitivity : 0.9091          
            Specificity : 0.3571          
         Pos Pred Value : 0.5263          
         Neg Pred Value : 0.8333          
             Prevalence : 0.4400          
         Detection Rate : 0.4000          
   Detection Prevalence : 0.7600          
      Balanced Accuracy : 0.6331          

       'Positive' Class : 1    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM