some last fixes; results still not good...
[talweg.git] / pkg / R / computeError.R
CommitLineData
af3b84f4 1#' Compute error
3d69ff21 2#'
102bcfda 3#' Compute the errors between forecasted and measured series.
3d69ff21 4#'
102bcfda
BA
5#' @param data Object of class \code{Data} output of \code{getData}
6#' @param pred Object of class \code{Forecast} output of \code{computeForecast}
2057c793
BA
7#' @param horizon Horizon where to compute the error
8#' (<= horizon used in \code{computeForecast})
3d69ff21 9#'
102bcfda
BA
10#' @return A list (abs,MAPE) of lists (day,indices). The "indices" slots contain series
11#' of size L where L is the number of predicted days; i-th value is the averaged error
12#' (absolute or MAPE) on day i. The "day" slots contain curves of errors, for each time
13#' step, averaged on the L forecasting days.
3d69ff21
BA
14#'
15#' @export
d2ab47a7 16computeError = function(data, pred, predict_from, horizon=length(data$getSerie(1)))
3d69ff21 17{
c8ef2ddb 18 L = pred$getSize()
1e8327df
BA
19 mape_day = rep(0, horizon-predict_from+1)
20 abs_day = rep(0, horizon-predict_from+1)
3d69ff21
BA
21 mape_indices = rep(NA, L)
22 abs_indices = rep(NA, L)
23
24 nb_no_NA_data = 0
25 for (i in seq_len(L))
26 {
c8ef2ddb 27 index = pred$getIndexInData(i)
d2ab47a7
BA
28 serie = data$getSerie(index)[predict_from:horizon]
29 forecast = pred$getForecast(i)[predict_from:horizon]
72b9c501 30 if (!any(is.na(serie)) && !any(is.na(forecast)))
3d69ff21
BA
31 {
32 nb_no_NA_data = nb_no_NA_data + 1
72b9c501 33 mape_increment = abs(serie - forecast) / serie
3d69ff21
BA
34 mape_increment[is.nan(mape_increment)] = 0. # 0 / 0
35 mape_increment[!is.finite(mape_increment)] = 1. # >0 / 0
36 mape_day = mape_day + mape_increment
72b9c501 37 abs_increment = abs(serie - forecast)
3d69ff21
BA
38 abs_day = abs_day + abs_increment
39 mape_indices[i] = mean(mape_increment)
40 abs_indices[i] = mean(abs_increment)
41 }
42 }
43
44 list(
45 "abs" = list(
46 "day" = abs_day / nb_no_NA_data,
47 "indices" = abs_indices),
48 "MAPE" = list(
49 "day" = mape_day / nb_no_NA_data,
50 "indices" = mape_indices) )
51}