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