fixes and improvements
[talweg.git] / R / D_Neighbors.R
index f7bb5a8..dba5f37 100644 (file)
@@ -4,14 +4,18 @@
 #' @inheritParams getZeroDeltaForecast
 getNeighborsDeltaForecast = function(data, today, memory, horizon, shape_params, ...)
 {
-       if (any(is.na(shape_params$weights) | is.na(shape_params$indices)))
+       first_day = max(1, today-memory)
+       filter = shape_params$indices >= first_day
+       indices = shape_params$indices[filter]
+       weights = shape_params$weights[filter]
+       if (any(is.na(weights) | is.na(indices)))
                return (NA)
 
-       gaps = sapply(shape_params$indices, function(i) {
+       gaps = sapply(indices, function(i) {
                data$getSerie(i+1)[1] - tail(data$getSerie(i), 1)
        })
 
-       scal_product = shape_params$weights * gaps
-       norm_fact = sum( shape_params$weights[!is.na(scal_product)] )
+       scal_product = weights * gaps
+       norm_fact = sum( weights[!is.na(scal_product)] )
        sum(scal_product, na.rm=TRUE) / norm_fact
 }