Looping with arima in R

Viewed 550

I am trying to do multiple arimas with the for function.

So far my attempt is this.

for(p in 0:20){
  for(q in 0:20){
    for (d in 0:3) {
      fit <- arima(y, order=c(p,d,q),method="ML")
      acc <- accuracy(fit)
      print(p);print(d);print(q)
    }
    }
      
}

I want to get all the accuracy vectors in one dataset, for every arima, with 3 extra columns with p,d,q.

Then I'd like to rescue the log-likelihood of the model and AIC

So the final output should be a dataframe that looks like this, where every line is one model.

                   ME     RMSE      MAE         MPE     MAPE     MASE       ACF1 loglikeli  AIC   p   d  q
Training set       x       x         x           x       x        x          x    x          x    x   x  x

Training set       w       w         w           w       w        w          w    w          w    w   w  w 

Training set       y       y         y           y       y        y          y    y          y    y   y  y

```
2 Answers

You're almost there. The easiest way to do this is to iteratively add the summary of each Arima object to a data frame.

The code below does what you need (I've reduced the number of iterations otherwise it takes too long).

# load package
library(forecast)

# create some data
y <- rnorm(100)

# object to store arima summary in
model_smry <- data.frame()

# loop to store
for(p in 1:2){
  for(q in 1:2){
    for (d in 1:2) {
      fit <- arima(y, order=c(p,d,q),method="ML")
      acc <- accuracy(fit)
      
      # gather everything into a single data frame 
      acc_ext <- data.frame(# information from accuracy function
                            acc,
                            # goodness of fit
                            loglikeli = logLik(fit),
                            AIC = AIC(fit), 
                            # arima order
                            p,
                            q,
                            d)
      
      # add arima summary
      model_smry <- rbind(model_smry, acc_ext)
   
    }
  }
  
}

# show summary
model_smry

Output:


                       ME      RMSE       MAE        MPE     MAPE      MASE         ACF1 loglikeli      AIC p q d
Training set   0.03590650 0.8270888 0.6536260   61.96955 124.9386 0.6845685 -0.006412280 -124.4806 254.9612 1 1 1
Training set1 -0.03384812 0.9791048 0.7565525 -540.90163 825.9039 0.7923675 -0.129324621 -140.8802 287.7604 1 1 2
Training set2  0.03709185 0.8225973 0.6502470   95.99749 134.0075 0.6810295  0.026597486 -123.9961 255.9921 1 2 1
Training set3 -0.04914004 0.8317765 0.6574596  -51.59546 250.4728 0.6885836 -0.013578522 -129.2061 266.4123 1 2 2
Training set4  0.03698832 0.8239479 0.6516438   26.90046 162.6580 0.6824924  0.001452607 -124.0094 256.0188 2 1 1
Training set5 -0.04342442 0.9527430 0.7233051  -39.01621 319.2866 0.7575462 -0.050439230 -138.4554 284.9108 2 1 2
Training set6  0.03606286 0.8227565 0.6522152  -30.19270 220.3092 0.6830908 -0.003839680 -123.8827 257.7654 2 2 1
Training set7 -0.05099161 0.8291406 0.6503652  -91.41328 289.5055 0.6811533 -0.004315754 -128.5307 267.0613 2 2 2

Instead of nested loops as in the original question and the answer of @ralph, it is also possible to use a function and then call it vectorized with apply. This style is better modularized and easier to debug:

library(forecast)

# create some data
set.seed(456)
y <- rnorm(100)

# create a matrix of all desired orders
pqd <- expand.grid(p=1:2, q=1:2, d=1:2)

# a function that does the analysis for a single case
fit_arima <- function(ord) {
  fit <- arima(y, order = ord, method = "ML")
  acc <- accuracy(fit)
  c(acc = acc, loglikeli = logLik(fit),  AIC = AIC(fit))
}

# test a single case
fit_arima(c(p=1, q=1, d=1))

# run all
ret <- apply(pqd, 1, fit_arima)

# bind input and results together
cbind(pqd, t(ret))
Related