构建循环GARCH / ARMA + GARCH模型的系数矩阵

时间:2016-11-25 21:14:34

标签: r loops

假设我在R中有以下“for”循环:

USDlogreturns=diff(log(prices))
for(p in 0:1) for(q in 0:1){
model<-ugarchspec(variance.model = list(model="fGARCH", submodel = "GARCH", garchOrder = c(1, 1)),mean.model = list(armaOrder = c(p, q), include.mean = TRUE), distribution.model = "norm")
modelfit<-ugarchfit(spec=model, data=USDlogreturns, solver="hybrid") 
}

将四个模型的系数存储在矩阵中的正确命令是什么?我已经尝试根据前一篇文章的答案构建命令(请参阅链接:Loop for ARMA model estimation),但无法获得上述循环的预期结果。

编辑:

以下“伪代码”是错误的,因为它不会在系数矩阵的第三,第四和第五列中存储omega,alpha1和beta1系数。

任何人都可以帮我找出并纠正错误吗?

的伪代码:

armagarchcoefmat=matrix(NA, 4, 6)
a=0
for(p in 0:1) for(q in 0:1){
a=a+1
model<-ugarchspec(variance.model = list(model="fGARCH", submodel = "GARCH", garchOrder = c(1, 1)),mean.model = list(armaOrder = c(p, q), include.mean = TRUE), distribution.model = "norm")
modelfit<-ugarchfit(spec=model, data=USDlogreturns, solver="hybrid") 
if(p>0) armagarchcoefmat[a, c(1:   p) ]=coef(modelfit)[2:6][c(   1 :   p )] 
if(q>0) armagarchcoefmat[a, c(2:(1+q))]=coef(modelfit)[2:6][c((p+1):(p+q))]  
armagarchcoefmat[a,      6  ]=head(coef(modelfit),1)  
}

1 个答案:

答案 0 :(得分:0)

最终,我设法解决了我的问题。下面是在“armagarchcoefmat”矩阵中返回模型系数的代码,以及生成的矩阵本身的样子。行分别代表(0,0),(0,1),(1,0)和(1,1)(p,q)对。这些列分别包含phi,theta,mu,omega,alpha1和beta1系数估计值。

library(rugarch)

  USDlogreturns=diff(log(prices))


    armagarchcoefmat=matrix(NA, 4, 6)
    a<-0
    for(p in 0:1) for(q in 0:1){
    a<-a+1
    model<-ugarchspec(variance.model = list(model="fGARCH", submodel = "GARCH", garchOrder = c(1, 1)),mean.model = list(armaOrder = c(p, q), include.mean = TRUE), distribution.model = "norm")
    modelfit<-ugarchfit(spec=model, data=USDlogreturns, solver="hybrid")
    if(p>0) armagarchcoefmat[a, c(1:   p) ]<-coef(modelfit)[2:3][c(   1 :   p )]
    if(q>0) armagarchcoefmat[a, c(2:(1+q))]<-coef(modelfit)[2:3][c((p+1):(p+q))]
    armagarchcoefmat[a,    3  ]<-head(coef(modelfit),1)
    armagarchcoefmat[a,    c(4:6)  ]<-tail(coef(modelfit),3)

    }

   > armagarchcoefmat
           [,1]      [,2]        [,3]         [,4]       [,5]      [,6]
[1,]         NA        NA 0.002168063 9.086569e-06 0.03919058 0.9598094
[2,]         NA 0.1301537 0.002158361 9.643089e-06 0.04207555 0.9569244
[3,]  0.1166279        NA 0.002172631 9.517783e-06 0.04160620 0.9573938
[4,] -0.3500512 0.4745337 0.002110809 9.745079e-06 0.04236628 0.9566337