I am new user in R. Please help me!
I have 1114 observations with 8 assets. For example, I have fitted a multivariate DCC-GARCH model to the first 1000 data points and I want to do 1-ahead forecast for 3 periods later such as
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,]
Below is my reproducible code:-
# load libraries
library(rugarch)
library(rmgarch)
library(FinTS)
library(tseries)
library (fPortfolio)
data(dji30retw)
for (i in 1:3) {
Dat.Initial = dji30retw[, 1:8, drop = FALSE]
Dat <- Dat.Initial[1:(1000+(i-1)), ]
#Fitting the data
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 <- list()
fit1[[i]] = dccfit(spec1, data = Dat, out.sample = 1, fit.control = list(eval.se=T))
#Out of sample forecasting
dcc.focast <- list()
dcc.focast[[i]]=dccforecast(fit1[[i]], n.ahead = 1, n.roll = 0)
print(dcc.focast[[i]])
}
The codes works perfectly. I can now get my dcc.focast values. But why is it, if I execute
dcc.focast[[1]]
NULL
it gives me "NULL". Shouldn't it produce the same answer as the "print(dcc.focast[[i]])" as in the loop?
The problem here, it only gives me dcc.focast[[3]]. The rest are are "NULL". What is the mistake that I have done? Anyone can help to explain?
Modify your code to define your lists outside the for
. You are deleting the previous computations when redefining your lists inside the loop.
data(dji30retw)
fit1 <- list()
dcc.focast <- list()
for (i in 1:3) {#i=1
Dat.Initial = dji30retw[, 1:8, drop = FALSE]
Dat <- Dat.Initial[1:(1000+(i-1)), ]
#Fitting the data
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 = 1, fit.control = list(eval.se=T))
#Out of sample forecasting
dcc.focast[[i]]=dccforecast(fit1[[i]], n.ahead = 1, n.roll = 0)
print(dcc.focast[[i]])
}
summary(dcc.focast)
dcc.focast[[1]]
> summary(dcc.focast)
Length Class Mode
[1,] 1 DCCforecast S4
[2,] 1 DCCforecast S4
[3,] 1 DCCforecast S4
> dcc.focast[[1]]
*---------------------------------*
* DCC GARCH Forecast *
*---------------------------------*
Distribution : mvnorm
Model : DCC(1,1)
Horizon : 1
Roll Steps : 0
-----------------------------------
0-roll forecast:
, , 1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1.0000 0.3378 0.3244 0.2383 0.2730 0.5086 0.3470 0.5073
[2,] 0.3378 1.0000 0.3498 0.5423 0.5910 0.3321 0.2802 0.3658
[3,] 0.3244 0.3498 1.0000 0.2372 0.2771 0.3479 0.2433 0.3625
[4,] 0.2383 0.5423 0.2372 1.0000 0.5746 0.2877 0.2303 0.3593
[5,] 0.2730 0.5910 0.2771 0.5746 1.0000 0.3063 0.2169 0.3101
[6,] 0.5086 0.3321 0.3479 0.2877 0.3063 1.0000 0.3637 0.4307
[7,] 0.3470 0.2802 0.2433 0.2303 0.2169 0.3637 1.0000 0.3976
[8,] 0.5073 0.3658 0.3625 0.3593 0.3101 0.4307 0.3976 1.0000