Solved – Rolling forecast with DCC-GARCH in R

forecastinggarchrtime series

I have fitted a DCC-GARCH model to my multivariate financial data and do the forecasting. Now, I would like to automate the procedure for a data set that I have.

# load libraries
library(rugarch)
library(rmgarch)
library(FinTS)
library(tseries)
Dat = dji30retw[, 1:8, drop = FALSE]

uspec = ugarchspec(mean.model = list(armaOrder = c(0,0)), variance.model = list(garchOrder = c(1,1), model = "sGARCH"), distribution.model = "norm")
spec1 = dccspec(uspec = multispec( replicate(8, uspec)), dccOrder = c(1,1), distribution = "mvnorm")
fit1 = dccfit(spec1, data = Dat, out.sample = 141, fit.control = list(eval.se=T))
print(fit1)

#Forecast
dcc.focast=dccforecast(fit1, n.ahead = 1, n.roll = 0) 

I have 1141 observations with 8 assets. I want to fit a multivariate DCC-GARCH model to the first 1000 data points and use the remaining 114 data points as the out of sample forecasting period. For example :-

1) Data[1:1000,] In-sample data, forecast for Data[1001,]
2) Data[1:1001,] In-sample data, forecast for Data[1002,]
3) Data[1:1002,] In-sample data, forecast for Data[1003,]
.. 
4) Data[1:1113,] In-sample data, forecast for Data[1141,]

How do I automate the process? I have never done looping before but I have tried to do the following.

Please find the example below:-

for (i in 1:2)
{Dat.Initial = dji30retw[, 1:8, drop = FALSE]

 Dat <- Dat.Initial[1:(1000+(i-1)), ] 

  #Fitting the data

  fit1 <- list()
  spec1 <-list()
  uspec = ugarchspec(mean.model = list(armaOrder = c(0,0)), variance.model = list(garchOrder = c(1,1), model = "sGARCH"), distribution.model = "norm")
  spec1[[i]] = dccspec(uspec = multispec( replicate(8, uspec)), dccOrder = c(1,1), distribution = "mvnorm")
  fit1[[i]] = dccfit(spec1[[i]], data = Dat, out.sample = 114, fit.control = list(eval.se=T))
  print(summary(fit1[[i]])) }

#Out of sample forecasting
dcc.focast <- list()
dcc.focast[[i]]=dccforecast(fit1, n.ahead = 1, n.roll = 0)
print(dcc.focast[[i]]) 
}

However, when I run the code, it comes out like this:

  Length  Class   Mode 
   1 DCCfit     S4 

So where are my results?

Update: I have obtained the result for the above question by executing the following command.

 for (i in 1:2)
 {Dat.Initial = dji30retw[, 1:8, drop = FALSE]
 Dat <- Dat.Initial[1:(1000+(i-1)), ] 

 fit1 <- list()
 uspec = ugarchspec(mean.model = list(armaOrder = c(0,0)), variance.model = list(garchOrder = c(1,1), model = "sGARCH"), distribution.model = "norm")
 spec1 = dccspec(uspec = multispec( replicate(8, uspec)), dccOrder = c(1,1), distribution = "mvnorm")
 fit1[[i]] = dccfit(spec1, data = Dat, out.sample = 114, fit.control = list(eval.se=T))
 print(summary(fit1[[i]])) 
 #Out of sample forecasting
dcc.focast <- list()
dcc.focast[[i]]=dccforecast(fit1[[i]], n.ahead = 1, n.roll = 0)
print(dcc.focast[[i]])
} 

Suppose that I don't want to over-write the fitted model and forecasting objects, though. From the forecasting, I will get the mean return and the variance-covariance matrix. If the for loop is working, I should get 114 values of mean returns and 114 set of variance covariance matrix.

covmat.focast= rcov(dcc.focast)  ##-->Only one covariance matrix.
mean.focast = fitted(dcc.focast)  ##Mean forecast matrix

Now, I want to get the mean returns forecast.

for (i in 1:2)
{mean.focast <- list()
mean.focast[[i]] = fitted(dcc.focast[[i]]) 
print(mean.focast[[i]]) 
}

#Error in fitted(dcc.focast[[i]]) : error in evaluating the argument 'object' in selecting a method for function 'fitted': Error in dcc.focast[[i]] : this S4 class is not subsettable

I have not manage to get the variance covariance matrix from the forecast. I have tried to use the following command:-

for (i in 1:2)
{
covmat.focast <- list()
covmat.focast[[i]]= dcc.focast@mforecast[i]
print(covmat.focast[[i]]) }
#Error: trying to get slot "mforecast" from an object of a basic class ("list") with no slots

Anyone can help me pls?

Best Answer

The problem may lie in this line:

dcc.focast[[i]]=dccforecast(fit1, n.ahead = 1, n.roll = 0)

Here you supply fit1 which is a list to the dccforecast function that requires supplying an object of class DCCfit instead.

What you could do to remedy that is run a loop over i where in each iteration you would execute the following

dcc.focast[[i]]=dccforecast(fit1[[i]], n.ahead = 1, n.roll = 0)

Alternatively you may consider using the dccroll function which does the rolling for you.

Also note that if dcc.focast is a list, you will not be able to execute the following:

covmat.focast= rcov(dcc.focast)  ##-->Only one covariance matrix.
mean.focast = fitted(dcc.focast)  ##Mean forecast matrix

You should do it element-by-element in a loop over i:

covmat.focast[[i]]= rcov(dcc.focast[[i]])
mean.focast[[i]] = fitted(dcc.focast[[i]])