title: loop function for ugarchfit function in R
body: <p>I have time-series data. I have returned the data and got the log returns. I aim to select the best fit <code>ARMA-GARCH</code> model from a list of models. Instead of fitting each model separately, I want to fit these models at once and select the best among them. Hence, I make a warp function to do the job for me. For example, I would like to fit several models:</p>
  <pre><code>   1- ARMA(1,1)-GARCH(1,1)
   
     2-  ARMA(1,2)-GARCH(1,1)
  
     3-  ARMA(1,3)-GARCH(1,1)
  
     4-  ARMA(2,1)-GARCH(1,1)
  
     5-  ARMA(2,2)-GARCH(1,1)
  
     6-  ARMA(2,3)-GARCH(1,1)
  
     .. ..
  
  1-  ARMA(1,1)-GARCH(1,2)
  
  2-  ARMA(1,1)-GARCH(1,3)
  
  3-  ARMA(1,1)-GARCH(2,1)
  </code></pre>
  <p>and so on. That is, lets <code>ARMA(p,q)</code> range from <code>1:6</code>, and <code>GARCH(p,q)</code> also from <code>1:6</code>. How to do this using <code>loop</code> function in R. <strong>I would like to return the smallest criteria (<code>AIC</code>) among all the models).</strong> In other words, I would like my function to loop between different orders automatically.</p>
  <p>Here is my try:</p>
  <pre><code>GarchWarp &lt;- function(n,dat,d){ ## n is the order of the model. d is the dimension of the data, for example, one series, two series. 
  
      GarchWarp &lt;- function(n,dat,d){
  
  meanModel &lt;- varModel &lt;- list()
    if(n &gt; 1){
      for(i in 1:n){
    
    meanModel[i] &lt;- list(armaOrder = c(i,i)) # ARMA
    varModel &lt;- list(model = &quot;sGARCH&quot;, garchOrder = c(i,i)) # GARCH
    uspec &lt;- ugarchspec(varModel, mean.model = meanModel,
                        distribution.model = &quot;std&quot;) # scaled Student t
    fitPC &lt;- apply(dat, 2, function(x) ugarchfit(uspec, data = x))
    Recrit &lt;- lapply(1:d, function(i) infocriteria(fitPC[[i]]))
      }
    }
    return(Recrit)
  }
  </code></pre>
  <p><strong>My loop function did not work</strong></p>
  <p><strong>Sample of my data: <code>d=2</code>.</strong></p>
  <pre><code>    structure(c(0.00618750144876756, 0.00491561236137983, 0.00178237912109402, 
  0.0052134813154332, 0.00129554617030614, 0.00232162478086728, 
  0, 0.0320481134224817, 0.00881819387210214, 0, 0.00569585759405733, 
  -0.00259413863757629), class = c(&quot;xts&quot;, &quot;zoo&quot;), index = structure(c(1514851200, 
  1514937600, 1515024000, 1515110400, 1515369600, 1515456000), tzone = &quot;UTC&quot;, tclass = c(&quot;POSIXct&quot;, 
  &quot;POSIXt&quot;)), .Dim = c(6L, 2L), .Dimnames = list(NULL, c(&quot;P_Sha&quot;, 
  &quot;P_Jap&quot;)))
  </code></pre>