[英]Implementation of tryCatch in time-series prediction in R
我有一些時間序列的銷售數據,我想預測給定范圍的銷售。 我使用Holt-Winters,ets,神經網絡和其他一些方法,然后將它們的結果進行混合。 但是,取決於Holt Winters並不總是有效。 我已經編寫了兩個函數,一個命名為forecastWithHoltWinters
,另一個命名為forecastWithoutHoltWinters
,可能的目標是,如果forecastWithHoltWinters
產生錯誤,那么我需要運行forecastWithoutHoltWinters
。 它們都具有相同的輸入,即salesdata
(時間序列數據)和horizon
(標量),並且輸出是給定horizon
期的salesforecast
。
檢查如何在R中編寫trycatch中最合適的答案,我編碼如下:
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 <- function (salesdata, horizon)
不會將輸入傳遞給forecastWithoutHoltWinters
函數,因此該函數無法正常工作。
只需將您的錯誤函數更改為:
error <- function (e)
{print(e)#comment out if you dont want to print error
salesforecast = forecastWithoutHoltWinters(salesdata, horizon)
return (salesforecast)
}
我認為這應該起作用。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.