Search code examples
rtime-seriesforecasting

Save R output as an object


Suppose, I have fitted my data to a multivariate DCC model and do the forecasting to get mean returns. Below is my reproducible code.

# load libraries
library(rugarch)
library(rmgarch)
library(FinTS)
library(tseries)
library(fPortfolio)
data(dji30retw)

for (i in 1:2)
{ 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 = 120, 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]])

#Mean returns 
mean.focast <- list()
mean.focast[[i]] = fitted(dcc.focast[[i]] )
print(mean.focast[[i]])

#Var-Cov 
covmat.focast[[i]]= rcov(dcc.focast[[i]])
print(covmat.focast[[i]])
}

R has given me the output in this form:-

, , 2004-01-30

             AA         AXP         BA         BAC           C         CAT         CVX          DD
T+1 0.002903173 0.003462776 0.00295735 0.003485212 0.004442807 0.002986181 0.002069151 0.002367464

, , 2004-02-06

         AA         AXP         BA         BAC           C       CAT        CVX          DD
T+1 0.002933206 0.003511235 0.00300817 0.003510005 0.004436862 0.0029913 0.00205337 0.002382313

Is there any way we can save the output as one object/xts so it will look something like this?

               AA              AXP         ..........  DD
 30-01-2004    0.002903173   0.003462776   ........... 0.002367464
 06-02-2004    0.002933206   0.003511235   ........... 0.002382313

And another question, why is it if I type only 'mean.focast' , it gives me the following output?

[[1]]
NULL

[[2]]
, , 2004-02-06

             AA         AXP         BA         BAC           C       CAT        CVX          DD
T+1 0.002933206 0.003511235 0.00300817 0.003510005 0.004436862 0.0029913 0.00205337 0.002382313

Where is the value for [[1]]?


Solution

  • Let's see. Your "troubles" are from this part of the code

    #Mean returns 
    mean.focast <- list()
    mean.focast[[i]] = fitted(dcc.focast[[i]] )
    print(mean.focast[[i]])
    

    You ask to print the output for each iteration (which is why you get the output you get), but note that you reinstantiate the list mean.focast in each step of the iteration. That is why you get NULL for the first list element, because you've made a new list and are only filling in element 2 (for iteration 2). The mean.focast <- list() should be moved outside the loop.

    As for the mean.focast result. You can combine the list elements into a single data frame in tons of ways. Here's one using reshape2.

    library(reshape2)
    res <- dcast(melt(mean.focast), Var3 ~ Var2)
    res
            Var3          AA         AXP         BA         BAC           C
    1 2004-01-30 0.002903173 0.003462776 0.00295735 0.003485212 0.004442807
    2 2004-02-06 0.002933206 0.003511235 0.00300817 0.003510005 0.004436862
              CAT         CVX          DD
    1 0.002986181 0.002069151 0.002367464
    2 0.002991300 0.002053370 0.002382313
    

    So your final code becomes

    mean.focast <- list()
    
    for (i in 1:2)
    { 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 = 120, 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]])
    
    #Mean returns 
    mean.focast[[i]] = fitted(dcc.focast[[i]] )
    print(mean.focast[[i]])}
    
    res <- dcast(melt(mean.focast), Var3 ~ Var2)
    res