문제

I have a large number of time series for which I would like to generate forecasts. In order to automatically generate the best forecast I would like to apply a number of models such as auto.arima, ets, (s)naive, neural network etc. Unfortunately, when it loops through the time series some models fail, which stop the execution of the R script. In order to make this more robust I started using tryCatch(); my main objective with this is that I would the script to continue not necessary catching the errors. When executing the code, the forecast() within the tryCatch() fails to produce a proper forecast.

Please find below a reproducable example of the error that I am encounter.

Historical timeseries:

ts <- structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 9, 10, 10, 16, 7, 13, 0, 9, 1, 11, 2, 11, 3, 
11, 4, 1, 20, 13, 13, 13, 9, 14, 16, 16, 18, 17, 20, 18, 19, 
16, 16, 16, 15, 14, 27, 24, 35, 8, 18, 21, 20, 19, 22, 18, 21, 
19, 24, 33, 23, 18, 26, 18, 17, 19, 19, 22, 19, 24, 29, 29, 18
), .Tsp = c(2025.25, 2032.16666666667, 12), class = "ts")

The following line of code produces a proper forecast:

fcast_arima <- forecast(auto.arima(ts),h=4)
fcast_arima
         Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
Apr 2032       24.69032 18.57094 30.80971 15.33153 34.04911
May 2032       25.00539 18.84018 31.17061 15.57651 34.43428
Jun 2032       25.32046 19.10975 31.53118 15.82200 34.81893
Jul 2032       25.63554 19.37966 31.89141 16.06800 35.20307

When I wrap the same code into a tryCatch line it fails to produce a forecast, see example below:

fcast_arima <- tryCatch({ fcast_arima <- forecast(auto.arima(ts),h=4)}, warning = function(warningcondition) {message(warningcondition)}, error = function(errorcondition) {message(errorcondition)}, finally={})
p-value smaller than printed p-value
fcast_arima
NULL

Can anybody explain why the code that is working fine without the tryCatch() is not working within a tryCatch()?

도움이 되었습니까?

해결책

I'm thinking that something is suppressing warnings by setting options(warn=-1), and generating a warning. I can simulate the behaviour you are seeing here.

Create a function that generates a warning and returns 99 but with the warning suppressed by options:

> foo=function(){o=options()$warn; options(warn=-1);warning("Yikes!");options(warn=o);return(99)}

That seems to run nicely:

> foo()
[1] 99

And I can run it in a simple tryCatch:

> tryCatch(foo())
[1] 99

But the warning gets trapped if I add a warning clause:

> tryCatch(foo(),warning=function(w){message(w)})
Yikes!> 

Although it still seems to run without warnings:

> foo()
[1] 99

I'm not sure what the solution is... Sorry... Perhaps getting whoever wrote the code that tries to suppress the warning to rewrite it to use suppressWarnings instead:

> foo=function(){suppressWarnings({warning("Yikes!")}) ; return(99)}
> foo()
[1] 99
> tryCatch(foo(),warning=function(w){message(w)})
[1] 99
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top