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