## S3 method for class 'numeric'
mape(actual, predicted, ...)
## S3 method for class 'numeric'
weighted.mape(actual, predicted, w, ...)
mape(...)
weighted.mape(...)
mean absolute percentage error
mape.numeric | R Documentation |
Description
The mape()
-function computes the mean absolute percentage error between the observed and predicted <numeric>
vectors. The weighted.mape()
function computes the weighted mean absolute percentage error.
Usage
Arguments
actual
|
A |
predicted
|
A |
…
|
Arguments passed into other methods. |
w
|
A |
Value
A <numeric>
vector of length 1.
Calculation
The metric is calculated as,
\[ \frac{1}{n} \sum_i^n \frac{|y_i - \upsilon_i|}{|y_i|} \]
Examples
# 1) fit a linear
# regression
<- lm(
model ~ .,
mpg data = mtcars
)
# 1.1) define actual
# and predicted values
# to measure performance
<- mtcars$mpg
actual <- fitted(model)
predicted
# 2) evaluate in-sample model
# performance using Mean Absolute Percentage Error (MAPE)
cat(
"Mean Absolute Percentage Error", mape(
actual = actual,
predicted = predicted,
),"Mean Absolute Percentage Error (weighted)", weighted.mape(
actual = actual,
predicted = predicted,
w = mtcars$mpg/mean(mtcars$mpg)
),sep = "\n"
)