将confusionMatrix的输出保存为.csv表

将confusionMatrix的输出保存为.csv表,r,csv,export-to-csv,confusion-matrix,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 <- 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
表格会导致错误消息:

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

好的,因此如果您检查
ConversionMatrix(xtab,流行率=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"
cm使用插入符号包

results <- confusionMatrix(pred, truth)
as.matrix(results,what=“total”)
给出

         Reference
Prediction  X1  X0
        X1  36  29
        X0 218 727
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
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
as.matrix(results,what=“classes”)
给出

         Reference
Prediction  X1  X0
        X1  36  29
        X0 218 727
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
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命令,您可以获得整个ConversionMatrix信息

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

如果
A
插入符号::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")

我发现capture.output最适合我

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

(您也可以使用.txt文件进行操作)


你想用“键:值”的形式写的东西是什么
write.csv
需要一个符合错误的data.frame,因此您必须将结果转换为它可以接受的内容。为了澄清,您必须将
confusionMatrix
的结果,并将您需要的数据转换为data.frame。@steveb是的,我现在明白了。mroto在下面的回复中非常清楚地描述了这一点。谢谢,这非常接近。事实上,我稍微更改了您的代码以获得我想要的:
tocsv我还注意到代码更改了原始行
95%CI:(0.7844,0.8668)
,而是显示这两行:
AccuracyLower 0.7844134380
AccuracyUpper 0.8667985207
。有没有办法保持原来的方式
95%CI:(0.7844,0.8668)
?默认情况下,
按类
总体
都是命名的数字向量,不能以当前形式组合在一起。如果转置它们,则在值旁边创建名称的字符向量。至于四舍五入的问题,可能是的,但如果你有新问题的话,通常是一个好的做法,而不是把它们塞进评论中。