preval estimates the prevalence of positive cases for a nominal/categorical predicted-observed dataset.

preval_t estimates the prevalence threshold for a binary predicted-observed dataset.

## Usage

preval(
data = NULL,
obs,
pred,
atom = FALSE,
pos_level = 2,
tidy = FALSE,
na.rm = TRUE
)

preval_t(
data = NULL,
obs,
pred,
atom = FALSE,
pos_level = 2,
tidy = FALSE,
na.rm = TRUE
)

## Arguments

data

(Optional) argument to call an existing data frame containing the data.

obs

Vector with observed values (character | factor).

pred

Vector with predicted values (character | factor).

atom

Logical operator (TRUE/FALSE) to decide if the estimate is made for each class (atom = TRUE) or at a global level (atom = FALSE); Default : FALSE.

pos_level

Integer, for binary cases, indicating the order (1|2) of the level corresponding to the positive. Generally, the positive level is the second (2) since following an alpha-numeric order, the most common pairs are (Negative | Positive), (0 | 1), (FALSE | TRUE). Default : 2.

tidy

Logical operator (TRUE/FALSE) to decide the type of return. TRUE returns a data.frame, FALSE returns a list; Default : FALSE.

na.rm

Logic argument to remove rows with missing values (NA). Default is na.rm = TRUE.

## Value

an object of class numeric within a list (if tidy = FALSE) or within a data frame (if tidy = TRUE).

## Details

The prevalence measures the overall proportion of actual positives with respect to the total number of observations. Currently, it is defined for binary cases only.

The general formula is:

$$preval = \frac{positive}{positive + negative}$$

The prevalence threshold represents an point on the ROC curve (function of sensitivity (recall) and specificity) below which the precision (or PPV) dramatically drops.

$$preval_t = \frac{\sqrt{TPR * FPR} - FPR}{TPR - FPR}$$

It is bounded between 0 and 1. The closer to 1 the better. Values towards zero indicate low performance. For the formula and more details, see online-documentation

## References

Freeman, E.A., Moisen, G.G. (2008). A comparison of the performance of threshold criteria for binary classification in terms of predicted prevalence and kappa. . Ecol. Modell. 217(1-2): 45-58. doi:10.1016/j.ecolmodel.2008.05.015

Balayla, J. (2020). Prevalence threshold and the geometry of screening curves. _Plos one, 15(10):e0240215, _ doi:10.1371/journal.pone.0240215

## Examples

# \donttest{
set.seed(123)
# Two-class
binomial_case <- data.frame(labels = sample(c("True","False"), 100, replace = TRUE),
predictions = sample(c("True","False"), 100, replace = TRUE))
# Multi-class
multinomial_case <- data.frame(labels = sample(c("Red","Blue", "Green"), 100, replace = TRUE),
predictions = sample(c("Red","Blue", "Green"), 100, replace = TRUE)    )

# Get prevalence estimate for two-class case
preval(data = binomial_case, obs = labels, pred = predictions, tidy = TRUE)
#>   prev
#> 1 0.57

# Get prevalence estimate for each class for the multi-class case
preval(data = multinomial_case, obs = labels, pred = predictions, atom = TRUE)
#> \$prev
#>  Blue Green   Red
#>  0.33  0.23  0.44
#>

# }