#' @title get Forecast #' #' @description Predict time-series curves for the selected days indices (lines in data). #' Run the forecasting task described by \code{delta_forecaster_name} and #' \code{shape_forecaster_name} on data obtained with \code{getData} #' #' @param data Dataset, object of type \code{Data} output of \code{getData} #' @param indices Days indices where to forecast (the day after) #' @param memory Data depth (in days) to be used for prediction #' @param horizon Number of time steps to predict #' @param shape_forecaster_name Name of the shape forcaster #' \itemize{ #' \item Persistence : use values of last (similar, next) day #' \item Neighbors : use PM10 from the k closest neighbors' tomorrows #' \item Average : global average of all the (similar) "tomorrow of past" #' } #' @param delta_forecaster_name Name of the delta forecaster #' \itemize{ #' \item Persistence : use last (similar) day values #' \item Neighbors: re-use the weights optimized in corresponding shape forecaster #' \item Zero: just output 0 (no adjustment) #' } #' @param ... Additional parameters for the forecasting models #' #' @return An object of class Forecast #' #' @examples #' data = getData(ts_data="data/pm10_mesures_H_loc.csv", exo_data="data/meteo_extra_noNAs.csv", #' input_tz = "Europe/Paris", working_tz="Europe/Paris", predict_at="07") #' pred = getForecast(data, 2200:2230, Inf, 12, "Persistence", "Persistence") #' \dontrun{#Sketch for real-time mode: #' data = new("Data", ...) #' repeat { #' data$append(some_new_data) #' pred = getForecast(data, ...) #' #do_something_with_pred #' }} #' @export getForecast = function(data, indices, memory, horizon, shape_forecaster_name, delta_forecaster_name, ...) { horizon = as.integer(horizon)[1] if (horizon<=0 || horizon>length(data$getCenteredSerie(2))) stop("Horizon too short or too long") indices = as.integer(indices) if (any(indices<=0 | indices>data$getSize())) stop("Indices out of range") indices = sapply(indices, dateIndexToInteger, data) #NOTE: some assymetry here... shape_forecaster = new(paste(shape_forecaster_name,"ShapeForecaster",sep=""), data=data) #A little bit strange, but match.fun() and get() fail delta_forecaster = getFromNamespace( paste("get",delta_forecaster_name,"DeltaForecast",sep=""), "talweg") pred = list() for (today in indices) { #NOTE: To estimate timing... # print(paste("Predict until index",today)) #shape always predicted first (on centered series, no scaling taken into account), #with side-effect: optimize some parameters (h, weights, ...) predicted_shape = shape_forecaster$predict(today, memory, horizon, ...) #then, delta prediction can re-use some variables optimized previously (like neighbors infos) predicted_delta = delta_forecaster(data, today, memory, horizon, shape_forecaster$getParameters(), ...) #TODO: this way is faster than a call to append(); why ? pred[[length(pred)+1]] = list( # Predict shape and align it on end of current day serie = predicted_shape + tail( data$getSerie(today), 1 ) - predicted_shape[1], predicted_delta, #add predicted jump params = shape_forecaster$getParameters(), index = today ) } new("Forecast",pred=pred) }