[R] arima crashes too

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri Oct 23 14:06:04 CEST 2009


On Fri, Oct 23, 2009 at 12:32 PM, Alberto Monteiro
<albmont at centroin.com.br> wrote:

> I mean that, if I run a loop, it doesn't finish. Or, more
> catastrophically, if I am running a loop and saving data to an
> open file, it terminates the loop and does not close the file.
>
> Reproducible example:
>
> test.arima <- function() {
>  lets.crash.arima <- c(71, 78, 95, 59) # , 113
>  for (x in 90:120) {
>    reg <- arima(c(lets.crash.arima, x), order = c(1,0,0))
>    cat("ok for x =", x, "\n")
>  }
>  cat("close file and prepare a nice summary\n")
>  return("arima passed the test")
> }
>
> test.arima()
>
> As you can see, the loop aborts, the function never returns, with
> potentially nasty effects (namely: I have to finish R with q() to
> close the files and examine them).

 If you're doing anything in a loop that has the potential to fail
because of singularities or other conditions when your model can't be
fitted, you need to stick what you are doing in a 'try' clause. This
lets you trap errors and do something with them.

 Plenty of examples in help(try) or this from me:

 for(i in 1:10){
 print(solve(matrix(c(3,3,3,i),2,2)))
 }

 This stops the loop at i=3. Now stick it in a try() clause:

 for(i in 1:10){
 print(try(solve(matrix(c(3,3,3,i),2,2))))
 }

 and it gives a warning and carries on. If you want your code to do
something with the failure cases then the help for try() tells you
what to look for.

 I'm not sure why your arima produces an error, but I'm assuming the
numbers are such that the model can't be fitted. I don't really know
what arima is doing.

Barry




More information about the R-help mailing list