cleaning - fix getSimilarDaysIndices ; to finish
[talweg.git] / pkg / R / F_Neighbors2.R
index fb63e40..83ef453 100644 (file)
@@ -9,15 +9,15 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
        inherit = Forecaster,
 
        public = list(
-               predictSerie = function(data, today, memory, horizon, ...)
-               {
-                       # Parameters (potentially) computed during shape prediction stage
-                       predicted_shape = self$predictShape(data, today, memory, horizon, ...)
-#                      predicted_delta = private$.pjump(data,today,memory,horizon,private$.params,...)
-                       # Predicted shape is aligned it on the end of current day + jump
-#                      predicted_shape+tail(data$getSerie(today),1)-predicted_shape[1]+predicted_delta
-                       predicted_shape
-               },
+#              predictSerie = function(data, today, memory, horizon, ...)
+#              {
+#                      # Parameters (potentially) computed during shape prediction stage
+#                      predicted_shape = self$predictShape(data, today, memory, horizon, ...)
+##                     predicted_delta = private$.pjump(data,today,memory,horizon,private$.params,...)
+#                      # Predicted shape is aligned it on the end of current day + jump
+##                     predicted_shape+tail(data$getSerie(today),1)-predicted_shape[1]+predicted_delta
+#                      predicted_shape
+#              },
                predictShape = function(data, today, memory, horizon, ...)
                {
                        # (re)initialize computed parameters
@@ -101,7 +101,7 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
                {
                        fdays = fdays[ fdays < today ]
                        # TODO: 3 = magic number
-                       if (length(fdays) < 3)
+                       if (length(fdays) < 1)
                                return (NA)
 
                        # Neighbors: days in "same season"
@@ -110,14 +110,26 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
                        levelToday = data$getLevel(today)
                        distances = sapply(indices, function(i) abs(data$getLevel(i)-levelToday))
                        same_pollution = (distances <= 2)
-                       if (sum(same_pollution) < 3) #TODO: 3 == magic number
+                       if (sum(same_pollution) < 1) #TODO: 3 == magic number
                        {
                                same_pollution = (distances <= 5)
-                               if (sum(same_pollution) < 3)
+                               if (sum(same_pollution) < 1)
                                        return (NA)
                        }
                        indices = indices[same_pollution]
 
+                       #TODO: we shouldn't need that block
+                       if (length(indices) == 1)
+                       {
+                               if (final_call)
+                               {
+                                       private$.params$weights <- 1
+                                       private$.params$indices <- indices
+                                       private$.params$window <- 1
+                               }
+                               return ( data$getSerie(indices[1])[1:horizon] ) #what else?!
+                       }
+
                        if (simtype != "exo")
                        {
                                h_endo = ifelse(simtype=="mix", h[1], h)
@@ -159,7 +171,7 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
                                sigma = cov(M) #NOTE: robust covariance is way too slow
 #                              sigma_inv = solve(sigma) #TODO: use pseudo-inverse if needed?
                                sigma_inv = MASS::ginv(sigma)
-#if (final_call) browser()
+
                                # Distances from last observed day to days in the past
                                distances2 = sapply(seq_along(indices), function(i) {
                                        delta = M[1,] - M[i+1,]
@@ -200,7 +212,7 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
                        if (final_call)
                        {
                                private$.params$weights <- similarities
-                               private$.params$indices <- fdays
+                               private$.params$indices <- indices
                                private$.params$window <-
                                        if (simtype=="endo")
                                                h_endo