[R] confusion matrix
Claudia Beleites
cbeleites at units.it
Fri Oct 8 15:38:31 CEST 2010
Dear Greg,
If it is only the NA that worries you: function table can deal with that.
? table
and:
example (table)
If you want to make a confusion matrix that works also with fractional answers
(e.g. 50% A, 50% B, a.k.a soft classification) then you can contact me and
become test user of a package that I'm just writing (you can also wait until it
is published to CRAN, but that will take a while).
Best regards,
Claudia
Gregory Ryslik wrote:
> Hi Everyone,
>
> In follow up to my previous question, I wrote some code that correctly makes a confusion matrix as I need it. However, it only works when the numbers are between 1 and n. If the possible outcomes are between 0 and n, then I can't reference row "0" of the matrix and the code breaks. Does anyone have any easy fixes for this? I've attached the entire code to this email.
>
>
> As always, thank you for your help!
>
> Greg
>
> Code:
>
> answers<-matrix(c(4,2,1,3,2,1),nrow =6)
> mat1<- matrix(c(3,3,4,NA,4,2),nrow = 6)
> mat2<-matrix(c(3,2,1,4,2,3),nrow = 6)
> mat3<-matrix(c(4,2,2,2,1,1),nrow = 6)
> mat4<-matrix(c(4,2,1,3,1,4),nrow = 6)
> mat5<-matrix(c(2,3,1,4,2,3),nrow = 6)
>
> matrixlist<- list(mat1,mat2,mat3,mat4,mat5)
> predicted.values<- matrix(unlist(matrixlist),nrow = dim(mat1)[1])
>
> confusion.matrix<-matrix(0, nrow = length(as.vector(unique(answers))),ncol = length(as.vector(unique(answers))))
>
> for(i in 1:dim(predicted.values)[1]){
> for(j in 1: dim(predicted.values)[2]){
>
> predicted.value<- predicted.values[i,j]
> if(!is.na(predicted.value)){
> true.value<- answers[i,]
> confusion.matrix[true.value, predicted.value] <- confusion.matrix[true.value,predicted.value]+1
> }
> }
> }
>
> class.error<- diag(1- prop.table(confusion.matrix,1))
> confusion.matrix<-cbind(confusion.matrix,class.error)
> confusion.data.frame<-as.data.frame(confusion.matrix)
> names(confusion.data.frame)[1:length(as.vector(unique(answers)))]<- 1:length(as.vector(unique(answers)))
> names(confusion.data.frame)[length(as.vector(unique(answers)))+1]<- "class.error"
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Claudia Beleites
Dipartimento dei Materiali e delle Risorse Naturali
Università degli Studi di Trieste
Via Alfonso Valerio 6/a
I-34127 Trieste
phone: +39 0 40 5 58-37 68
email: cbeleites at units.it
More information about the R-help
mailing list