将confusionMatrix的输出保存为.csv表

时间:2016-01-17 19:45:40

标签: r csv export-to-csv confusion-matrix

我有以下代码,产生类似于表的输出

 lvs <- c("normal", "abnormal")
 truth <- factor(rep(lvs, times = c(86, 258)),
                 levels = rev(lvs))
 pred <- factor(
                c(
                  rep(lvs, times = c(54, 32)),
                  rep(lvs, times = c(27, 231))),               
                levels = rev(lvs))

 xtab <- table(pred, truth)

 library(caret)
 confusionMatrix(xtab)

 confusionMatrix(pred, truth)
 confusionMatrix(xtab, prevalence = 0.25)   

我想将输出的下方部分导出为.csv

               Accuracy : 0.8285          
                 95% CI : (0.7844, 0.8668)
    No Information Rate : 0.75            
    P-Value [Acc > NIR] : 0.0003097       

                  Kappa : 0.5336          
 Mcnemar's Test P-Value : 0.6025370       

            Sensitivity : 0.8953          
            Specificity : 0.6279          
         Pos Pred Value : 0.8783          
         Neg Pred Value : 0.6667          
             Prevalence : 0.7500          
         Detection Rate : 0.6715          
   Detection Prevalence : 0.7645          
      Balanced Accuracy : 0.7616  

尝试将其写为.csv表会导致错误消息:

write.csv(confusionMatrix(xtab),file="file.csv")
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
cannot coerce class ""confusionMatrix"" to a data.frame

出于显而易见的原因,手动完成整个工作是不切实际的,容易出现人为错误。

有关如何将其导出为.csv的任何建议吗?

5 个答案:

答案 0 :(得分:3)

好的,所以如果你检查confusionMatrix(xtab, prevalence = 0.25)的输出,它就是一个列表:

cm <- confusionMatrix(pred, truth)
str(cm)

    List of 5
 $ positive: chr "abnormal"
 $ table   : 'table' int [1:2, 1:2] 231 27 32 54
  ..- attr(*, "dimnames")=List of 2
  .. ..$ Prediction: chr [1:2] "abnormal" "normal"
  .. ..$ Reference : chr [1:2] "abnormal" "normal"
 $ overall : Named num [1:7] 0.828 0.534 0.784 0.867 0.75 ...
  ..- attr(*, "names")= chr [1:7] "Accuracy" "Kappa" "AccuracyLower" "AccuracyUpper" ...
 $ byClass : Named num [1:8] 0.895 0.628 0.878 0.667 0.75 ...
  ..- attr(*, "names")= chr [1:8] "Sensitivity" "Specificity" "Pos Pred Value" "Neg Pred Value" ...
 $ dots    : list()
 - attr(*, "class")= chr "confusionMatrix"

从此处开始,选择要从中创建csv的相应对象,并创建一个data.frame,其中包含每个变量的列。在您的情况下,这将是:

tocsv <- data.frame(cbind(t(cm$overall),t(cm$byClass)))

# You can then use
write.csv(tocsv,file="file.csv")

答案 1 :(得分:1)

使用插入符号包

results <- confusionMatrix(pred, truth)

as.table(results)给出了

         Reference
Prediction  X1  X0
        X1  36  29
        X0 218 727

as.matrix(results,what="overall")给出了

Accuracy       7.554455e-01
Kappa          1.372895e-01
AccuracyLower  7.277208e-01
AccuracyUpper  7.816725e-01
AccuracyNull   7.485149e-01
AccuracyPValue 3.203599e-01
McnemarPValue  5.608817e-33

和     as.matrix(results, what = "classes")给出了

Sensitivity          0.8953488
Specificity          0.6279070
Pos Pred Value       0.8783270
Neg Pred Value       0.6666667
Precision            0.8783270
Recall               0.8953488
F1                   0.8867562
Prevalence           0.7500000
Detection Rate       0.6715116
Detection Prevalence 0.7645349
Balanced Accuracy    0.7616279

使用这些和write.csv命令可以获得整个confusionMatrix信息

答案 2 :(得分:0)

绝对简单的解决方案是使用readr::write_rds进行写出。您可以在导出和导入所有内容的同时保持confusionMatrix结构的完整性。

答案 3 :(得分:0)

如果Acaret::confusionMatrix对象,则:

broom::tidy(A) %>% writexl::write_xlsx("mymatrix.xlsx")


可以将writexl替换为write.csv()

还要在单独的表格中包含表格:

broom::tidy(A) %>% list(as.data.frame(A$table)) %>% writexl::write_xlsx("mymatrix.xlsx")

答案 4 :(得分:0)

我发现capture.output最适合我。

它只是将您的输出复制为.csv文件

(您也可以将其保存为.txt)

capture.output(
confusionMatrix(xtab, prevalence = 0.25),
 file = "F:/Home Office/result.csv")