[R] confusion matrix
Claudia Beleites
cbeleites at units.it
Fri Oct 8 16:17:46 CEST 2010
Gregory Ryslik wrote:
> Hi,
>
> Thank you for the help! Would this imply then that if my "answers" and
> "predicted" are both matrices, I need to first make them into factors? I was
> hoping to avoid that step...
Why are they matrices? What is the additional dimension? And: what should become
of the additional dimension? with 2d reference and prediction, do you want to
produce 3d or 4d confusion "matrices"?
> Thank you again!
You are welcome.
Claudia
>
> Kind regards, Greg On Oct 8, 2010, at 10:04 AM, Claudia Beleites wrote:
>
>> Gregory Ryslik wrote:
>>> Hi, I played with the table option but I seem to be only able to give me
>>> counts for numbers that exist. For example, if I don't have any 4's that
>>> are predicted, that number is skipped!
>> Well, you need to tell the function that there _could_ be a 4 :
>>
>>> ref <- factor (1 : 3) ref <- factor (1 : 4) pred <- factor (c (1 : 3, 1),
>>> levels = levels (ref)) ref
>> [1] 1 2 3 4 Levels: 1 2 3 4
>>> pred
>> [1] 1 2 3 1 Levels: 1 2 3 4
>>> table (ref, pred)
>> pred ref 1 2 3 4 1 1 0 0 0 2 0 1 0 0 3 0 0 1 0 4 1 0 0 0
>>
>> Claudia
>>
>>> Thanks, Greg Sent via BlackBerry by AT&T -----Original Message----- From:
>>> Claudia Beleites <cbeleites at units.it> Date: Fri, 08 Oct 2010 15:38:31 To:
>>> Gregory Ryslik<rsaber at comcast.net> Cc: R Help<r-help at r-project.org>
>>> Subject: Re: [R] confusion matrix 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
>
--
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