无法使用confusionMatrix显示灵敏度/特异性

时间:2016-01-24 18:43:50

标签: r confusion-matrix

我想要使用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)

我想用confusionMatrix来分析敏感度和特异性的输出,但它不起作用:

confusionMatrix(table(value>600,genotype))

如果我做错了什么想法?

1 个答案:

答案 0 :(得分:4)

如果你看看你的桌子,你会发现它的格式不正确。行和列标签应该相同,但在这种情况下不一样。

tab = table(value>600,genotype)

tab

       genotype
         A  B
  FALSE 83  6
  TRUE  17 94

当我们运行confusionMatrix时,由于不同的行和列标签(因为错误消息告诉您),我们会收到错误:

confusionMatrix(tab)
Error in !all.equal(rownames(data), colnames(data)) : 
  invalid argument type

通常,要创建混淆矩阵,您应该有一列预测标签和一列参考标签(真值),所以我不确定您创建的表是否有意义作为混淆矩阵。在任何情况下,只是为了显示表格的正确格式,让我们将行标签更改为与列标签相同。然后该功能将起作用:

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