'update'
[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 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})
11#'
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.
16#'
17#' @export
18computeError = function(data, pred, predict_from, horizon=length(data$getSerie(1)))
19{
20 L = pred$getSize()
21 mape_day = rep(0, horizon-predict_from+1)
22 abs_day = rep(0, horizon-predict_from+1)
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 {
29 index = pred$getIndexInData(i)
30 serie = data$getSerie(index)[predict_from:horizon]
31 forecast = pred$getForecast(i)[predict_from:horizon]
32 if (!any(is.na(serie)) && !any(is.na(forecast)))
33 {
34 nb_no_NA_data = nb_no_NA_data + 1
35 mape_increment = abs(serie - forecast) / serie
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
39 abs_increment = abs(serie - forecast)
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}