Rugarch - 估算各种错误分布和输入数据的模型

时间:2017-01-28 11:28:58

标签: r loops lapply

我是R编程初学者。

我需要在估算函数的两个不同参数上按以下顺序重复以下后答案Adding EGARCH flavor into a loop over fGARCH-flavor models的代码。

重申的第一个论点是错误分布。这是应该在模型估计命令中改变的向量(参见链接的帖子):

error_distribution=c("norm","std","ged")

model_2012=ugarchspec(variance.model = list(model="fGARCH", submodel = j, garchOrder = c(1, i)),mean.model = list(armaOrder = c(0, 0)),distribution.model=z)

modelfit_2012=ugarchfit(spec=model_2012, data=bchain_2012_logreturns, solver="hybrid") 

重申的第二个论点是输入数据,它由包含2012 - 2016年的对数回报的五个向量给出,如下所示:

bchain_2012_logreturns
bchain_2013_logreturns 
bchain_2014_logreturns
bchain_2015_logreturns  
bchain_2016_logreturns 

预期结果是单个数据框架,根据各自的AIC,每年对45个GARCH模型进行排名。请参阅以下输出:

   model_name error_distribution order         AIC model_name error_distribution order         AIC model_name error_distribution order         AIC
31     eGARCH                ged (1,1) -4.75865287     TGARCH                ged (1,1) -3.08102463     eGARCH                ged (1,1) -4.05490216
33      GARCH                ged (1,1) -4.75652725    AVGARCH                ged (1,1) -3.08045023     TGARCH                ged (1,1) -4.05374702
43    NAGARCH                ged (1,1) -4.75490561    AVGARCH                std (1,1) -3.07788929    AVGARCH                ged (1,1) -4.05331759
39   GJRGARCH                ged (1,1) -4.75278902     TGARCH                std (1,1) -3.07725803      GARCH                ged (1,1) -4.05294280
41     NGARCH                ged (1,1) -4.75108028     NGARCH                ged (1,1) -3.07641387     NGARCH                ged (1,1) -4.05211362
37     TGARCH                ged (1,1) -4.74863010     APARCH                ged (1,1) -3.07511457    NAGARCH                ged (1,1) -4.05184210
45     APARCH                ged (1,1) -4.74737629   GJRGARCH                ged (1,1) -3.07409228   GJRGARCH                ged (1,1) -4.05104571
35    AVGARCH                ged (1,1) -4.74587528      GARCH                ged (1,1) -3.07400130     APARCH                ged (1,1) -4.05064538
16     eGARCH                std (1,1) -4.69989618    NAGARCH                ged (1,1) -3.07318299    AVGARCH                std (1,1) -4.04762280
18      GARCH                std (1,1) -4.69719283     APARCH                std (1,1) -3.07315401     TGARCH                std (1,1) -4.04601307
28    NAGARCH                std (1,1) -4.69405444     eGARCH                std (1,1) -3.07269706     eGARCH                std (1,1) -4.04406967
26     NGARCH                std (1,1) -4.69404219     NGARCH                std (1,1) -3.06768570    NAGARCH                std (1,1) -4.04226303
24   GJRGARCH                std (1,1) -4.69226266     eGARCH                ged (1,1) -3.06481282     APARCH                std (1,1) -4.04141302
30     APARCH                std (1,1) -4.68892186    NAGARCH                std (1,1) -3.05214076   GJRGARCH                std (1,1) -4.03922662
20    AVGARCH                std (1,1) -4.68635636   GJRGARCH                std (1,1) -3.04994480     NGARCH                std (1,1) -4.03902956
22     TGARCH                std (1,1) -4.68545602      GARCH                std (1,1) -3.04671032      GARCH                std (1,1) -4.03587846
17      GARCH                std (1,0) -4.63367371     NGARCH                std (1,0) -2.96723963     NGARCH                std (1,0) -4.02141342
27    NAGARCH                std (1,0) -4.63086175     TGARCH                std (1,0) -2.96703818     APARCH                std (1,0) -4.01926563
25     NGARCH                std (1,0) -4.62854627      GARCH                std (1,0) -2.96419867     TGARCH                std (1,0) -4.01739401
23   GJRGARCH                std (1,0) -4.62836367     APARCH                std (1,0) -2.96180909    NAGARCH                std (1,0) -4.00275498
21     TGARCH                std (1,0) -4.62233634    NAGARCH                std (1,0) -2.96011709      GARCH                std (1,0) -4.00197866
19    AVGARCH                std (1,0) -4.60732059   GJRGARCH                std (1,0) -2.95925904   GJRGARCH                std (1,0) -3.99942924
29     APARCH                std (1,0) -4.60040491    AVGARCH                std (1,0) -2.93447870    AVGARCH                std (1,0) -3.97651737
1      eGARCH               norm (1,1) -4.37707925    AVGARCH               norm (1,1) -2.84142521     eGARCH               norm (1,1) -3.89143994
5     AVGARCH               norm (1,1) -4.36877118   GJRGARCH               norm (1,1) -2.83307523      GARCH               norm (1,1) -3.88777006
7      TGARCH               norm (1,1) -4.35992439      GARCH               norm (1,1) -2.83172032     NGARCH               norm (1,1) -3.88227550
9    GJRGARCH               norm (1,1) -4.35675975    NAGARCH               norm (1,1) -2.83096966    NAGARCH               norm (1,1) -3.88075879
13    NAGARCH               norm (1,1) -4.35669056     NGARCH               norm (1,1) -2.82936979     TGARCH               norm (1,1) -3.87732087
15     APARCH               norm (1,1) -4.35639445     APARCH               norm (1,1) -2.82908704     APARCH               norm (1,1) -3.87128476
3       GARCH               norm (1,1) -4.34457950     TGARCH               norm (1,1) -2.82664751     NGARCH               norm (1,0) -3.86962700
11     NGARCH               norm (1,1) -4.34333420     eGARCH               norm (1,1) -2.79347854   GJRGARCH               norm (1,1) -3.86727511
8    GJRGARCH               norm (1,0) -4.09031551     NGARCH               norm (1,0) -2.71997549    AVGARCH               norm (1,1) -3.86704031
2       GARCH               norm (1,0) -4.08846458      GARCH               norm (1,0) -2.71897732      GARCH               norm (1,0) -3.79428684
14     APARCH               norm (1,0) -4.08213105     APARCH               norm (1,0) -2.71487076    NAGARCH               norm (1,0) -3.79161679
10     NGARCH               norm (1,0) -4.06826972    NAGARCH               norm (1,0) -0.87217161    AVGARCH               norm (1,0) -3.73878504
4     AVGARCH               norm (1,0) -3.97389330    AVGARCH               norm (1,0) -0.30823178     APARCH               norm (1,0) -3.73584478
12    NAGARCH               norm (1,0) -3.95954524     TGARCH                ged (1,0)  0.03351648     TGARCH                ged (1,0)  0.03351648
6      TGARCH               norm (1,0) -1.08711502    AVGARCH                ged (1,0)  0.03901099    AVGARCH                ged (1,0)  0.03901099
36     TGARCH                ged (1,0)  0.03342466     APARCH                ged (1,0)  0.03901099     APARCH                ged (1,0)  0.03901099
34    AVGARCH                ged (1,0)  0.03890411   GJRGARCH               norm (1,0)  1.05494307   GJRGARCH               norm (1,0)  3.06619678
44     APARCH                ged (1,0)  0.03890411     TGARCH               norm (1,0)  1.66969764     TGARCH               norm (1,0) 13.23452126
42    NAGARCH                ged (1,0)  8.03028586    NAGARCH                ged (1,0) 30.06384244   GJRGARCH                ged (1,0) 32.95929809
32      GARCH                ged (1,0)  8.13606127      GARCH                ged (1,0) 30.29489274    NAGARCH                ged (1,0) 33.19740991
40     NGARCH                ged (1,0)  8.14154072     NGARCH                ged (1,0) 30.30038725      GARCH                ged (1,0) 33.20214247
38   GJRGARCH                ged (1,0)  8.16366093   GJRGARCH                ged (1,0) 30.34334891     NGARCH                ged (1,0) 33.20763698
   model_name error_distribution order          AIC model_name error_distribution order         AIC
