R CMD check OK
[talweg.git] / pkg / R / computeForecast.R
CommitLineData
af3b84f4 1#' Compute forecast
3d69ff21 2#'
102bcfda
BA
3#' Predict time-series curves ("tomorrows") at the selected days indices ("todays").
4#' This function just runs a loop over all requested indices, and stores the individual
5#' forecasts into a list which is then turned into a Forecast object.
c4c329f6 6#'
102bcfda 7#' @param data Object of class Data, output of \code{getData()}.
2057c793 8#' @param indices Indices where to forecast (the day after); integers relative to the
e169b5d5
BA
9#' beginning of data, or (convertible to) Date objects.
10#' @param forecaster Name of the main forecaster; more details: ?F_<forecastername>
3d69ff21 11#' \itemize{
e169b5d5
BA
12#' \item Persistence : use last (similar, next) day
13#' \item Neighbors : weighted tomorrows of similar days
14#' \item Average : average tomorrow of all same day-in-week
e030a6e3 15#' \item Zero : just output 0 (benchmarking purpose)
3d69ff21 16#' }
e169b5d5
BA
17#' @param pjump Function to predict the jump at the interface between two days;
18#' more details: ?J_<functionname>
3d69ff21 19#' \itemize{
e169b5d5
BA
20#' \item Persistence : use last (similar, next) day
21#' \item Neighbors: re-use the weights from F_Neighbors
3d69ff21
BA
22#' \item Zero: just output 0 (no adjustment)
23#' }
e169b5d5
BA
24#' @param memory Data depth (in days) to be used for prediction.
25#' @param horizon Number of time steps to predict.
26#' @param ncores Number of cores for parallel execution (1 to disable).
27#' @param ... Additional parameters for the forecasting models.
3d69ff21 28#'
a66a84b5 29#' @return An object of class Forecast
3d69ff21
BA
30#'
31#' @examples
e169b5d5
BA
32#' ts_data <- system.file("extdata","pm10_mesures_H_loc.csv",package="talweg")
33#' exo_data <- system.file("extdata","meteo_extra_noNAs.csv",package="talweg")
3ddf1c12
BA
34#' data <- getData(ts_data, exo_data, input_tz="GMT", working_tz="GMT",
35#' predict_at=7, limit=200)
36#' pred <- computeForecast(data, 100:130, "Persistence", "Zero",
37#' memory=50, horizon=12, ncores=1)
3d69ff21 38#' \dontrun{#Sketch for real-time mode:
e169b5d5 39#' data <- Data$new()
e169b5d5 40#' forecaster <- MyForecaster$new(myJumpPredictFunc)
3d69ff21 41#' repeat {
e169b5d5 42#' # In the morning 7am+ or afternoon 1pm+:
c1be9898 43#' data$append(
e169b5d5
BA
44#' times_from_H+1_yersteday_to_Hnow,
45#' PM10_values_of_last_24h,
c1be9898
BA
46#' exogenous_measures_of_last_24h,
47#' exogenous_predictions_for_next_24h)
102bcfda 48#' pred <- forecaster$predictSerie(data, data$getSize(), ...)
3d69ff21
BA
49#' #do_something_with_pred
50#' }}
51#' @export
25b75559 52computeForecast = function(data, indices, forecaster, pjump,
ee8b1b4e 53 memory=Inf, horizon=data$getStdHorizon(), ncores=3, ...)
3d69ff21 54{
e030a6e3 55 # (basic) Arguments sanity checks
3d69ff21 56 horizon = as.integer(horizon)[1]
72b9c501 57 if (horizon<=0 || horizon>length(data$getCenteredSerie(1)))
3d69ff21 58 stop("Horizon too short or too long")
98e958ca 59 integer_indices = sapply(indices, function(i) dateIndexToInteger(i,data))
a66a84b5 60 if (any(integer_indices<=0 | integer_indices>data$getSize()))
3d69ff21 61 stop("Indices out of range")
a66a84b5
BA
62 if (!is.character(forecaster) || !is.character(pjump))
63 stop("forecaster (name) and pjump (function) should be of class character")
3d69ff21 64
98e958ca 65 pred = Forecast$new( sapply(indices, function(i) integerIndexToDate(i,data)) )
72b9c501
BA
66 forecaster_class_name = getFromNamespace(
67 paste(forecaster,"Forecaster",sep=""), "talweg")
98e958ca
BA
68 forecaster = forecaster_class_name$new( #.pjump =
69 getFromNamespace(paste("get",pjump,"JumpPredict",sep=""), "talweg"))
5e838b3e 70
ee8b1b4e 71 if (ncores > 1 && requireNamespace("parallel",quietly=TRUE))
a866acb3 72 {
ee8b1b4e 73 p <- parallel::mclapply(seq_along(integer_indices), function(i) {
a866acb3 74 list(
72b9c501
BA
75 "forecast" = forecaster$predictSerie(
76 data, integer_indices[i], memory, horizon, ...),
a866acb3
BA
77 "params"= forecaster$getParameters(),
78 "index" = integer_indices[i] )
ee8b1b4e 79 }, mc.cores=ncores)
a866acb3
BA
80 }
81 else
82 {
ee8b1b4e 83 p <- lapply(seq_along(integer_indices), function(i) {
a866acb3 84 list(
72b9c501
BA
85 "forecast" = forecaster$predictSerie(
86 data, integer_indices[i], memory, horizon, ...),
a866acb3
BA
87 "params"= forecaster$getParameters(),
88 "index" = integer_indices[i] )
89 })
90 }
5e838b3e 91
ee8b1b4e
BA
92 # TODO: find a way to fill pred in //...
93 for (i in seq_along(integer_indices))
94 {
95 pred$append(
72b9c501
BA
96 forecast = p[[i]]$forecast,
97 params = p[[i]]$params,
98 index_in_data = p[[i]]$index
ee8b1b4e
BA
99 )
100 }
25b75559 101 pred
3d69ff21 102}