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