ali - 9 months ago 76

R Question

I have wrote a function that forecasts a time series object using different forecast methods like

`forecast::nnetar`

`forecast::tbats`

`forecast::Arima`

`forecast::ets`

I know that

`forecastHybrid::hybridModel`

`result$mean`

`result$error`

`result$fit`

EDIT: About the function, it takes a

`ts`

`nnetar`

`ets`

`tbats`

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`

`result$fit`

`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.

Answer

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'))
}
```

Source (Stackoverflow)