Reputation: 41
Suppose that I have the following "for" loop. Specifically this code that I built firstly computes the AIC for ARCH(1) models of all specified flavors and secondly computes the AIC for GARCH(1,1) models of the said flavors.
library(rugarch)
bchain_2012_logreturns=diff(log(prices))
aic_2012=matrix(NA,14,1)
garch_flavor=c("GARCH","AVGARCH","TGARCH","GJRGARCH","NGARCH","NAGARCH","APARCH")
k=1
for (i in 0:1){
for (j in garch_flavor){
model_2012=ugarchspec(variance.model = list(model="fGARCH", submodel = j, garchOrder = c(1, i)),mean.model = list(armaOrder = c(0, 0)))
modelfit_2012=ugarchfit(spec=model_2012, data=bchain_2012_logreturns, solver="hybrid")
aic_2012[k,]=infocriteria(modelfit_2012)[1]
k=k+1
}
}
As stated in the "rugarch" package vignette on R CRAN (see link: https://cran.r-project.org/web/packages/rugarch/vignette/Introduction_to_the_rugarch_package.pdf), the "eGARCH" model flavor is NOT included among the family GARCH (i.e. "fGARCH") submodels. The needed command to estimate an eGARCH model with the "rugarch" package is the following:
model_2012=ugarchspec(variance.model = list(model="eGARCH",garchOrder = c(1, 1)),mean.model = list(armaOrder = c(0, 0)))
modelfit_2012=ugarchfit(spec=model_2012, data=bchain_2012_logreturns, solver="hybrid")
What I need to do SIMULTANEOUSLY is to:
1) Integrate the last command for eGARCH estimation into the loop so that I have only one command to compute the AICs of all the 8 GARCH model flavors.
2) Set the "unified" loop to iterate just on eGARCH of order (1,1).
I am a newbie of R programming and this is a non-trivial problem to me.
Thank you in advance.
Upvotes: 0
Views: 925
Reputation: 3597
Using lapply
and conditionals you could do:
#Combine all model names into one vector
model_names =c("eGARCH","GARCH","AVGARCH","TGARCH","GJRGARCH","NGARCH","NAGARCH","APARCH")
#If model name equals "eGARCH" use formula1 else formula2, compute for order (1,1) only
#for other models compute for orders (1,0) and (1,1)
#calculate AIC for each model and rbind to form a combined data.frame
AIC_2012 = do.call(rbind,lapply(model_names,function( x ) {
if ( x=="eGARCH" ){
model_2012_0 = ugarchspec(variance.model = list(model= x,garchOrder = c(1, 1)),mean.model = list(armaOrder = c(0, 0)))
modelfit_2012_0 =ugarchfit(spec=model_2012_0 , data=bchain_2012_logreturns, solver="hybrid")
DF = data.frame(model_name = x,order= "1,1" ,AIC = infocriteria(modelfit_2012_0)[1])
}else {
model_2012_0 = ugarchspec(variance.model = list(model="fGARCH", submodel = x, garchOrder = c(1, 0)),mean.model = list(armaOrder = c(0, 0)))
model_2012_1 = ugarchspec(variance.model = list(model="fGARCH", submodel = x, garchOrder = c(1, 1)),mean.model = list(armaOrder = c(0, 0)))
modelfit_2012_0 =ugarchfit(spec=model_2012_0 , data=bchain_2012_logreturns, solver="hybrid")
modelfit_2012_1 =ugarchfit(spec=model_2012_1, data=bchain_2012_logreturns, solver="hybrid")
DF_0 = data.frame(model_name = x, order= "1,0" , AIC = infocriteria(modelfit_2012_0)[1])
DF_1 = data.frame(model_name = x, order= "1,1" , AIC = infocriteria(modelfit_2012_1)[1])
DF = rbind(DF_0,DF_1)
}
return(DF)
}))
Upvotes: 1