Commit | Line | Data |
---|---|---|
3d69ff21 BA |
1 | #' @title get Forecast |
2 | #' | |
3 | #' @description Predict time-series curves for the selected days indices (lines in data). | |
3d69ff21 BA |
4 | #' |
5 | #' @param data Dataset, object of type \code{Data} output of \code{getData} | |
6 | #' @param indices Days indices where to forecast (the day after) | |
e030a6e3 | 7 | #' @param forecaster Name of the main forcaster |
3d69ff21 BA |
8 | #' \itemize{ |
9 | #' \item Persistence : use values of last (similar, next) day | |
e030a6e3 | 10 | #' \item Neighbors : use values from the k closest neighbors' tomorrows |
3d69ff21 | 11 | #' \item Average : global average of all the (similar) "tomorrow of past" |
e030a6e3 | 12 | #' \item Zero : just output 0 (benchmarking purpose) |
3d69ff21 | 13 | #' } |
e030a6e3 | 14 | #' @param pjump How to predict the jump at the interface between two days ? |
3d69ff21 BA |
15 | #' \itemize{ |
16 | #' \item Persistence : use last (similar) day values | |
e030a6e3 | 17 | #' \item Neighbors: re-use the weights optimized in corresponding forecaster |
3d69ff21 BA |
18 | #' \item Zero: just output 0 (no adjustment) |
19 | #' } | |
e030a6e3 BA |
20 | #' @param memory Data depth (in days) to be used for prediction |
21 | #' @param horizon Number of time steps to predict | |
3d69ff21 BA |
22 | #' @param ... Additional parameters for the forecasting models |
23 | #' | |
24 | #' @return An object of class Forecast | |
25 | #' | |
26 | #' @examples | |
613a986f | 27 | #' data = getData(ts_data="pm10_mesures_H_loc.csv", exo_data="meteo_extra_noNAs.csv", |
e030a6e3 BA |
28 | #' input_tz = "Europe/Paris", working_tz="Europe/Paris", predict_at=7) |
29 | #' pred = getForecast(data, 2200:2230, "Persistence", "Persistence", 500, 12) | |
3d69ff21 BA |
30 | #' \dontrun{#Sketch for real-time mode: |
31 | #' data = new("Data", ...) | |
e030a6e3 | 32 | #' forecaster = new(..., data=data) |
3d69ff21 BA |
33 | #' repeat { |
34 | #' data$append(some_new_data) | |
e030a6e3 | 35 | #' pred = forecaster$predict(data$getSize(), ...) |
3d69ff21 BA |
36 | #' #do_something_with_pred |
37 | #' }} | |
38 | #' @export | |
e5aa669a | 39 | getForecast = function(data, indices, forecaster, pjump=NULL, |
e030a6e3 | 40 | memory=Inf, horizon=data$getStdHorizon(), ...) |
3d69ff21 | 41 | { |
e030a6e3 | 42 | # (basic) Arguments sanity checks |
3d69ff21 BA |
43 | horizon = as.integer(horizon)[1] |
44 | if (horizon<=0 || horizon>length(data$getCenteredSerie(2))) | |
45 | stop("Horizon too short or too long") | |
09cf9c19 | 46 | indices = sapply( seq_along(indices), function(i) dateIndexToInteger(indices[i], data) ) |
3d69ff21 BA |
47 | if (any(indices<=0 | indices>data$getSize())) |
48 | stop("Indices out of range") | |
49 | indices = sapply(indices, dateIndexToInteger, data) | |
e5aa669a BA |
50 | if (!is.character(forecaster)) |
51 | stop("forecaster (name) should be of class character") #pjump could be NULL | |
3d69ff21 BA |
52 | |
53 | pred = list() | |
e030a6e3 | 54 | forecaster = new(paste(forecaster,"Forecaster",sep=""), data=data, |
e5aa669a BA |
55 | pjump = |
56 | if (is.null(pjump)) | |
57 | function() {} | |
58 | else | |
59 | getFromNamespace(paste("get",pjump,"JumpPredict",sep=""), "talweg")) | |
3d69ff21 BA |
60 | for (today in indices) |
61 | { | |
1e20780e | 62 | #pred$append(...) is slow; TODO: use R6 class |
3d69ff21 | 63 | pred[[length(pred)+1]] = list( |
e030a6e3 BA |
64 | "serie" = forecaster$predict(today, memory, horizon, ...), |
65 | "params" = forecaster$getParameters(), | |
66 | "index" = today | |
67 | ) | |
3d69ff21 BA |
68 | } |
69 | new("Forecast",pred=pred) | |
70 | } |