'update'
[talweg.git] / pkg / R / computeForecast.R
CommitLineData
af3b84f4 1#' Compute forecast
3d69ff21 2#'
af3b84f4 3#' 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
ee8b1b4e 22#' @param ncores Number of cores for parallel execution (1 to disable)
3d69ff21
BA
23#' @param ... Additional parameters for the forecasting models
24#'
a66a84b5 25#' @return An object of class Forecast
3d69ff21
BA
26#'
27#' @examples
44a9990b
BA
28#' ts_data = system.file("extdata","pm10_mesures_H_loc.csv",package="talweg")
29#' exo_data = system.file("extdata","meteo_extra_noNAs.csv",package="talweg")
445e7bbc 30#' data = getData(ts_data, exo_data, input_tz="GMT", working_tz="GMT", predict_at=7)
99f83c9a 31#' pred = computeForecast(data, 2200:2230, "Persistence", "Persistence", 500, 12)
3d69ff21
BA
32#' \dontrun{#Sketch for real-time mode:
33#' data = new("Data", ...)
e030a6e3 34#' forecaster = new(..., data=data)
3d69ff21
BA
35#' repeat {
36#' data$append(some_new_data)
e030a6e3 37#' pred = forecaster$predict(data$getSize(), ...)
3d69ff21
BA
38#' #do_something_with_pred
39#' }}
40#' @export
25b75559 41computeForecast = function(data, indices, forecaster, pjump,
ee8b1b4e 42 memory=Inf, horizon=data$getStdHorizon(), ncores=3, ...)
3d69ff21 43{
e030a6e3 44 # (basic) Arguments sanity checks
3d69ff21
BA
45 horizon = as.integer(horizon)[1]
46 if (horizon<=0 || horizon>length(data$getCenteredSerie(2)))
47 stop("Horizon too short or too long")
98e958ca 48 integer_indices = sapply(indices, function(i) dateIndexToInteger(i,data))
a66a84b5 49 if (any(integer_indices<=0 | integer_indices>data$getSize()))
3d69ff21 50 stop("Indices out of range")
a66a84b5
BA
51 if (!is.character(forecaster) || !is.character(pjump))
52 stop("forecaster (name) and pjump (function) should be of class character")
3d69ff21 53
98e958ca 54 pred = Forecast$new( sapply(indices, function(i) integerIndexToDate(i,data)) )
25b75559 55 forecaster_class_name = getFromNamespace(paste(forecaster,"Forecaster",sep=""), "talweg")
98e958ca
BA
56 forecaster = forecaster_class_name$new( #.pjump =
57 getFromNamespace(paste("get",pjump,"JumpPredict",sep=""), "talweg"))
5e838b3e 58
ee8b1b4e 59 if (ncores > 1 && requireNamespace("parallel",quietly=TRUE))
a866acb3 60 {
ee8b1b4e 61 p <- parallel::mclapply(seq_along(integer_indices), function(i) {
a866acb3
BA
62 list(
63 "forecast" = forecaster$predictSerie(data, integer_indices[i], memory, horizon, ...),
64 "params"= forecaster$getParameters(),
65 "index" = integer_indices[i] )
ee8b1b4e 66 }, mc.cores=ncores)
a866acb3
BA
67 }
68 else
69 {
ee8b1b4e 70 p <- lapply(seq_along(integer_indices), function(i) {
a866acb3
BA
71 list(
72 "forecast" = forecaster$predictSerie(data, integer_indices[i], memory, horizon, ...),
73 "params"= forecaster$getParameters(),
74 "index" = integer_indices[i] )
75 })
76 }
5e838b3e 77
ee8b1b4e
BA
78 # TODO: find a way to fill pred in //...
79 for (i in seq_along(integer_indices))
80 {
81 pred$append(
82 new_serie = p[[i]]$forecast,
83 new_params = p[[i]]$params,
84 new_index_in_data = p[[i]]$index
85 )
86 }
25b75559 87 pred
3d69ff21 88}