0

Suppose that I have the following "for" loop in R:

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") 
}

What is the proper command to store the four models' coefficients in a matrix? I already tried to build the command based on an answer to a previous post (see link: Loop for ARMA model estimation) but was not able to obtain the expected result for the above loop.

EDIT:

The following "pseudo-code" is wrong in that it does not store the omega, alpha1 and beta1 coefficients in the third, fourth, and fifth columns of the coefficient matrix.

Can anyone help me find out and correct the error?

Pseudo-code:

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)  
}
Community
  • 1
  • 1
msmna93
  • 41
  • 6
  • You need to set up a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), including all `library()` calls w/ functional code, input data, current results like the output of `modelfit`, and expected results,? – Parfait Nov 26 '16 at 14:54

1 Answers1

0

Eventually, I managed to solve my problem. Below is the code for returning model coefficients in the "armagarchcoefmat" matrix and how the resulting matrix itself looks like. The rows represent the (0,0), (0,1), (1,0) and (1,1) (p,q) couples respectively. The columns contain the phi, theta, mu, omega, alpha1 and beta1 coefficient estimates respectively.

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
msmna93
  • 41
  • 6