add realtime option, slightly refactor data acquisition
[talweg.git] / pkg / R / J_Neighbors.R
CommitLineData
e030a6e3 1#' Obtain jump forecast by the Neighbors method
3d69ff21 2#'
99f83c9a 3#' @inheritParams computeForecast
e030a6e3 4#' @inheritParams getZeroJumpPredict
546b0cb6 5#'
e030a6e3 6getNeighborsJumpPredict = function(data, today, memory, horizon, params, ...)
3d69ff21 7{
09cf9c19 8 first_day = max(1, today-memory)
ee8b1b4e 9 filter = (params$indices >= first_day)
e030a6e3
BA
10 indices = params$indices[filter]
11 weights = params$weights[filter]
72b9c501 12 realtime = ifelse(hasArg("realtime"), list(...)$realtime, FALSE)
99f83c9a 13
09cf9c19 14 if (any(is.na(weights) | is.na(indices)))
3d69ff21
BA
15 return (NA)
16
09cf9c19 17 gaps = sapply(indices, function(i) {
72b9c501 18 data$getSerie(i+1,hat=(realtime && i+1==today))[1] - tail(data$getSerie(i), 1)
3d69ff21 19 })
09cf9c19
BA
20 scal_product = weights * gaps
21 norm_fact = sum( weights[!is.na(scal_product)] )
3d69ff21
BA
22 sum(scal_product, na.rm=TRUE) / norm_fact
23}