Michael,
Thank you very much. My code is certainly put to shame by yours. I promise to
read about factor to see how you use it and why ;-))
I really appreciate your help.
Monica> Subject: RE: [R] confusion matrix - better code?> Date: Fri, 7 Sep 2007
15:36:00 -0500> From: michael.conklin@markettools.com> To:
pisicandru@hotmail.com> > How about this?> > >
tab<-table(factor(tr,levels=unique(tr)),factor(pr,levels=unique(tr)))>
> tab<-rbind(tab,colSums(tab))> >
tab<-cbind(tab,rowSums(tab))> > tab> 1 2 3 4 > 1 2 1 0 0 3> 2
2 1 0 0 3> 3 0 0 3 0 3> 4 0 1 0 0 1> 4 3 3 0 10> >> > Of
course you can add some dimnames for the 5th row and 5th column if> you
want.> > dimnames(tab)[[1]][5]<-"Total">
dimnames(tab)[[2]][5]<-"Total"> > tab> 1 2 3 4 Total> 1
2 1 0 0 3> 2 2 1 0 0 3> 3 0 0 3 0 3> 4 0 1 0 0 1> Total 4 3 3 0
10> > Michael Conklin> > Chief Methodologist - Advanced
Analytics> > > > MarketTools, Inc.> > 6465 Wayzata Blvd. Suite
170> > Minneapolis, MN 55426 > > Tel: 952.417.4719 |
Mobile:612.201.8978> > Michael.Conklin@markettools.com> > > >
MarketTools(r) http://www.markettools.com> > > > This e-mail and any
attachments may contain privileged, confidential or> proprietary information.
If you are not the intended recipient, be aware> that any review, copying, or
distribution of this e-mail or any> attachment is strictly prohibited. If you
have received this e-mail in> error, please return it to the sender
immediately, and permanently> delete the original and any copies from your
system. Thank you for your> cooperation.> > > > -----Original
Message-----> From: r-help-bounces@stat.math.ethz.ch>
[mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Monica Pisica> Sent:
Friday, September 07, 2007 2:53 PM> To: r-help@stat.math.ethz.ch> Subject:
[R] confusion matrix - better code?> Importance: High> > > Hi,>
> I've written some code to obtain a confusion matrix when the true>
classification and the predicted classification are known. Suppose true>
classification is called "tr" and predicted classification is
"pr". I> have 4 classes in tr, but only 3 classes out of 4 are
predicted in "pr".> Following is my code, but looks quite
"clunky" to me. I wonder if you> have any suggestions to improve
it.> > Thanks,> > Monica> > ----------------------------->
> tr <- c(1,2,2,3,3,3,2,4,1,1)> pr<-c(1,2,1,3,3,3,1,2,1,2)> dat
<- data.frame(tr, pr)> class <- c(1:length(tr))> m <-
max(c(length(unique(tr)), length(unique(pr))))> for(i in 1:length(class))
{> class[i] <- sub(' ','',paste(dat[i,1],dat[i,2])) }>
dat <- data.frame(dat, class)> mat <- matrix(0, nrow=m, ncol=m)> for
(i in 1:m){> for (j in 1:m){> mat[i,j] <- sub('
','',paste(i,j))> }}> cat <- matrix(0, nrow=(m+1),
ncol=(m+1))> for (i in 1:m){> for(j in 1:m){> cat[i,j]<-
nrow(dat[dat$class==mat[i,j],])> }}> for (i in 1:m){>
cat[(m+1),i]<-sum(cat[1:m,i])> cat[i,(m+1)]<- sum(cat[i,1:m])>
cat[(m+1),(m+1)] <- sum(cat[1:m,(m+1)])> }> cat> [,1] [,2] [,3] [,4]
[,5]> [1,] 2 1 0 0 3> [2,] 2 1 0 0 3> [3,] 0 0 3 0 3> [4,] 0 1 0 0
1> [5,] 4 3 3 0 10> > The 5th row / col represents the sum on each row
/ col respectively.>
_________________________________________________________________> Gear up
for Halo(r) 3 with free downloads and an exclusive offer. It's> our way
of saying thanks for using Windows Live(tm).> > [[alternative HTML version
deleted]]>
_________________________________________________________________
é.
[[alternative HTML version deleted]]