-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
regr_mape.R
32 lines (31 loc) · 891 Bytes
/
regr_mape.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#' @title Mean Absolute Percent Error
#'
#' @details
#' The Mean Absolute Percent Error is defined as \deqn{
#' \frac{1}{n} \sum_{i=1}^n w_i \left| \frac{ t_i - r_i}{t_i} \right|,
#' }{
#' weighted.mean(abs((t - r) / t), w),
#' }
#' where \eqn{w_i} are normalized sample weights.
#'
#' @details
#' This measure is undefined if any element of \eqn{t} is \eqn{0}.
#'
#' @templateVar mid mape
#' @template regr_template
#'
#' @references
#' `r format_bib("de_myttenaere_2016")`
#'
#' @inheritParams regr_params
#' @template regr_example
#' @export
mape = function(truth, response, sample_weights = NULL, na_value = NaN, ...) {
assert_regr(truth, response = response)
if (any(abs(truth) < TOL)) {
return(na_value)
}
wmean(.ape(truth, response), sample_weights)
}
#' @include measures.R
add_measure(mape, "Mean Absolute Percent Error", "regr", 0, Inf, TRUE, obs_loss = "ape")