fixes and improvements
[talweg.git] / R / D_Neighbors.R
CommitLineData
3d69ff21
BA
1#' Obtain delta forecast by the Neighbors method
2#'
3#' @inheritParams getForecast
4#' @inheritParams getZeroDeltaForecast
5getNeighborsDeltaForecast = function(data, today, memory, horizon, shape_params, ...)
6{
09cf9c19
BA
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)))
3d69ff21
BA
12 return (NA)
13
09cf9c19 14 gaps = sapply(indices, function(i) {
3d69ff21
BA
15 data$getSerie(i+1)[1] - tail(data$getSerie(i), 1)
16 })
17
09cf9c19
BA
18 scal_product = weights * gaps
19 norm_fact = sum( weights[!is.na(scal_product)] )
3d69ff21
BA
20 sum(scal_product, na.rm=TRUE) / norm_fact
21}