prepare package for yearly report
[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}
4f3fdbb8
BA
7#' @param predict_from First time step to consider (>= predict_from used in
8#' \code{computeForecast()})
9#' @param horizon Horizon where to compute the error (<= horizon used in
10#' \code{computeForecast})
3d69ff21 11#'
102bcfda
BA
12#' @return A list (abs,MAPE) of lists (day,indices). The "indices" slots contain series
13#' of size L where L is the number of predicted days; i-th value is the averaged error
14#' (absolute or MAPE) on day i. The "day" slots contain curves of errors, for each time
15#' step, averaged on the L forecasting days.
3d69ff21
BA
16#'
17#' @export
d2ab47a7 18computeError = function(data, pred, predict_from, horizon=length(data$getSerie(1)))
3d69ff21 19{
c8ef2ddb 20 L = pred$getSize()
1e8327df
BA
21 mape_day = rep(0, horizon-predict_from+1)
22 abs_day = rep(0, horizon-predict_from+1)
3d69ff21
BA
23 mape_indices = rep(NA, L)
24 abs_indices = rep(NA, L)
25
26 nb_no_NA_data = 0
27 for (i in seq_len(L))
28 {
c8ef2ddb 29 index = pred$getIndexInData(i)
d2ab47a7
BA
30 serie = data$getSerie(index)[predict_from:horizon]
31 forecast = pred$getForecast(i)[predict_from:horizon]
72b9c501 32 if (!any(is.na(serie)) && !any(is.na(forecast)))
3d69ff21
BA
33 {
34 nb_no_NA_data = nb_no_NA_data + 1
72b9c501 35 mape_increment = abs(serie - forecast) / serie
3d69ff21
BA
36 mape_increment[is.nan(mape_increment)] = 0. # 0 / 0
37 mape_increment[!is.finite(mape_increment)] = 1. # >0 / 0
38 mape_day = mape_day + mape_increment
72b9c501 39 abs_increment = abs(serie - forecast)
3d69ff21
BA
40 abs_day = abs_day + abs_increment
41 mape_indices[i] = mean(mape_increment)
42 abs_indices[i] = mean(abs_increment)
43 }
44 }
45
46 list(
47 "abs" = list(
48 "day" = abs_day / nb_no_NA_data,
49 "indices" = abs_indices),
50 "MAPE" = list(
51 "day" = mape_day / nb_no_NA_data,
52 "indices" = mape_indices) )
53}