如何從 R 中的混淆矩陣計算敏感性和特異性?
如果我們有一個混淆矩陣,那麼可以使用 caret 包的 confusionMatrix 函式來計算敏感性和特異性。例如,如果我們有一個名為 table 的列聯表,則可以使用程式碼 confusionMatrix(table)。這將返回敏感性和特異性以及許多其他指標。
示例 1
> 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
正在載入 caret 包
> library(caret)
正在查詢 table1 的敏感性和特異性
> 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
示例 2
> 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
示例 3
> 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 StatisticsFemale 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP