# Simulate ARIMA by hand

I was working on ARIMA in R and I am trying not to use library forecast as much as possible. I have a code for finding the best ARIMA model, but it is showing some warning messages.

Here is my function:

best.aic<-1e8
n<-length(x.ts)
for(p in 0:maxord[1]) for(d in 0:maxord[2]) for(q in 0:maxord[3])
{
fit<-arima(x.ts,order=c(p,d,q))
fit.aic<--2*fit$loglik+(log(n)+1)*length(fit$coef)
if(fit.aic<best.aic)
{
best.aic<-fit.aic
best.fit<-fit
best.model<-c(p,d,q)
}
}
list(best.aic,best.fit,best.model)


It gives the same warning 4 times:

In arima(x.ts, order = c(p, d, q)) :
possible convergence problem: optim gave code=1


Also, I will appreciate any help concerning simulation of ARIMA by hand, not by arima.sim function. Thank you.

• Please take in mind that I am new to both arima and R (started working in Jan this year), hence please make the answers simpler, however any help is appreciated. Commented Mar 23, 2013 at 3:05
• You appear to be asking two fairly unrelated questions (and your title doesn't relate to the first one at all). You should probably ask two different questions. Please take care that they both fall inside the bounds for CV questions given in the faq. Commented Mar 23, 2013 at 3:54
• Sorry, I will try to be more careful. However is there something wrong in the above code. As for arima, is there any place where I can find its implementation, or can get enough understanding to implement it myself. Commented Mar 23, 2013 at 5:33

The warning is because the normal optimization for the MLE has reached the default maximum number of iterations before convergence. You can increase that by adding optim.control = list(maxit = ?) at the end of your ARIMA fit. Something like arima(x.ts,order=c(p,d,q),list(maxit = 1000)). I think the default for ? is 500 but you can increase it.