'update'
authorBenjamin Auder <benjamin.auder@somewhere>
Mon, 27 Mar 2017 10:34:47 +0000 (12:34 +0200)
committerBenjamin Auder <benjamin.auder@somewhere>
Mon, 27 Mar 2017 10:34:47 +0000 (12:34 +0200)
pkg/R/F_Neighbors2.R

index e6addde..bf9422a 100644 (file)
@@ -19,7 +19,10 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
                                return (NA)
 
                        # Determine indices of no-NAs days followed by no-NAs tomorrows
-                       fdays = getNoNA2(data, max(today-memory,1), today-1)
+                       # Indices of similar days for cross-validation; TODO: 45 = magic number
+                       fdays = intersect(
+                               getNoNA2(data, max(today-memory,1), today-1)
+                               getSimilarDaysIndices(today, limit=45, same_season=TRUE) )
 
                        # Get optional args
                        kernel = ifelse(hasArg("kernel"), list(...)$kernel, "Gauss") #or "Epan"
@@ -29,21 +32,15 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
                                        fdays, today, horizon, list(...)$h_window, kernel, TRUE) )
                        }
 
-
-                       # Indices of similar days for cross-validation; TODO: 45 = magic number
-                       # TODO: ici faut une sorte de "same_season==TRUE" --> mois similaires epandage
-                       sdays = getSimilarDaysIndices(today, limit=45, same_season=FALSE)
-
-
                        # Function to optimize h : h |--> sum of prediction errors on last 45 "similar" days
                        errorOnLastNdays = function(h, kernel)
                        {
                                error = 0
                                nb_jours = 0
-                               for (i in intersect(fdays,sdays))
+                               for (day in fdays)
                                {
                                        # mix_strategy is never used here (simtype != "mix"), therefore left blank
-                                       prediction = private$.predictShapeAux(data, fdays, i, horizon, h, kernel, FALSE)
+                                       prediction = private$.predictShapeAux(data,fdays,day,horizon,h,kernel,FALSE)
                                        if (!is.na(prediction[1]))
                                        {
                                                nb_jours = nb_jours + 1
@@ -55,9 +52,8 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
                        }
 
                        # h :: only for endo in this variation
-                       h_best_endo = optimize(errorOnLastNdays, c(0,10), kernel=kernel)$minimum
-
-                       return (private$.predictShapeAux(data, fdays, today, horizon, h_best, kernel, TRUE))
+                       h_best = optimize(errorOnLastNdays, c(0,10), kernel=kernel)$minimum
+                       return (private$.predictShapeAux(data,fdays,today,horizon,h_best,kernel,TRUE))
                }
        ),
        private = list(
@@ -96,6 +92,7 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
                                        u
                                }
 
+
                        # EXOGENS: distances computations are enough
                        # TODO: search among similar concentrations....... at this stage ?!
                        M = matrix( nrow=1+length(fdays), ncol=1+length(data$getExo(today)) )
@@ -117,6 +114,7 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
                        ppv <- sort(distances2, index.return=TRUE)$ix[1:10] #..............
 #PPV pour endo ?
 
+
                        similarities =
                                if (simtype == "exo")
                                        simils_exo
@@ -134,13 +132,7 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
                        {
                                private$.params$weights <- similarities
                                private$.params$indices <- fdays
-                               private$.params$window <-
-                                       if (simtype=="endo")
-                                               h_endo
-                                       else if (simtype=="exo")
-                                               h_exo
-                                       else #mix
-                                               c(h_endo,h_exo)
+                               private$.params$window <- h
                        }
 
                        return (prediction)