storage by-rows
[talweg.git] / pkg / R / F_Neighbors.R
index 202b7e2..a3d44a3 100644 (file)
@@ -4,6 +4,7 @@
 #'
 #' Predict tomorrow as a weighted combination of "futures of the past" days.
 #' Inherits \code{\link{Forecaster}}
+#'
 NeighborsForecaster = R6::R6Class("NeighborsForecaster",
        inherit = Forecaster,
 
@@ -100,17 +101,22 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster",
                                {
                                        delta = data$getCenteredSerie(today) - data$getCenteredSerie(fdays[i])
                                        # Require at least half of non-NA common values to compute the distance
-                                       if (sum(is.na(delta)) <= 0) #length(delta)/2)
-                                               distances2[i] = mean(delta^2) #, na.rm=TRUE)
+                                       if ( !any( is.na(delta) ) )
+                                               distances2[i] = mean(delta^2)
                                }
 
                                sd_dist = sd(distances2)
                                if (sd_dist < .Machine$double.eps)
+                               {
+                                       warning("All computed distances are very close: stdev too small")
                                        sd_dist = 1 #mostly for tests... FIXME:
+                               }
                                simils_endo =
                                        if (kernel=="Gauss")
                                                exp(-distances2/(sd_dist*h_endo^2))
-                                       else { #Epanechnikov
+                                       else
+                                       {
+                                               # Epanechnikov
                                                u = 1 - distances2/(sd_dist*h_endo^2)
                                                u[abs(u)>1] = 0.
                                                u
@@ -138,10 +144,17 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster",
                                }
 
                                sd_dist = sd(distances2)
+                               if (sd_dist < .Machine$double.eps)
+                               {
+                                       warning("All computed distances are very close: stdev too small")
+                                       sd_dist = 1 #mostly for tests... FIXME:
+                               }
                                simils_exo =
                                        if (kernel=="Gauss")
                                                exp(-distances2/(sd_dist*h_exo^2))
-                                       else { #Epanechnikov
+                                       else
+                                       {
+                                               # Epanechnikov
                                                u = 1 - distances2/(sd_dist*h_exo^2)
                                                u[abs(u)>1] = 0.
                                                u
@@ -166,13 +179,12 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster",
                                private$.params$weights <- similarities
                                private$.params$indices <- fdays
                                private$.params$window <-
-                                       if (simtype=="endo") {
+                                       if (simtype=="endo")
                                                h_endo
-                                       } else if (simtype=="exo") {
+                                       else if (simtype=="exo")
                                                h_exo
-                                       } else { #mix
+                                       else #mix
                                                c(h_endo,h_exo)
-                                       }
                        }
 
                        return (prediction)