[R] calculating percent error from 2 vectors

(Ted Harding) ted.harding at nessie.mcc.ac.uk
Thu Jul 12 21:15:37 CEST 2007


On 12-Jul-07 17:32:03, Scott Bearer wrote:
> Hello,
> 
> I believe this is an easy scripting problem, but one I am stumbling on.
> 
> I have a "known" vector of 3 colors with nrow=10:
> known<-c("red", "blue", "red", "red", "yellow", "blue", "yellow",
> "blue",
> "blue", "yellow")
> 
> and a model output vector:
> modelout<-c("red", "red", "red", "blue", "yellow", "blue", "blue",
> "red",
> "blue", "yellow")
> 
> I would like to determine the proportion (in)correctly identified for
> each
> color.  In other words:
> % correct "red"=
> % correct "blue"=
> % correct "yellow"=
> 
> How would I code this (assuming the actual dataset is more complex)?

For your example:

> tbl<-table(known,modelout)

> tbl
        modelout
known    blue red yellow
  blue   2    2   0     
  red    1    2   0     
  yellow 1    0   2     

> dim(tbl)
[1] 3 3

> for(i in (1:dim(tbl)[1])){print(sum(tbl[i,-i])/sum(tbl[i,]))}
[1] 0.5
[1] 0.3333333
[1] 0.3333333

and you can modify the "print" command produce a desired format,
e.g. using rownames(tbl)[i] for the successive colour names.

Hoping this helps (as a start),
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 12-Jul-07                                       Time: 20:15:34
------------------------------ XFMail ------------------------------



More information about the R-help mailing list