问题描述
假设我有以下"for"循环.具体来说,我构建的这段代码首先计算所有指定风味的ARCH(1)模型的AIC,其次计算上述风味的GARCH(1,1)模型的AIC.
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
}
}
如R CRAN上的"rugarch"包小插图中所述(请参阅链接: https://cran.r-project.org/web/packages/rugarch/vignette/Introduction_to_the_rugarch_package.pdf ),"eGARCH"模型类型不包括在GARCH系列中(即"fGARCH")子模型.使用"rugarch"软件包估算eGARCH模型所需的命令如下:
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")
我需要同时做的是:
1)将用于eGARCH估计的最后一个命令集成到循环中,这样我只有一个命令可以计算所有8种GARCH模型类型的AIC.
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)设置统一"循环以仅对顺序为(1,1)的eGARCH进行迭代.
2) Set the "unified" loop to iterate just on eGARCH of order (1,1).
我是R编程的新手,这对我来说不是一个简单的问题.
I am a newbie of R programming and this is a non-trivial problem to me.
谢谢.
推荐答案
使用lapply
和条件语句,您可以执行以下操作:
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)
}))
这篇关于通过fGARCH风味模型将EGARCH风味添加到循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!