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