Re: [R] confusion matrix - better code?
Wolfgang, This looks great and certainly puts to shame my code . Thanks, Monica Date: Fri, 7 Sep 2007 21:04:32 +0100 From: [EMAIL PROTECTED] To: [EMAIL PROTECTED] CC: r-help@stat.math.ethz.ch Subject: Re: [R] confusion matrix - better code? Dear Monica, try this: cm = table(tr, pr) cm pr tr 1 2 3 1 2 1 0 2 2 1 0 3 0 0 3 4 0 1 0 rowSums(cm) colSums(cm) Best wishes Wolfgang Huber Monica Pisica ha scritto: 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 10The 5th row / col represents the sum on each row / col respectively. _ Gear up for Halo® 3 with free downloads and an exclusive offer. Itâs our way of saying thanks for using Windows Liveâ¢. http://gethalo3gear.com?ocid=SeptemberWLHalo3_WLHMTxt_2 [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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.
Re: [R] confusion matrix - better code?
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: [EMAIL PROTECTED] To: [EMAIL PROTECTED] 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 [EMAIL PROTECTED] MarketTools(r) http://www.markettools.comThis 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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]] __ R-help@stat.math.ethz.ch 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.
[R] confusion matrix - better code?
Hi, Ive 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,]21003 [2,]21003 [3,]00303 [4,]01001 [5,]4330 10 The 5th row / col represents the sum on each row / col respectively. _ Gear up for Halo® 3 with free downloads and an exclusive offer. Its our way of saying thanks for using Windows Live. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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.
Re: [R] confusion matrix - better code?
Dear Monica, try this: cm = table(tr, pr) cm pr tr 1 2 3 1 2 1 0 2 2 1 0 3 0 0 3 4 0 1 0 rowSums(cm) colSums(cm) Best wishes Wolfgang Huber Monica Pisica ha scritto: 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,]21003 [2,]21003 [3,]00303 [4,]01001 [5,]4330 10 The 5th row / col represents the sum on each row / col respectively. __ R-help@stat.math.ethz.ch 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.