I have some time series sales data and I want to forecast sales for a given horizon. I use Holt-Winters, ets, Neural Networks and some other methods and then hybridize their results. However, depending on the Holt Winters doesn't always work. I have written two functions, one named forecastWithHoltWinters
and the other is forecastWithoutHoltWinters
and may objective is that is if forecastWithHoltWinters
yields an error, then I need to run the forecastWithoutHoltWinters
. They both have the same inputs, that are salesdata
(time series data) and horizon
(a scalar), and the output is salesforecast
for the given horizon
periods.
Checking the most upvoted answer in How to write trycatch in R , I coded as given below:
forecastWithHoltWinters <- function(salesdata, horizon)
{
salesforecast <- tryCatch(
{
#Lots of lines that forecastWithHoltWinters does
}, error <- function (salesdata, horizon)
{
salesforecast = forecastWithoutHoltWinters(salesdata, horizon)
return (salesforecast)
}
return (salesforecast)
}
I can see that things are so wrong here but I cannot seem to adapt my problem to tryCatch()
. I have gone though http://mazamascience.com/WorkingWithData/?p=912, too. I'll adjust the warnings later, but first I need to deal with errors.
How do I do this?
Edit: I worked on it for a while, there aren't any sourcing errors. Yet, the error <- function (salesdata, horizon)
won't transfer the inputs to the forecastWithoutHoltWinters
function, hence the function isn't working.