fix 2 tests out of 3; TODO: test forecasters
[talweg.git] / pkg / R / getError.R
1 #' @title Get error
2 #'
3 #' @description Obtain the errors between forecast and data
4 #'
5 #' @param data Dataset, object of class \code{Data} output of \code{getData}
6 #' @param forecast Forecast object, class \code{Forecast} output of \code{getForecast}
7 #' @param horizon Horizon where to compute the error (<= horizon used in \code{getForecast})
8 #'
9 #' @return A list (abs,MAPE) of lists (day,indices)
10 #'
11 #' @export
12 getError = function(data, forecast, horizon=data$getStdHorizon())
13 {
14 L = forecast$getSize()
15 mape_day = rep(0, horizon)
16 abs_day = rep(0, horizon)
17 mape_indices = rep(NA, L)
18 abs_indices = rep(NA, L)
19
20 nb_no_NA_data = 0
21 for (i in seq_len(L))
22 {
23 index = forecast$getIndexInData(i)
24 serie = data$getSerie(index+1)[1:horizon]
25 pred = forecast$getSerie(i)[1:horizon]
26 if (!any(is.na(serie)) && !any(is.na(pred)))
27 {
28 nb_no_NA_data = nb_no_NA_data + 1
29 mape_increment = abs(serie - pred) / serie
30 mape_increment[is.nan(mape_increment)] = 0. # 0 / 0
31 mape_increment[!is.finite(mape_increment)] = 1. # >0 / 0
32 mape_day = mape_day + mape_increment
33 abs_increment = abs(serie - pred)
34 abs_day = abs_day + abs_increment
35 mape_indices[i] = mean(mape_increment)
36 abs_indices[i] = mean(abs_increment)
37 }
38 }
39
40 list(
41 "abs" = list(
42 "day" = abs_day / nb_no_NA_data,
43 "indices" = abs_indices),
44 "MAPE" = list(
45 "day" = mape_day / nb_no_NA_data,
46 "indices" = mape_indices) )
47 }