fix computeError.R
[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
72b9c501 16computeError = function(data, pred, horizon=data$getStdHorizon())
3d69ff21 17{
c8ef2ddb 18 L = pred$getSize()
3d69ff21
BA
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 {
c8ef2ddb 27 index = pred$getIndexInData(i)
3d69ff21 28 serie = data$getSerie(index+1)[1:horizon]
72b9c501
BA
29 forecast = pred$getForecast(i)[1:horizon]
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}