'update'
[talweg.git] / pkg / R / J_Neighbors.R
CommitLineData
102bcfda
BA
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.
3d69ff21 7#'
99f83c9a 8#' @inheritParams computeForecast
e030a6e3 9#' @inheritParams getZeroJumpPredict
546b0cb6 10#'
3ddf1c12 11#' @aliases J_Neighbors
102bcfda 12#'
d2ab47a7
BA
13getNeighborsJumpPredict = function(data, today, memory, predict_from, horizon,
14 params, ...)
3d69ff21 15{
09cf9c19 16 first_day = max(1, today-memory)
ee8b1b4e 17 filter = (params$indices >= first_day)
e030a6e3
BA
18 indices = params$indices[filter]
19 weights = params$weights[filter]
99f83c9a 20
8ab64202
BA
21 if (is.na(indices[1]))
22 return (NA)
23
09cf9c19 24 gaps = sapply(indices, function(i) {
d2ab47a7 25 if (predict_from >= 2)
9e0f25f6 26 data$getSerie(i)[predict_from] - data$getSerie(i)[predict_from-1]
d2ab47a7 27 else
9e0f25f6 28 head(data$getSerie(i),1) - tail(data$getSerie(i-1),1)
3d69ff21 29 })
09cf9c19
BA
30 scal_product = weights * gaps
31 norm_fact = sum( weights[!is.na(scal_product)] )
3d69ff21
BA
32 sum(scal_product, na.rm=TRUE) / norm_fact
33}