在 R 中进行 DCC-GARCH 估计后,如何计算每年的平均相关系数?

How to compute average correlation coefficient per year after DCC-GARCH estimation in R?

目前,我已将 DCC-GARCH 模型拟合到我的财务 returns 数据中,并通过以下可重现代码估算相关系数:

library(rugarch)
library(rmgarch)
data(dji30retw)
Dat = dji30retw[, 1:8, drop = FALSE]
uspec = ugarchspec(mean.model = list(armaOrder = c(0,0)), variance.model = list(garchOrder = c(1,1), model = "eGARCH"), distribution.model = "norm")
spec1 = dccspec(uspec = multispec(replicate(8, uspec)), dccOrder = c(1,1),  distribution = "mvnorm")
fit1 = dccfit(spec1, data = Dat)
print(fit1)
r1=rcor(fit1, type="cor")

现在,我需要创建一个数据框,其中包含 1 列,其中包括每周相关系数的平均值。我怎样才能做到这一点?

看来你想要的是

Dat$meancor <- apply(r1, 3, function(r) mean(r[upper.tri(r)]))

给予

head(Dat, 2)
#                      AA          AXP           BA         BAC           C        CAT         CVX          DD   meancor
# 1987-03-27 -0.005952399 -0.007942854 -0.008866429 -0.03119837 -0.02586351 0.01587335 -0.02204814 0.006389798 0.3786852
# 1987-04-03  0.005952399 -0.025849581 -0.010230268  0.00000000 -0.01762160 0.04113691  0.06324389 0.035457312 0.375325

为了实现这一点,apply 遍历 r1 的第三维(遍历每个相关矩阵)`,取上三角元素并对它们进行平均。