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