loop function for ugarchfit function in R

180 views Asked by At

I have time-series data. I have returned the data and got the log returns. I aim to select the best fit ARMA-GARCH 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:

   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)

and so on. That is, lets ARMA(p,q) range from 1:6, and GARCH(p,q) also from 1:6. How to do this using loop function in R. I would like to return the smallest criteria (AIC) among all the models). In other words, I would like my function to loop between different orders automatically.

Here is my try:

GarchWarp <- 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 <- function(n,dat,d){

meanModel <- varModel <- list()
  if(n > 1){
    for(i in 1:n){
  
  meanModel[i] <- list(armaOrder = c(i,i)) # ARMA
  varModel <- list(model = "sGARCH", garchOrder = c(i,i)) # GARCH
  uspec <- ugarchspec(varModel, mean.model = meanModel,
                      distribution.model = "std") # scaled Student t
  fitPC <- apply(dat, 2, function(x) ugarchfit(uspec, data = x))
  Recrit <- lapply(1:d, function(i) infocriteria(fitPC[[i]]))
    }
  }
  return(Recrit)
}

My loop function did not work

Sample of my data: d=2.

    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("xts", "zoo"), index = structure(c(1514851200, 
1514937600, 1515024000, 1515110400, 1515369600, 1515456000), tzone = "UTC", tclass = c("POSIXct", 
"POSIXt")), .Dim = c(6L, 2L), .Dimnames = list(NULL, c("P_Sha", 
"P_Jap")))
0

There are 0 answers