31     APARCH                ged (1,1)  -4.43376898      GARCH                ged (1,1) -5.23989609
33     NGARCH                ged (1,1)  -4.43125597    NAGARCH                ged (1,1) -5.23846855
43      GARCH                ged (1,1)  -4.43024900   GJRGARCH                ged (1,1) -5.23694535
39   GJRGARCH                ged (1,1)  -4.42919245     NGARCH                ged (1,1) -5.23441634
41    NAGARCH                std (1,1)  -4.42526122     APARCH                ged (1,1) -5.23278501
37    AVGARCH                ged (1,1)  -4.41838285     eGARCH                std (1,1) -5.23184947
45     eGARCH                ged (1,1)  -4.41674667    AVGARCH                std (1,1) -5.23141871
35   GJRGARCH                std (1,1)  -4.41582574     NGARCH                std (1,1) -5.23124651
16     APARCH                std (1,1)  -4.41051921     eGARCH                ged (1,1) -5.23097095
18      GARCH                std (1,1)  -4.40684699    NAGARCH                std (1,1) -5.22847769
28     eGARCH                std (1,1)  -4.40379536     TGARCH                ged (1,1) -5.22758361
26     NGARCH                std (1,1)  -4.40146116      GARCH                std (1,1) -5.22727192
24     TGARCH                ged (1,1)  -4.40045970     TGARCH                std (1,1) -5.22670653
30     TGARCH                std (1,1)  -4.39302792   GJRGARCH                std (1,1) -5.22562117
20    AVGARCH                std (1,1)  -4.38983868    AVGARCH                ged (1,1) -5.22542480
22      GARCH                std (1,0)  -4.38731276    AVGARCH                std (1,0) -5.21402359
17    NAGARCH                std (1,0)  -4.38699935    NAGARCH                std (1,0) -5.20394522
27    NAGARCH                ged (1,1)  -4.38672794     APARCH                std (1,1) -5.19630862
25   GJRGARCH                std (1,0)  -4.38216336      GARCH                std (1,0) -5.18779640
23     NGARCH                std (1,0)  -4.38182166     NGARCH                std (1,0) -5.18468472
21    AVGARCH                std (1,0)  -4.37746757   GJRGARCH                std (1,0) -5.18242298
19     APARCH                std (1,0)  -4.37668704     APARCH                std (1,0) -5.17696075
29     TGARCH                std (1,0)  -4.36889276     TGARCH                std (1,0) -5.16984529
1      APARCH               norm (1,1)  -4.22868663      GARCH               norm (1,1) -4.86762096
5      NGARCH               norm (1,1)  -4.22662065   GJRGARCH               norm (1,1) -4.86355228
7       GARCH               norm (1,1)  -4.18924653     NGARCH               norm (1,1) -4.86339583
9    GJRGARCH               norm (1,1)  -4.18544590    NAGARCH               norm (1,1) -4.86270926
13    NAGARCH               norm (1,1)  -4.18480391     eGARCH               norm (1,1) -4.85324513
15     eGARCH               norm (1,1)  -4.16017862    AVGARCH               norm (1,1) -4.84896561
3      NGARCH               norm (1,0)  -4.13398631     TGARCH               norm (1,1) -4.84311948
11    AVGARCH               norm (1,1)  -4.12548973    NAGARCH               norm (1,0) -4.76807819
8      APARCH               norm (1,0)  -4.12331541     APARCH               norm (1,1) -4.75358590
2     NAGARCH               norm (1,0)  -4.11927591      GARCH               norm (1,0) -4.73585352
14     TGARCH               norm (1,1)  -4.09717462     NGARCH               norm (1,0) -4.72947512
10   GJRGARCH               norm (1,0)  -0.03847664     APARCH               norm (1,0) -4.72645043
4      TGARCH                ged (1,0)   0.03351648    AVGARCH               norm (1,0) -3.71073192
12    AVGARCH                ged (1,0)   0.03901099   GJRGARCH               norm (1,0) -0.85666042
6      APARCH                ged (1,0)   0.03901099     TGARCH               norm (1,0) -0.40795391
36      GARCH               norm (1,0)   5.28913383     TGARCH                ged (1,0)  0.03342466
34     TGARCH               norm (1,0)  22.84463294    AVGARCH                ged (1,0)  0.03890411
44   GJRGARCH                ged (1,0)  31.04458936     APARCH                ged (1,0)  0.03890411
42      GARCH                ged (1,0)  31.04836775      GARCH                ged (1,0) 15.92143895
32    NAGARCH                ged (1,0)  31.04957524     NGARCH                ged (1,0) 15.92691840
40     NGARCH                ged (1,0)  31.05386225   GJRGARCH                ged (1,0) 16.00220144
38    AVGARCH               norm (1,0) 128.26641562    NAGARCH                ged (1,0) 16.00801808

