1

I have wrote a function that forecasts a time series object using different forecast methods like forecast::nnetar, forecast::tbats, forecast::Arima and forecast::ets.
I know that forecastHybrid::hybridModel function is doing this, I just wanted to create something more customized. So I am now returning a list with result$mean, result$error and result$fit. I want to use accuracy or plot function like forecast object. Is there a easy way to do that? or is it too complicated?

EDIT: About the function, it takes a ts object, an arima model-that I found by taking differences and checking ACF-PACF graphs- and a horizon to be forecasted. It applies nnetar, ets, tbats and my arima model.
It combines their fits and forecasts and create a new fit values and forecast values.
It returns a list object with forecasted values as result$mean (this is also same in forecast objects), fitted values as result$fit and errors as result$error.
Now with that returned object, I cant automate some series of works like having accuracy, making plots etc. So I want to return a forecast object if it is possible. that is what it is.

Dave2e
  • 22,192
  • 18
  • 42
  • 50
ali srn
  • 573
  • 5
  • 24
  • 1
    Could you show a reproducible example of the code you're using and/or the object you're returning, and a specific example of what you want to do with it (e.g. what kind of plot)? – David Robinson Sep 08 '16 at 19:19
  • 2
    Voting to close as too broad, ...not to mention unclear about exactly what is expected from data that is not presented. – IRTFM Sep 08 '16 at 19:24
  • edited. @DavidRobinson – ali srn Sep 08 '16 at 19:32
  • 1
    All the different methods in forecast return different classes of output, for example `class(nnetar(lynx)) == "nnetar")`. You need to add a new method to `plot` for it to work with your object. If your object contains all the necessary pieces for `accuracy`, just edit the `accuracy` function. There is a check in the body `if (!any(is.element(class(f), c("mforecast", "forecast", "ts", "integer", "numeric", "Arima", "ets", "lm", "bats", "tbats", "nnetar", "stlm"))))` – Vlo Sep 08 '16 at 20:28

1 Answers1

3

A forecast object is just a list containing a few items, and given a class "forecast". Look at any of the existing functions to see how to do it. Here is a very simple template:

myfun <- function(x, h, ...)
{
  # Compute some forecasts
  fc <- ....
  # Construct output list
  output <- list(mean=fc, x=x, ...)
  # Return with forecasting class
  return(structure(output, class='forecast'))
}
Rob Hyndman
  • 30,301
  • 7
  • 73
  • 85