dba5f378a99661dee36edb45443584ca7e4e249d
[talweg.git] / R / D_Neighbors.R
1 #' Obtain delta forecast by the Neighbors method
2 #'
3 #' @inheritParams getForecast
4 #' @inheritParams getZeroDeltaForecast
5 getNeighborsDeltaForecast = function(data, today, memory, horizon, shape_params, ...)
6 {
7 first_day = max(1, today-memory)
8 filter = shape_params$indices >= first_day
9 indices = shape_params$indices[filter]
10 weights = shape_params$weights[filter]
11 if (any(is.na(weights) | is.na(indices)))
12 return (NA)
13
14 gaps = sapply(indices, function(i) {
15 data$getSerie(i+1)[1] - tail(data$getSerie(i), 1)
16 })
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 }