| 1 | #' Forecast |
| 2 | #' |
| 3 | #' Forecast encapsulation |
| 4 | #' |
| 5 | #' @field pred List with |
| 6 | #' \itemize{ |
| 7 | #' \item serie: forecasted serie |
| 8 | #' \item params: corresponding list of parameters (weights, neighbors...) |
| 9 | #' \item index: corresponding index in data object |
| 10 | #' } |
| 11 | #' |
| 12 | #' @docType class |
| 13 | #' @importFrom R6 R6Class |
| 14 | #' |
| 15 | #' @export |
| 16 | Forecast = R6::R6Class("Forecast", |
| 17 | private = list( |
| 18 | .pred = "list", |
| 19 | .dates = "Date" |
| 20 | ), |
| 21 | public = list( |
| 22 | initialize = function(dates) |
| 23 | initialize(self, private, dates) |
| 24 | , |
| 25 | getSize = function() |
| 26 | getSize(private) |
| 27 | , |
| 28 | append = function(new_serie, new_params, new_index) |
| 29 | append(private, new_serie, new_params, new_index) |
| 30 | , |
| 31 | getDates = function() |
| 32 | getDates(private) |
| 33 | , |
| 34 | getSerie = function(index) |
| 35 | getSerie(private, index) |
| 36 | , |
| 37 | getParams = function(index) |
| 38 | getParams(private, index) |
| 39 | , |
| 40 | getIndexInData = function(index) |
| 41 | getIndexInData(private, index) |
| 42 | ) |
| 43 | ) |
| 44 | |
| 45 | #' Initialize empty Forecast object |
| 46 | #' |
| 47 | #' @param o Object of class Forecast |
| 48 | #' @param private List of private members in o |
| 49 | #' @param dates vector of dates where forecast occurs |
| 50 | initialize = function(o, private, dates) |
| 51 | { |
| 52 | private$.dates <<- dates |
| 53 | private$.pred <<- list() |
| 54 | invisible(o) |
| 55 | } |
| 56 | |
| 57 | #' Number of individual forecasts" |
| 58 | #' |
| 59 | #' @inheritParams initialize |
| 60 | getSize = function(private) |
| 61 | length(private$.pred) |
| 62 | |
| 63 | #' Obtain a new pair (serie, params)" |
| 64 | #' |
| 65 | #' @inheritParams initialize |
| 66 | append = function(new_serie, new_params, new_index_in_data) |
| 67 | { |
| 68 | private$.pred[[length(private$.pred)+1]] <<- |
| 69 | list("serie"=new_serie, "params"=new_params, "index_in_data"=new_index_in_data) |
| 70 | } |
| 71 | |
| 72 | #' Dates where prediction occurs |
| 73 | #' |
| 74 | #' inheritParams initialize |
| 75 | getDates = function(private) |
| 76 | private$.dates |
| 77 | |
| 78 | #' Serie values at specified index" |
| 79 | #' |
| 80 | #' @inheritParams initialize |
| 81 | #' @param index Return value at this index |
| 82 | getSerie = function(index) |
| 83 | { |
| 84 | if (is(index,"Date")) |
| 85 | index = match(index, private$.dates) |
| 86 | private$.pred[[index]]$serie |
| 87 | } |
| 88 | |
| 89 | #' Params at specified index" |
| 90 | #' |
| 91 | #' @inheritParams getSerie |
| 92 | getParams = function(index) |
| 93 | { |
| 94 | if (is(index,"Date")) |
| 95 | index = match(index, private$.dates) |
| 96 | private$.pred[[index]]$params |
| 97 | } |
| 98 | |
| 99 | #' (day) Index in data where prediction took place" |
| 100 | #' |
| 101 | #' @inheritParams getSerie |
| 102 | getIndexInData = function(index) |
| 103 | { |
| 104 | if (is(index,"Date")) |
| 105 | index = match(index, private$.dates) |
| 106 | private$.pred[[index]]$index_in_data |
| 107 | } |