1
$\begingroup$

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?

$\endgroup$
2
$\begingroup$

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]])
| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks. I can now get the result for the dcc.focast[[i]]. However, I still have problem with covmat.focast[[i]]= rcov(dcc.focast[[i]]). There is error when I execute the comand as you suggested. It says "Error in UseMethod("rcov") : no applicable method for 'rcov' applied to an object of class "NULL". Any idea how to solve it? $\endgroup$ – nsaa Feb 1 '16 at 11:23
  • $\begingroup$ To execute the command rcov, the class for dccforecast, should be in [1] "standardGeneric"attr(,"package") [1] "methods". Is there any other ways to get the covariance matrix from the forecast command? Or any idea how to solve this? $\endgroup$ – nsaa Feb 1 '16 at 11:26
  • $\begingroup$ I have also tried to use the command for (i in 1:2) { covmat.focast[[i]]= dcc.focast@mforecast[[i]] print(covmat.focast[[i]]) } But there is error saying that "Error: trying to get slot "mforecast" from an object of a basic class ("list") with no slots" $\endgroup$ – nsaa Feb 1 '16 at 11:31
  • $\begingroup$ There is a method rcov for the class DCCforecast, so in principle it should work. Make sure that the object you are supplying to rcov really is a forecast from a DCC model of a class DCCforecast; is it? The error message you cited above seems to indicate otherwise. Another way to extract elements from a DCCforecast object is use slotNames(your_object) and extract the relevant slots by your_object@slot_name. These may be object of class S3 or S4, I don't remember, so you can possible extract deeper elements by using another @ or $ at the end: (to be continued) $\endgroup$ – Richard Hardy Feb 1 '16 at 11:36
  • $\begingroup$ I have tried to do like this --> for (i in 1:2){ dcc.focast[[i]]=dccforecast(fit1[[i]], n.ahead = 1, n.roll = 0) print(dcc.focast[[i]]) } The first error says that, Error in UseMethod("dccforecast") : no applicable method for 'dccforecast' applied to an object of class "NULL" . $\endgroup$ – nsaa Feb 1 '16 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.