precision, recall and f-measure in R

Fanny picture Fanny · Sep 24, 2012 · Viewed 28.1k times · Source

I haven't used R in a while, so maybe I'm just not used to it yet, but.. I have a table in R with two colums, the first one has predicted values (a value can be either 0 or 1), the second one has the actual values (also 0 or 1). I need to find recall, precision and f-measures, but cannot find a good function for it in R. (I also read about ROCR, but all I could do was creating some plots, but I really don't need plots, I need the numbers).

Is there any good functions for finding precision, recall and f-measure in R? Are there any different ways to do it?

Answer

JACKY Li picture JACKY Li · Sep 25, 2012

First I create a data set as

> predict <- sample(c(0, 1), 20, replace=T)
> true <- sample(c(0, 1), 20, replace=T)

I suppose those 1's in the predicted values are the retrieved. The total number of retrieved is

> retrieved <- sum(predict)

Precision which is the fraction of retrieved instances that are relevant, is

> precision <- sum(predict & true) / retrieved

Recall which is the fraction of relevant instances that are retrieved, is

> recall <- sum(predict & true) / sum(true)

F-measure is 2 * precision * recall / (precision + recall) is

> Fmeasure <- 2 * precision * recall / (precision + recall)