【问题标题】:Implementation of tryCatch in time-series prediction in RR中时间序列预测中tryCatch的实现
【发布时间】:2016-02-05 11:52:13
【问题描述】:

我有一些时间序列销售数据,我想预测给定范围内的销售。我使用 Holt-Winters、ets、神经网络和其他一些方法,然后混合它们的结果。但是,取决于 Holt Winters 并不总是有效。我编写了两个函数,一个名为forecastWithHoltWinters,另一个是forecastWithoutHoltWinters,可能目标是如果forecastWithHoltWinters 产生错误,那么我需要运行forecastWithoutHoltWinters。它们都有相同的输入,即salesdata(时间序列数据)和horizon(标量),对于给定的horizon 周期,输出是salesforecast

检查How to write trycatch in R 中最受好评的答案,我编码如下:

forecastWithHoltWinters <- function(salesdata, horizon)
{
     salesforecast <- tryCatch(
     {
        #Lots of lines that forecastWithHoltWinters does
     }, error <- function (salesdata, horizon)
     {
         salesforecast = forecastWithoutHoltWinters(salesdata, horizon)
         return (salesforecast)
     }
return (salesforecast)
}

我可以看到这里的情况很不对,但我似乎无法将我的问题适应tryCatch()。我也去过http://mazamascience.com/WorkingWithData/?p=912。稍后我会调整警告,但首先我需要处理错误。

我该怎么做?

编辑:我研究了一段时间,没有任何采购错误。然而,error &lt;- function (salesdata, horizon) 不会将输入传输到forecastWithoutHoltWinters 函数,因此该函数不起作用。

【问题讨论】:

    标签: r try-catch time-series


    【解决方案1】:

    只需将您的错误函数更改为:

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

    我认为这应该可行。 :)

    【讨论】:

    • 其实是forecastWithoutHoltWinters函数,我这里打错了。我编辑了它。谢谢。
    • tryCatch 代码对我来说看起来不错。只有在 forecastWithoutHoltWinters 函数失败时,您的代码才会遇到问题。如果这没有发生,那么您的代码应该运行。
    • 也许我应该详细说明错误。我说#Lots of lines that forecastWithHoltWinters does 的部分以变量定义clforecast=matrix(,nrow=5,ncol=horizon) 开头,错误出现在第一个字母中。这与大括号无关。
    • 知道了!这不是关于大括号,而是关于括号。谢谢。
    • 但是,它仍然不会运行,error &lt;- function (salesdata, horizon) 不会接受输入。因此forecastWithoutHoltWinters 不起作用。
    猜你喜欢
    • 2015-05-19
    • 2013-09-26
    • 2014-04-03
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2018-09-21
    • 2016-11-27
    相关资源
    最近更新 更多