R/bonsai_forecast_extractor.R

#' Extracts specifc parts of forecasts
#' 
#' Takes in a list of forecasts generated by bonsai_forecast_saver and return a matrix with residuals or mean or any other parameter
#' @param list_forecasts a list of forecasts generated by bonsai_forecast_saver
#' @param extract_object what to extract usually means, residuals, upper or lower
#' @param forecast_functions a commodity to name the rownames of thematrix may be depreciated
#' @param colname_logic a commodity to name the colnames of the matrix may be depreciated
#' @return A matrix with the desired object
#' @export 
bonsai_forecast_extractor <-
  function(list_forecasts,
           extract_object = "mean",
           forecast_functions,
           colname_logic = "h=") {
extracted_object <- lapply(list_forecasts,
                           `[`,
                           extract_object)
matrix_extracted_object <-
  matrix(unlist(extracted_object),
         nrow = length(extracted_object),
         byrow = TRUE)
colnames(matrix_extracted_object) <- paste(colname_logic,
                                       1:length(matrix_extracted_object[1, ]),
                                       sep = "")
rownames(matrix_extracted_object) <- names(forecast_functions)
return(matrix_extracted_object)
}
brunocarlin/forecast.bonsai documentation built on May 3, 2019, 5:45 p.m.