实现预期结果的一种天真方法是在每次迭代时手动更改两个必需参数,对五个AIC数据帧进行排序,最后将它们连接成一个数据帧。

实现预期结果的更好方法可能是使用两个" for"在继续订购和连接AIC数据帧之前进行循环。但是,我无法将这两个循环插入到lapply函数中以产生预期结果。那么,为此目的的任何建议?你能指出任何其他更智能的解决方案给R初学者吗?

1 个答案:

答案 0 :(得分:1)

使用expand.grid,我们可以创建诸如(“bchain_2012_logreturns”,“norm”),(“bchain_2012_logreturns”,“std”)等的组合。

对于每个组合,我们调用自定义函数来计算所有模型的AIC。此功能是我之前解决方案的修改版本。

由于缺乏数据,我无法验证结果,但这应该有效。

创建组合:

error_distribution=c("norm","std","ged")

dataList = c("bchain_2012_logreturns","bchain_2013_logreturns","bchain_2014_logreturns","bchain_2015_logreturns","bchain_2016_logreturns")

paramCombinations = expand.grid(dataList,error_distribution)

paramCombinations
#                     Var1 Var2
#1  bchain_2012_logreturns norm
#2  bchain_2013_logreturns norm
#3  bchain_2014_logreturns norm
#4  bchain_2015_logreturns norm
#5  bchain_2016_logreturns norm
#6  bchain_2012_logreturns  std

