How to calculate the sensitivity and specificity from a confusion matrix in R?

R ProgrammingServer Side ProgrammingProgramming

If we have a confusion matrix then the sensitivity and specificity can be calculated using confusionMatrix function of caret package. For example, if we have a contingency table named as table then we can use the code confusionMatrix(table). This will return sensitivity and specificity as well as many other metrics.

Example1

Live Demo

> x1<-sample(letters[1:4],20,replace=TRUE)
> y1<-sample(letters[1:4],20,replace=TRUE)
> table1<-table(x1,y1)
> table1

Output

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

Loading caret package:

> library(caret)

Finding sensitivity and specificity of table1:

> confusionMatrix(table1)

Confusion Matrix and Statistics

Output

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

Overall Statistics

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

Statistics by Class:

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

Example2

Live Demo

> 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

Output

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

Overall Statistics

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

Statistics by Class:

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

Example3

> 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
raja
Updated on 06-Nov-2020 12:48:06

Advertisements