1
votes

I'm trying to fit Arima model and see which order is the best based on AIC i Have the following for statement, my question is how to show the order of the model because it just gives me AIC values and can't determine which model,, mid.ts is time series created by xts the data is as follows:

 mid.ts= 
              deltao
   Jan 1751  -41.23
    Jan 1754 -41.10
    Jan 1756  -40.25
    Jan 1759  -43.61
    Jan 1761  -41.54
    Jan 1764  -39.79
    Jan 1766   -39.63
    Jan 1769  -40.74
    Jan 1771  -42.63
    Jan 1774  -39.47
    Jan 1776  -40.30
    Jan 1778  -40.30
    Jan 1781  -41.56

library(forecast)

for(d in 0:1){
    for(p in 0:9){
      for(q in 0:9){

 fit=Arima(mid.ts,order=c(p,d,q))
  print(AIC(fit))

      }
    }
  }
1
Why not using auto.arima?user3710546
I tried that but it didn't give me the lowest AICmms
I also recommend robjhyndman.com/hyndsight/aicuser3710546
Thanks but I'm trying to see which combination gives the lowest AIC using the loop and if it match the one from the auto.arimamms
Please turn your question into a reproducible one./user3710546

1 Answers

1
votes

You can do it in following two ways:

  1. Based on printing the parameters along with AIC. Here you will have to visually inspect which model is best, which is time consuming and not a good way.

    library(forecast)
    
    for(d in 0:1){
      for(p in 0:9){
        for(q in 0:9){
    
          fit=Arima(mid.ts,order=c(p,d,q))
          print(paste0("AIC is ", AIC(fit), " for d = ", d, ", p = ", p, " and q = ", q))
    
        }
      }
    }
    
  2. Save model parameters in a data frame and then using code to find the optimum parameters. I will favor this method, because it involves less work.

    library(forecast)
    modelAIC <- data.frame()
    for(d in 0:1){
      for(p in 0:9){
        for(q in 0:9){
    
          fit=Arima(mid.ts,order=c(p,d,q))
          modelAIC <- rbind(modelAIC, c(d,p,q,AIC(fit))) #
        }
      }
    }
    names(modelAIC) <- c("d", "p", "q",  "AIC")
    rowNum <- which(modelAIC$AIC==max(modelAIC$AIC))
    modelAIC[rowNum,]#Required model parameters