1

I'm running a for loop to do Arima using R. My for loop will change the p,d,q value & run the arima & store the p-d-q value in a data frame. But in this process some p-d-q value throw errors & my for loop is getting stopped.I don't want my for loop to stop in middle without completing the complete loops. Is it possible to not interrupt my for loop & store all p-d-q value except the error?

R Learner
  • 545
  • 1
  • 7
  • 13
  • Besides the straightforward `try` Ricardo proposed (which is all I've ever used), you may want to look at this question: http://stackoverflow.com/questions/2622777/exception-handling-in-r – Frank May 18 '13 at 04:43

1 Answers1

3

you are looking for try. (or altenatively, tryCatch)

I'm assuming you are running something like this:

  for (p in ..)
    for (q in...)
  ...

  mod <- arima(x, c(p,d,q))

If so, simply change that last line to

  mod <- try(arima(x, c(p,d,q)), silent=TRUE)
 # the silent is optional

However, you are probably better off doing:

 pdq <- expand.grid(p, d, q)
 apply(pdq, 1, function(o) try(arima(x, o), silent=TRUE))

Lastly, make sure that you're not just fishing http://xkcd.com/882/

Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
  • Thanks for your reply Ricardo. I'm using the following code after modifying what you suggested – R Learner May 18 '13 at 06:17
  • Code is available at https://skydrive.live.com/#cid=FF431A41D367C7D9&id=FF431A41D367C7D9%21105 Still I'm using following error Error in fit$aic : $ operator is invalid for atomic vectors In addition: Warning messages: 1: In log(s2) : NaNs produced 2: In log(s2) : NaNs produced 3: In log(s2) : NaNs produced 4: In log(s2) : NaNs produced Can you please help me to resolve the error? – R Learner May 18 '13 at 06:24
  • the error is telling you that `$` doesn't work with `fit`. I'm guessing you are assigning something wrong to fit. Please understand that asking someone to sift through your code is a relatively tall order. Instead, consider trying to trouble shoot it yourself and when you encounter a specific issue, narrow it down to the offending section or lines and then asking a concrete question about it. – Ricardo Saporta May 18 '13 at 07:51
  • Thanks for your response Ricardo. Narrowing down it further, What I'm doing is storing Arima result in fit & then using fit to get several outcome,like AIC,predict future response & calculate MAPE. Now when I'm putting fit=try(Arima(..),silent=T). it is handling Arima error but still it is throwung error as it is not getting proper value of fit for fit$aic. So my question is-> Can I put multiple line of codes inside "try" block? if so how? I tried separate try block for each line of code but I'm not getting the output in proper format – R Learner May 18 '13 at 08:50