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