recall: Calculate recall, precision and F values

View source: R/prec_rec.R

recallR Documentation

Calculate recall, precision and F values

Description

These functions calculate the recall, precision or F values of a measurement system for finding/retrieving relevant documents compared to reference results (the truth regarding relevance). The measurement and "truth" data must have the same two possible outcomes and one of the outcomes must be thought of as a "relevant" results.

Usage

recall(data, ...)

## S3 method for class 'table'
recall(data, relevant = rownames(data)[1], ...)

## Default S3 method:
recall(data, reference, relevant = levels(reference)[1], na.rm = TRUE, ...)

precision(data, ...)

## Default S3 method:
precision(data, reference, relevant = levels(reference)[1], na.rm = TRUE, ...)

## S3 method for class 'table'
precision(data, relevant = rownames(data)[1], ...)

F_meas(data, ...)

## Default S3 method:
F_meas(
  data,
  reference,
  relevant = levels(reference)[1],
  beta = 1,
  na.rm = TRUE,
  ...
)

## S3 method for class 'table'
F_meas(data, relevant = rownames(data)[1], beta = 1, ...)

Arguments

data

for the default functions, a factor containing the discrete measurements. For the table function, a table.

...

not currently used

relevant

a character string that defines the factor level corresponding to the "relevant" results

reference

a factor containing the reference values (i.e. truth)

na.rm

a logical value indicating whether NA values should be stripped before the computation proceeds

beta

a numeric value used to weight precision and recall. A value of 1 is traditionally used and corresponds to the harmonic mean of the two values but other values weight recall beta times more important than precision.

Details

The recall (aka sensitivity) is defined as the proportion of relevant results out of the number of samples which were actually relevant. When there are no relevant results, recall is not defined and a value of NA is returned.

The precision is percentage of predicted truly relevant results of the total number of predicted relevant results and characterizes the "purity in retrieval performance" (Buckland and Gey, 1994)

The measure "F" is a combination of precision and recall (see below).

Suppose a 2x2 table with notation

Reference
Predicted relevant Irrelevant
relevant A B
Irrelevant C D

The formulas used here are:

recall = A/(A+C)

precision = A/(A+B)

F_i = (1+i^2)*prec*recall/((i^2 * precision)+recall)

See the references for discussions of the statistics.

Value

A number between 0 and 1 (or NA).

Author(s)

Max Kuhn

References

Kuhn, M. (2008), “Building predictive models in R using the caret package, ” Journal of Statistical Software, (\Sexpr[results=rd]{tools:::Rd_expr_doi("10.18637/jss.v028.i05")}).

Buckland, M., & Gey, F. (1994). The relationship between Recall and Precision. Journal of the American Society for Information Science, 45(1), 12-19.

Powers, D. (2007). Evaluation: From Precision, Recall and F Factor to ROC, Informedness, Markedness and Correlation. Technical Report SIE-07-001, Flinders University

See Also

confusionMatrix

Examples


###################
## Data in Table 2 of Powers (2007)

lvs <- c("Relevant", "Irrelevant")
tbl_2_1_pred <- factor(rep(lvs, times = c(42, 58)), levels = lvs)
tbl_2_1_truth <- factor(c(rep(lvs, times = c(30, 12)),
                          rep(lvs, times = c(30, 28))),
                        levels = lvs)
tbl_2_1 <- table(tbl_2_1_pred, tbl_2_1_truth)

precision(tbl_2_1)
precision(data = tbl_2_1_pred, reference = tbl_2_1_truth, relevant = "Relevant")
recall(tbl_2_1)
recall(data = tbl_2_1_pred, reference = tbl_2_1_truth, relevant = "Relevant")


tbl_2_2_pred <- factor(rep(lvs, times = c(76, 24)), levels = lvs)
tbl_2_2_truth <- factor(c(rep(lvs, times = c(56, 20)),
                          rep(lvs, times = c(12, 12))),
                        levels = lvs)
tbl_2_2 <- table(tbl_2_2_pred, tbl_2_2_truth)

precision(tbl_2_2)
precision(data = tbl_2_2_pred, reference = tbl_2_2_truth, relevant = "Relevant")
recall(tbl_2_2)
recall(data = tbl_2_2_pred, reference = tbl_2_2_truth, relevant = "Relevant")


caret documentation built on March 31, 2023, 9:49 p.m.