X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=pkg%2FR%2FF_Neighbors.R;h=600c5c8ceeaae272a824a259c36f5075662c4b78;hb=5c49f6cecd547358b327e9363e62bcc8219e9e33;hp=202b7e2472a4901f75f471391cf956b52d0294f0;hpb=ff5df8e310b73883565761ab4b1aa5a0672e9f27;p=talweg.git diff --git a/pkg/R/F_Neighbors.R b/pkg/R/F_Neighbors.R index 202b7e2..600c5c8 100644 --- a/pkg/R/F_Neighbors.R +++ b/pkg/R/F_Neighbors.R @@ -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) + Centered} 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 @@ -158,7 +171,7 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster", prediction = rep(0, horizon) for (i in seq_along(fdays)) - prediction = prediction + similarities[i] * data$getSerie(fdays[i]+1)[1:horizon] + prediction = prediction + similarities[i] * data$getCenteredSerie(fdays[i]+1)[1:horizon] prediction = prediction / sum(similarities, na.rm=TRUE) if (final_call) @@ -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)