| 1 | #' Forecast |
| 2 | #' |
| 3 | #' Forecast encapsulation as a list (days where prediction occur) of lists (components). |
| 4 | #' |
| 5 | #' The private field .pred is a list where each cell contains the predicted variables for |
| 6 | #' a period of time of H-P+1<=24 hours, from hour P until H, where P == predict_from. |
| 7 | #' \code{forecast$getForecast(i)} output forecasts for |
| 8 | #' \code{data$getSerie(forecast$getIndexInData(i))}. |
| 9 | #' |
| 10 | #' @usage # Forecast$new(dates) |
| 11 | #' |
| 12 | #' @field .pred List with |
| 13 | #' \itemize{ |
| 14 | #' \item serie: the forecasted serie |
| 15 | #' \item params: corresponding list of parameters (weights, neighbors...) |
| 16 | #' \item index_in_data: corresponding index in data object |
| 17 | #' } |
| 18 | #' @field .dates vector of (integer) day indices where forecast occurs |
| 19 | #' |
| 20 | #' @section Methods: |
| 21 | #' \describe{ |
| 22 | #' \item{\code{initialize(dates)}}{ |
| 23 | #' Initialize a Forecast object with a series of date indices.} |
| 24 | #' \item{\code{getSize()}}{ |
| 25 | #' Return number of individual forecasts.} |
| 26 | #' \item{\code{append(forecast, params, index_in_data)}}{ |
| 27 | #' Acquire an individual forecast, with its (optimized) parameters and the |
| 28 | #' corresponding index in the dataset.} |
| 29 | #' \item{\code{getDates()}}{ |
| 30 | #' Get dates where forecast occurs.} |
| 31 | #' \item{\code{getForecast(index)}}{ |
| 32 | #' Get forecasted serie at specified index.} |
| 33 | #' \item{\code{getParams(index)}}{ |
| 34 | #' Get parameters at specified index (for 'Neighbors' method).} |
| 35 | #' \item{\code{getIndexInData(index)}}{ |
| 36 | #' Get index in data which corresponds to current forecast.} |
| 37 | #' } |
| 38 | #' |
| 39 | #' @docType class |
| 40 | #' @format R6 class |
| 41 | #' |
| 42 | Forecast = R6::R6Class("Forecast", |
| 43 | private = list( |
| 44 | .pred = list(), |
| 45 | .dates = integer(0) #store dates as integers (from 1970-01-01) |
| 46 | ), |
| 47 | public = list( |
| 48 | initialize = function(dates) |
| 49 | { |
| 50 | private$.dates <- dates |
| 51 | invisible(self) |
| 52 | }, |
| 53 | getSize = function() |
| 54 | length(private$.pred) |
| 55 | , |
| 56 | append = function(forecast, params, index_in_data) |
| 57 | { |
| 58 | private$.pred[[length(private$.pred)+1]] <- |
| 59 | list("forecast"=forecast, "params"=params, "index_in_data"=index_in_data) |
| 60 | }, |
| 61 | getDates = function() |
| 62 | as.Date( private$.dates, origin="1970-01-01" ) |
| 63 | , |
| 64 | getForecast = function(index) |
| 65 | { |
| 66 | if (is(index,"Date")) |
| 67 | index = match(index, private$.dates) |
| 68 | private$.pred[[index]]$forecast |
| 69 | }, |
| 70 | getParams = function(index) |
| 71 | { |
| 72 | if (is(index,"Date")) |
| 73 | index = match(index, private$.dates) |
| 74 | private$.pred[[index]]$params |
| 75 | }, |
| 76 | getIndexInData = function(index) |
| 77 | { |
| 78 | if (is(index,"Date")) |
| 79 | index = match(index, private$.dates) |
| 80 | private$.pred[[index]]$index_in_data |
| 81 | } |
| 82 | ) |
| 83 | ) |