简单
set.seed(42)
my_reference=sample(c("t","r","tr"),20, replace = TRUE)
my_data= sample(c("t","r"),20, replace = TRUE)
# create new ref
my_ref2=my_reference
# convert tr in ref to be the same with prediction
my_ref2[my_reference=="tr"]=my_data[my_reference=="tr"]
caret::confusionMatrix(factor(my_data,c("t","r")),reference=factor(my_ref2,c("t","r")))
#> Confusion Matrix and Statistics
#>
#> Reference
#> Prediction t r
#> t 7 1
#> r 5 7
#>
#> Accuracy : 0.7
#> 95% CI : (0.4572, 0.8811)
#> No Information Rate : 0.6
#> P-Value [Acc > NIR] : 0.2500
#>
#> Kappa : 0.4231
#>
#> Mcnemar's Test P-Value : 0.2207
#>
#> Sensitivity : 0.5833
#> Specificity : 0.8750
#> Pos Pred Value : 0.8750
#> Neg Pred Value : 0.5833
#> Prevalence : 0.6000
#> Detection Rate : 0.3500
#> Detection Prevalence : 0.4000
#> Balanced Accuracy : 0.7292
#>
#> 'Positive' Class : t
#>
<sup>Created on 2020-11-18 by the reprex package (v0.3.0.9000)</sup>