假设我已将数据拟合到多变量DCC模型并进行预测以获得平均回报。以下是我可重现的代码。
# 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以这种形式给出了输出: -
, , 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
我们有什么方法可以将输出保存为一个对象/ xts所以它看起来像这样吗?
AA AXP .......... DD
30-01-2004 0.002903173 0.003462776 ........... 0.002367464
06-02-2004 0.002933206 0.003511235 ........... 0.002382313
另一个问题是,为什么只输入'mean.focast',它会给我以下输出?
[[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
[[1]]的值在哪里?
答案 0 :(得分:1)
我们来看看。你的“麻烦”来自代码的这一部分
#Mean returns
mean.focast <- list()
mean.focast[[i]] = fitted(dcc.focast[[i]] )
print(mean.focast[[i]])
您要求为每次迭代打印输出(这就是您获得输出的原因),但请注意,您在迭代的每个步骤中重新实例化列表mean.focast
。这就是为什么你得到第一个列表元素NULL
的原因,因为你已经创建了一个新列表并且只填充了元素2(对于迭代2)。 mean.focast <- list()
应该移出循环。
至于mean.focast
结果。您可以通过多种方式将列表元素组合到单个数据框中。这是一个使用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
所以你的最终代码变成了
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