如果我们有混淆矩阵,则可以使用插入符号包装的confusionMatrix函数来计算敏感性和特异性。例如,如果我们有一个名为table的列联表,则可以使用代码confusionMatrix(table)。这将返回灵敏度和特异性以及许多其他指标。
> x1<-sample(letters[1:4],20,replace=TRUE) > y1<-sample(letters[1:4],20,replace=TRUE) > table1<-table(x1,y1) > table1
输出结果
y1 x1 a b c d a 0 0 1 0 b 0 1 2 1 c 2 2 0 2 d 3 2 1 3
加载插入符号包:
> library(caret)
查找表1的敏感性和特异性:
> confusionMatrix(table1)
混淆矩阵与统计
输出结果
y1 x1 a b c d a 0 0 1 0 b 0 1 2 1 c 2 2 0 2 d 3 2 1 3
总体统计
Accuracy : 0.2 95% CI : (0.0573, 0.4366) No Information Rate : 0.3 P-Value [Acc > NIR] : 0.8929 Kappa : -0.0774 Mcnemar's Test P-Value : NA
分类统计:
Class: a Class: b Class: c Class: d Sensitivity 0.0000 0.20 0.0000 0.5000 Specificity 0.9333 0.80 0.6250 0.5714 Pos Pred Value 0.0000 0.25 0.0000 0.3333 Neg Pred Value 0.7368 0.75 0.7143 0.7273 Prevalence 0.2500 0.25 0.2000 0.3000 Detection Rate 0.0000 0.05 0.0000 0.1500 Detection Prevalence 0.0500 0.20 0.3000 0.4500 Balanced Accuracy 0.4667 0.50 0.3125 0.5357
> x2<-sample(c("India","China","Croatia","Indonesia"),2000,replace=TRUE) > y2<-sample(c("India","China","Croatia","Indonesia"),2000,replace=TRUE) > table2<-table(x2,y2) > table2
输出结果
y2 x2 China Croatia India Indonesia China 143 131 138 118 Croatia 118 118 123 119 India 115 132 115 132 Indonesia 107 126 124 141
> confusionMatrix(table2) Confusion Matrix and Statistics
y2 x2 China Croatia India Indonesia China 143 131 138 118 Croatia 118 118 123 119 India 115 132 115 132 Indonesia 107 126 124 141
总体统计
Accuracy : 0.2585 95% CI : (0.2394, 0.2783) No Information Rate : 0.255 P-Value [Acc > NIR] : 0.3680 Kappa : 0.0116 Mcnemar's Test P-Value : 0.6665
分类统计:
Class: China Class: Croatia Class: India Class: Indonesia Sensitivity 0.2961 0.2327 0.2300 0.2765 Specificity 0.7449 0.7589 0.7473 0.7604 Pos Pred Value 0.2698 0.2469 0.2328 0.2831 Neg Pred Value 0.7687 0.7444 0.7444 0.7543 Prevalence 0.2415 0.2535 0.2500 0.2550 Detection Rate 0.0715 0.0590 0.0575 0.0705 Detection Prevalence 0.2650 0.2390 0.2470 0.2490 Balanced Accuracy 0.5205 0.4958 0.4887 0.5184
> x3<-sample(c("Male","Female"),20,replace=TRUE) > y3<-sample(c("Male","Female"),20,replace=TRUE) > df<-data.frame(x3,y3) > confusionMatrix(table(df$x3,df$y3)) Confusion Matrix and Statistics
Female Male Female 3 7 Male 6 4 Accuracy : 0.35 95% CI : (0.1539, 0.5922) No Information Rate : 0.55 P-Value [Acc > NIR] : 0.9786 Kappa : -0.3 Mcnemar's Test P-Value : 1.0000 Sensitivity : 0.3333 Specificity : 0.3636 Pos Pred Value : 0.3000 Neg Pred Value : 0.4000 Prevalence : 0.4500 Detection Rate : 0.1500 Detection Prevalence : 0.5000 Balanced Accuracy : 0.3485 'Positive' Class : Female