/R_utils/model_eval.R

https://github.com/microsoft/forecasting · R · 50 lines · 32 code · 3 blank · 15 comment · 1 complexity · 6cba2cd68276d3eb9fa5b2171d31ea70 MD5 · raw file

  1. # Copyright (c) Microsoft Corporation.
  2. # Licensed under the MIT License.
  3. #' Computes forecast values on a dataset
  4. #'
  5. #' @param mable A mable (model table) as returned by `fabletools::model`.
  6. #' @param newdata The dataset for which to compute forecasts.
  7. #' @param ... Further arguments to `fabletools::forecast`.
  8. #' @return
  9. #' A tsibble, with one column per model type in `mable`, and one column named `.response` containing the response variable from `newdata`.
  10. get_forecasts <- function(mable, newdata, ...)
  11. {
  12. fcast <- forecast(mable, new_data=newdata, ...)
  13. keyvars <- key_vars(fcast)
  14. keyvars <- keyvars[-length(keyvars)]
  15. indexvar <- index_var(fcast)
  16. fcastvar <- names(fcast)[length(keyvars) + 3]
  17. fcast <- fcast %>%
  18. as_tibble() %>%
  19. pivot_wider(
  20. id_cols=all_of(c(keyvars, indexvar)),
  21. names_from=.model,
  22. values_from=.mean)
  23. select(newdata, !!keyvars, !!indexvar, !!fcastvar) %>%
  24. rename(.response=!!fcastvar) %>%
  25. inner_join(fcast)
  26. }
  27. #' Evaluate quality of forecasts given a criterion
  28. #'
  29. #' @param fcast_df A tsibble as returned from `get_forecasts`.
  30. #' @param gof A goodness-of-fit function. The default is to use `fabletools::MAPE`, which computes the mean absolute percentage error.
  31. #' @return
  32. #' A single-row data frame with the computed goodness-of-fit statistic for each model.
  33. eval_forecasts <- function(fcast_df, gof=fabletools::MAPE)
  34. {
  35. if(!is.function(gof))
  36. gof <- get(gof, mode="function")
  37. resp <- fcast_df$.response
  38. keyvars <- key_vars(fcast_df)
  39. indexvar <- index_var(fcast_df)
  40. fcast_df %>%
  41. as_tibble() %>%
  42. select(-all_of(c(keyvars, indexvar, ".response"))) %>%
  43. summarise_all(
  44. function(x, .actual) gof(x - .actual, .actual=.actual),
  45. .actual=resp
  46. )
  47. }