2017-06-08 45 views
1

我試圖在sweave r中創建一個表格,但它並沒有按照我的意願出現。這是我的代碼。如何用stargazer和caret創建混淆矩陣

<<results='asis'>>= 
## 2 class example 
library(caret) 
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) 
Con.Mat <- confusionMatrix(xtab) 
Con.Mat$table 
Con.Mat$overall 
Con.Mat$byClass 

stargazer::stargazer(Con.Mat$table,head=FALSE,title = "Table") 
stargazer::stargazer(Con.Mat$overall,head=FALSE,title = "overall") 
stargazer::stargazer(Con.Mat$byClass,head=FALSE,title = "byClass") 
@ 
+0

這將是有益的,包括所期望的輸出,和一個以上位的解釋你想達到什麼目的,出了什麼問題。 – Bea

回答

1

在使用斯塔蓋澤 R與混淆矩陣()函數的輸出被如下

> confusionMatrix(xtab) 
Confusion Matrix and Statistics 

      truth 
pred  abnormal normal 
    abnormal  231  32 
    normal   27  54 

       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   

     'Positive' Class : abnormal 

enter image description here