AIC计算:

fn_AIC_Calc = function(yearlyReturns = "bchain_2012_logreturns" ,errorDistrib = "norm" ) {

cat(paste0("Begin AIC calc with data =",deparse(substitute(yearlyReturns)),":","errorDistrib=",deparse(substitute(errorDistrib)),"\n"))

yearlyReturns = eval(parse(text="yearlyReturns"))


AIC_DF = do.call(rbind,lapply(model_names,function( x ) {

   if ( x=="eGARCH" ){

   model_year_0 = ugarchspec(variance.model = list(model= x,garchOrder = c(1, 1)),mean.model = list(armaOrder = c(0, 0)),distribution.model = errorDistrib)
   modelfit_year_0 =ugarchfit(spec=model_year_0   , data = yearlyReturns, solver="hybrid")

   DF = data.frame(model_name = x,error_distribution = errorDistrib ,order= "1,1" ,AIC = infocriteria(modelfit_year_0)[1])

   }else {

   model_year_0 = ugarchspec(variance.model = list(model="fGARCH", submodel = x, garchOrder = c(1, 0)),mean.model = list(armaOrder = c(0, 0)),,distribution.model = errorDistrib)
   model_year_1 = ugarchspec(variance.model = list(model="fGARCH", submodel = x, garchOrder = c(1, 1)),mean.model = list(armaOrder = c(0, 0)),,distribution.model = errorDistrib)

   modelfit_year_0 =ugarchfit(spec=model_year_0   , data = yearlyReturns, solver="hybrid")
   modelfit_year_1 =ugarchfit(spec=model_year_1, data = yearlyReturns, solver="hybrid")

   DF_0 = data.frame(model_name = x,error_distribution = errorDistrib, order= "1,0" , AIC = infocriteria(modelfit_year_0)[1])
   DF_1 = data.frame(model_name = x,error_distribution = errorDistrib, order= "1,1" , AIC = infocriteria(modelfit_year_1)[1])

   DF = rbind(DF_0,DF_1)

   }

   return(DF)

}))


cat(paste0("End AIC calc with data =",deparse(substitute(yearlyReturns)),":","errorDistrib=",deparse(substitute(errorDistrib)),"\n"))

}

通话功能:

AIC_Aggregate = do.call(rbind,lapply(1:nrow(paramCombinations),function(x) {

#For each combination of yearly returns dataset and error distribution compute AIC for all models

yearErrorDistribDF = fn_AIC_Calc(yearlyReturns = as.character(paramCombinations[x,"Var1"]) ,errorDistrib = as.character(paramCombinations[x,"Var2"]))

return(yearErrorDistribDF)

}))