#'
#' Predict tomorrow as a weighted combination of "futures of the past" days.
#' Inherits \code{\link{Forecaster}}
+#'
NeighborsForecaster = R6::R6Class("NeighborsForecaster",
inherit = Forecaster,
# (re)initialize computed parameters
private$.params <- list("weights"=NA, "indices"=NA, "window"=NA)
+ # Do not forecast on days with NAs (TODO: softer condition...)
+ if (any(is.na(data$getCenteredSerie(today))))
+ return (NA)
+
# Determine indices of no-NAs days followed by no-NAs tomorrows
fdays = getNoNA2(data, max(today-memory,1), today-1)
{
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
}
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
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)
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)