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@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.

Reply via email to