1 #' @include Forecaster.R
3 #' Neighbors2 Forecaster
5 #' Predict tomorrow as a weighted combination of "futures of the past" days.
6 #' Inherits \code{\link{Forecaster}}
8 Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
12 predictShape = function(data, today, memory, horizon, ...)
14 # (re)initialize computed parameters
15 private$.params <- list("weights"=NA, "indices"=NA, "window"=NA)
17 # Do not forecast on days with NAs (TODO: softer condition...)
18 if (any(is.na(data$getCenteredSerie(today))))
21 # Determine indices of no-NAs days followed by no-NAs tomorrows
22 fdays = getNoNA2(data, max(today-memory,1), today-1)
25 kernel = ifelse(hasArg("kernel"), list(...)$kernel, "Gauss") #or "Epan"
28 return ( private$.predictShapeAux(data,
29 fdays, today, horizon, list(...)$h_window, kernel, TRUE) )
32 # Indices of similar days for cross-validation; TODO: 45 = magic number
33 sdays = getSimilarDaysIndices(today, limit=45, same_season=FALSE)
35 # Function to optimize h : h |--> sum of prediction errors on last 45 "similar" days
36 errorOnLastNdays = function(h, kernel)
40 for (day in intersect(fdays,sdays))
42 # mix_strategy is never used here (simtype != "mix"), therefore left blank
43 prediction = private$.predictShapeAux(data,fdays,day,horizon,h,kernel,FALSE)
44 if (!is.na(prediction[1]))
46 nb_jours = nb_jours + 1
48 mean((data$getSerie(i+1)[1:horizon] - prediction)^2)
51 return (error / nb_jours)
54 # h :: only for endo in this variation
55 h_best = optimize(errorOnLastNdays, c(0,7), kernel=kernel)$minimum
56 return (private$.predictShapeAux(data,fdays,today,horizon,h_best,kernel,TRUE))
60 # Precondition: "today" is full (no NAs)
61 .predictShapeAux = function(data, fdays, today, horizon, h, kernel, final_call)
63 fdays = fdays[ fdays < today ]
64 # TODO: 3 = magic number
65 if (length(fdays) < 3)
68 # Neighbors: days in "same season"
69 sdays = getSimilarDaysIndices(today, limit=45, same_season=TRUE, data)
70 indices = intersect(fdays,sdays)
71 levelToday = data$getLevel(today)
72 distances = sapply(seq_along(indices), function(i) abs(data$getLevel(i)-levelToday))
73 same_pollution = (distances <= 2)
74 if (sum(same_pollution) < 3) #TODO: 3 == magic number
76 same_pollution = (distances <= 5)
77 if (sum(same_pollution) < 3)
80 indices = indices[same_pollution]
82 # Now OK: indices same season, same pollution level
86 # ENDO:: Distances from last observed day to days in the past
87 serieToday = data$getSerie(today)
88 distances2 = sapply(indices, function(i) {
89 delta = serieToday - data$getSerie(i)
90 distances2[i] = mean(delta^2)
93 sd_dist = sd(distances2)
94 if (sd_dist < .Machine$double.eps)
96 # warning("All computed distances are very close: stdev too small")
97 sd_dist = 1 #mostly for tests... FIXME:
101 exp(-distances2/(sd_dist*h_endo^2))
105 u = 1 - distances2/(sd_dist*h_endo^2)
110 # # EXOGENS: distances computations are enough
111 # # TODO: search among similar concentrations....... at this stage ?!
112 # M = matrix( nrow=1+length(fdays), ncol=1+length(data$getExo(today)) )
113 # M[1,] = c( data$getLevel(today), as.double(data$getExo(today)) )
114 # for (i in seq_along(fdays))
115 # M[i+1,] = c( data$getLevel(fdays[i]), as.double(data$getExo(fdays[i])) )
117 # sigma = cov(M) #NOTE: robust covariance is way too slow
118 # sigma_inv = solve(sigma) #TODO: use pseudo-inverse if needed?
120 # # Distances from last observed day to days in the past
121 # distances2 = rep(NA, nrow(M)-1)
122 # for (i in 2:nrow(M))
124 # delta = M[1,] - M[i,]
125 # distances2[i-1] = delta %*% sigma_inv %*% delta
128 similarities = simils_endo
130 prediction = rep(0, horizon)
131 for (i in seq_along(indices))
132 prediction = prediction + similarities[i] * data$getSerie(indices[i]+1)[1:horizon]
133 prediction = prediction / sum(similarities, na.rm=TRUE)
137 private$.params$weights <- similarities
138 private$.params$indices <- indices
139 private$.params$window <- h