[R] misclassification matrix

Gregory Ryslik rsaber at comcast.net
Wed Oct 6 22:22:26 CEST 2010


Hi,

Thanks for the help! The prop.table definitely will save some time but my question is how would you actually build the "mat" matrix. 

What I have now is 1000 matrices that are nx1 which have a predicted value for each of the n observations. I want to count then how many elements are really in class 1, but were classified as 1, as 2, as 3, etc...

What I need to do is build what we have for "mat" below.

Thanks!

Greg
On Oct 6, 2010, at 1:46 PM, Tom Fletcher wrote:

> I think what you are looking for is 
> 
> ?table
> and/or 
> ?prop.table 
> 
> So, let's say you have two matrices: ACTUAL and CLASS, you can ...
> 
> table(ACTUAL, CLASS)
> 
> 
> 
> Or, diag(1-prop.table(table(ACTUAL, CLASS), 1)) to get row percentages
> and take the diagonal.
> 
> So, using your example:
> 
> # table() as above would render the following matrix
> mat <- matrix(c(100, 25, 10, 250, 100, 5, 50, 25, 40), 3)
> 
> and prop.table() renders:
> 
>> prop.table(mat, 1)
>          [,1]      [,2]      [,3]
> [1,] 0.2500000 0.6250000 0.1250000
> [2,] 0.1666667 0.6666667 0.1666667
> [3,] 0.1818182 0.0909091 0.7272727
> 
> In the above, the diagonal is the % correctly classified. 
> 
>> diag(1-prop.table(mat, 1))
> [1] 0.7500000 0.3333333 0.2727273
> 
> 
> TF
> 
> 
> 
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Gregory Ryslik
> Sent: Wednesday, October 06, 2010 10:19 AM
> To: R Help
> Subject: [R] misclassification matrix
> 
> HI Everyone,
> 
> I am working with the following situation. I have n observations and j
> possible outcomes and each one of the n observations is assigned a class
> from 1 to j.  Furthermore, this process is done m times (for some large
> m > 1000). 
> 
> What I want to do is create a misclassification matrix which tells me
> for each one of the possible classes, how many observations were
> classified correctly, and then how many were incorrectly at each level.
> Here is an example of what I mean
> 
> 	0 	1	 2	Misclassification 
> 0     100 250  50   .75
> 1	25	100	25	.333333
> 2	10	5	40   .2727273
> 
> 
> For each one of the 1 to j elements, I can use a nested for loop to
> count how many were classified as 0, are in 1, etc and then construct
> such matrix. Thus for each element I have j comparisons and then I have
> j total rows leading to an O(j^2) running time.
> 
> Any way I can avoid such a for loop and perhaps make it run a bit
> quicker? If not, any ideas then at least how to avoid the double for
> loop and make the code more aesthetically pleasing?
> 
> As always, thank you for your help!
> 
> Kind regards,
> Greg
> ______________________________________________
> 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.



More information about the R-help mailing list