I have a following table that I want to analyze using confusionMatrix
:
value<-cbind(c(rnorm(100,500,90),rnorm(100,800,120)))
genotype<-cbind(c(rep("A",100),rep("B",100)))
df<-cbind(value,genotype)
df<-as.data.frame(df)
colnames(df)<-c("value","genotype")
df$value<-as.numeric(as.character(df$value))
table(value>600,genotype)
i want to analyze the output for sensitivities and specificities with the confusionMatrix
but it does not work:
confusionMatrix(table(value>600,genotype))
Any thoughts if I'm doing something wrong?
If you look at your table, you'll see that it's not in the right format. The row and column labels should be the same, but they aren't in this case.
tab = table(value>600,genotype)
tab
genotype
A B
FALSE 83 6
TRUE 17 94
When we run confusionMatrix
, we therefore get an error due to the different row and column labels (that's what the error message is telling you):
confusionMatrix(tab)
Error in !all.equal(rownames(data), colnames(data)) : invalid argument type
Normally, to create the confusion matrix, you should have a column of predicted labels and a column of reference labels (the true values), so I'm not sure the table you've created is meaningful as a confusion matrix. In any case, just to show the right formatting for the table, let's change the row labels to be the same as the column labels. Then the function will work:
dimnames(tab)[[1]] = c("A","B")
tab
genotype
A B
A 83 6
B 17 94
confusionMatrix(tab)
Confusion Matrix and Statistics genotype A B A 83 6 B 17 94 Accuracy : 0.885 95% CI : (0.8325, 0.9257) No Information Rate : 0.5 P-Value [Acc > NIR] : < 2e-16 Kappa : 0.77 Mcnemar's Test P-Value : 0.03706 Sensitivity : 0.8300 Specificity : 0.9400 Pos Pred Value : 0.9326 Neg Pred Value : 0.8468 Prevalence : 0.5000 Detection Rate : 0.4150 Detection Prevalence : 0.4450 Balanced Accuracy : 0.8850 'Positive' Class : A