title: Using apply() to generate all subset regression for ARIMA
body: <p>I am trying to generate fitted models for all the combinations for ARIMA(p,0,q), say, for p&lt;=5 and q&lt;=5.</p>
  
  <p>My ultimate goal is to select the best model based on BIC, AIC and RMSE, etc(computed using accuracy).</p>
  
  <p>Using a loop method, I was able to get the desired output, </p>
  
  <pre><code>pdq_index_mat=expand.grid(0L:5L,0L,0L:5L)
  # access the following link for the csv and run the following code to access the data for replication purposes for the following code/error
  # https://drive.google.com/open?id=0ByLULem4QYiEcDB2OTJaejdVUkk
  tsdf&lt;-as.ts(read.csv(file.choose()))
  
  #Loop method
  ic_result_mat&lt;-c()
  reg&lt;-c()
  y&lt;-tsdf[,"psoda"] #dependent variable of interest
  for(i in 1:nrow(pdq_index_mat)){
    # print(as.numeric(pdq_index_mat[i,])) serve as the c(p,0,q) to indicate which ARIMA of interest
    reg&lt;-arima(tsdf[,"psoda"],order=as.numeric(pdq_index_mat[i,]))
    ic_result_mat&lt;-rbind(ic_result_mat,c(BIC=BIC(reg),AIC=AIC(reg),accuracy(reg)[,c("RMSE","MAE","MPE","MAPE")]))
    # uncomment the following line for the first error
    # colnames(tsdf)[ncol(tsdf)+i]&lt;-paste0("ARIMA(",paste0(pdq_index_mat[i,],collapse=","),")")
  }
  colnames(ic_result_mat)&lt;-c("BIC","AIC","RMSE","MAE","MPE","MAPE")
  rownames(ic_result_mat)&lt;-paste0("ARIMA(",do.call(paste,c(pdq_index_mat[1:nrow(ic_result_mat),],sep=",")),")")
  </code></pre>
  
  <p>Except with 1 flaw, I wasn't able to generate rownames inside the loop 
  (eg. ARIMA(0,0,0) for the first row), and prompting the following error:</p>
  
  <pre><code>Error in `rownames&lt;-`(`*tmp*`, value = c(NA, NA, NA, NA, "ARIMA(1,0,0)" : 
    length of 'dimnames' [1] not equal to array extent
  </code></pre>
  
  <p>or</p>
  
  <pre><code> Error in `rownames&lt;-`(`*tmp*`, value = c("ARIMA(0)", "ARIMA(0)", "ARIMA(0)" : 
      attempt to set 'rownames' on an object with no dimensions
  </code></pre>
  
  <p>As for using the <code>apply()</code> method, I am stuck even getting the list of regression</p>
  
  <pre><code>#List method
  ic_result_mat&lt;-c()
  y&lt;-tsdf[,"psoda"] #dependent variable of interest
  reg_lst&lt;-apply(pdq_index_mat,1,function(i)arima(y,order=as.integer(pdq_index_mat[i,])))
  </code></pre>
  
  <p>and prompting the following error:</p>
  
  <pre><code>Error in if (!is.numeric(order) || length(order) != 3L || any(order &lt;  : 
    missing value where TRUE/FALSE needed 
  </code></pre>
  
  <p>Sorry, not a professional coder. Any help from the pro is greatly appreciated. </p>