Skip to contents

It estimates the error rate for a nominal/categorical predicted-observed dataset.


error_rate(data = NULL, obs, pred, tidy = FALSE, na.rm = TRUE)



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


Vector with observed values (character | factor).


Vector with predicted values (character | factor).


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


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


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


The error rate represents the opposite of accuracy, referring to a measure of the degree to which the predictions miss-classify the reality. The classification error_rate is calculated as the ratio between the number of incorrectly classified objects with respect to the total number of objects. It is bounded between 0 and 1. The closer to 1 the worse. Values towards zero indicate low error_rate of predictions. It can be also expressed as percentage if multiplied by 100. It is estimated at a global level (not at the class level). The error rate is directly related to the accuracy, since error_rate = 1 – accuracy' . For the formula and more details, see online-documentation


(2017) Accuracy. In: Sammut C., Webb G.I. (eds) Encyclopedia of Machine Learning and Data Mining Springer, Boston, MA. doi:10.1007/978-1-4899-7687-1_3


# \donttest{
# 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 error_rate estimate for two-class case
error_rate(data = binomial_case, obs = labels, pred = predictions, tidy = TRUE)
#>   error_rate
#> 1       0.51

# Get error_rate estimate for multi-class case
error_rate(data = multinomial_case, obs = labels, pred = predictions, tidy = TRUE)
#>   error_rate
#> 1       0.71
# }