'update'
[talweg.git] / pkg / R / J_Neighbors.R
... / ...
CommitLineData
1#' getNeighborsJumpPredict
2#'
3#' Apply optimized weights on gaps observed on selected neighbors.
4#' This jump prediction method can only be used in conjunction with the Neighbors
5#' Forecaster, because it makes use of the optimized parameters to re-apply the weights
6#' on the jumps observed at days interfaces of the past neighbors.
7#'
8#' @inheritParams computeForecast
9#' @inheritParams getZeroJumpPredict
10#'
11#' @aliases J_Neighbors
12#'
13getNeighborsJumpPredict = function(data, today, memory, predict_from, horizon,
14 params, ...)
15{
16 first_day = max(1, today-memory)
17 filter = (params$indices >= first_day)
18 indices = params$indices[filter]
19 weights = params$weights[filter]
20
21 if (is.na(indices[1]))
22 return (NA)
23
24 gaps = sapply(indices, function(i) {
25 if (predict_from >= 2)
26 data$getSerie(i)[predict_from] - data$getSerie(i)[predict_from-1]
27 else
28 head(data$getSerie(i),1) - tail(data$getSerie(i-1),1)
29 })
30 scal_product = weights * gaps
31 norm_fact = sum( weights[!is.na(scal_product)] )
32 sum(scal_product, na.rm=TRUE) / norm_fact
33}