fix computeError.R
[talweg.git] / pkg / R / computeError.R
... / ...
CommitLineData
1#' Compute error
2#'
3#' Compute the errors between forecasted and measured series.
4#'
5#' @param data Object of class \code{Data} output of \code{getData}
6#' @param pred Object of 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). 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.
14#'
15#' @export
16computeError = function(data, pred, horizon=data$getStdHorizon())
17{
18 L = pred$getSize()
19 mape_day = rep(0, horizon)
20 abs_day = rep(0, horizon)
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 {
27 index = pred$getIndexInData(i)
28 serie = data$getSerie(index+1)[1:horizon]
29 forecast = pred$getForecast(i)[1:horizon]
30 if (!any(is.na(serie)) && !any(is.na(forecast)))
31 {
32 nb_no_NA_data = nb_no_NA_data + 1
33 mape_increment = abs(serie - forecast) / serie
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
37 abs_increment = abs(serie - forecast)
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}