Reputation: 2289
I'm trying to create a one table in sweave r, but it does not come out as I want. This is the code I have.
<<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")
@
Upvotes: 1
Views: 1465
Reputation: 886
I came across the same problem and found the following workaround:
1) First you must convert the confusion matrix from class table to dataframe with as.data.frame.matrix().
ConfMat <- as.data.frame.matrix(Con.Mat$table)
2) Then, you can force Stargazer to produce the output as a data frame, with summary = FALSE argument.
stargazer(ConfMat, head = FALSE, title = "Table", summary = FALSE)
Later, you can add columns or row total by creating them in the dataframe. Also, add inbetween rows with percentages.
Hope it helps!
Upvotes: 1
Reputation: 2289
The output of R with the confusionMatrix () function is as follows
> 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
While using stargazer enter image description here
Upvotes: 1