0

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.

Community
  • 1
  • 1
ayca altay
  • 51
  • 1
  • 8

1 Answers1

1

Just change your error function to:

error <- function (e)
     {print(e)#comment out if you dont want to print error
      salesforecast = forecastWithoutHoltWinters(salesdata, horizon)
      return (salesforecast)
     }

This I think should work. :)

Kumar Manglam
  • 2,780
  • 1
  • 19
  • 28
  • It is actually `forecastWithoutHoltWinters` function, I mistyped here. I edited it. Thank you. – ayca altay Feb 05 '16 at 12:18
  • 1
    The tryCatch code looks fine to me. Your code will encounter problem only when forecastWithoutHoltWinters function will fail. If that does not happen, then your code should run. – Kumar Manglam Feb 05 '16 at 12:31
  • Maybe I should elaborate on the error. The part where I say `#Lots of lines that forecastWithHoltWinters does` starts with a variable definition, `clforecast=matrix(,nrow=5,ncol=horizon)` and the error is in the first letter. It is not about braces. – ayca altay Feb 05 '16 at 13:04
  • Got it! It's not about braces, but it's about brackets. Thank you. – ayca altay Feb 05 '16 at 13:15
  • However, it still won't run and `error <- function (salesdata, horizon)` won't take inputs. Hence `forecastWithoutHoltWinters` isn't working. – ayca altay Feb 05 '16 at 13:41
  • I edited my answer. Please check if it works. If yes, Please upvote answer. :) – Kumar Manglam Feb 05 '16 at 14:00
  • Thank you, it partly worked. I'm not getting any source errors but the `forecastWithoutHoltWinters` isn't taking any inputs. – ayca altay Feb 05 '16 at 14:04