garchSim 输出不是 10 * 2 格式。我需要日期作为第一列,Garch 值作为第二列

garchSim output not in 10 *2 format . I need the dates as the first column and Garch values as the 2nd

如何将 garchSim 的输出转换为 10*2 格式

> c1 <- garchSpec(model = list(alpha = c(0.2, 0.4), beta = .2, omega =.5))
> c2 <- (garchSim(spec= c1,n=10))
> gfit.acc <- garchFit(~ garch(1,1), data = c2 , trace = FALSE,cond.disw t="snorm")
> c2
GMT
                 garch> c1 <- garchSpec(model = list(alpha = c(0.2, 0.4), beta = .2, omega =.5))
> c2 <- (garchSim(spec= c1,n=10))
> gfit.acc <- garchFit(~ garch(1,1), data = c2 , trace = FALSE,cond.dist="snorm")
> c2
GMT
                 garch
2014-12-26  0.31241878
2014-12-27 -0.02558373
2014-12-28 -0.33445052
2014-12-29  0.68646771
2014-12-30 -0.38295362
2014-12-31  2.24453598
2015-01-01  0.73116526
2015-01-02  0.98165356
2015-01-03  0.09430824
2015-01-04 -0.92170632

> colnames(c2) <- c("date","garch")
Error: length of 'colnames' not equal to array extent

2014-12-26  0.31241878
2014-12-27 -0.02558373
2014-12-28 -0.33445052
2014-12-29  0.68646771
2014-12-30 -0.38295362
2014-12-31  2.24453598
2015-01-01  0.73116526
2015-01-02  0.98165356
2015-01-03  0.09430824
2015-01-04 -0.92170632

> colnames(c2) <- c("date","garch")
Error: length of 'colnames' not equal to array extent

您没有指定包,因此示例尚未完成,但我很确定 c2-object 是 zooxts-class 有一个矩阵 "core"。如果我是正确的,那么您可能需要其中之一来构建数据框结果:

# c2.df <- as.data.frame( c2 ) # if there is an as.data.frame method 

或者;

c2.df <- data.frame( date=as.POSIXct(index(c2)), garch= coredata(c2) )

编辑:有一个 as.data.frame 方法,但 ti 不提供两列结果,因此您需要这个:

> cbind(date =index(x), as.data.frame(x, row.names=1:nrow(x)) )
         date           x
1  2003-01-06  0.94436731
2  2003-01-13 -1.39460234
3  2003-01-19 -0.60883152
4  2003-01-24 -0.97946327
5  2003-02-09 -0.08630589
6  2003-02-15 -0.69076969
7  2003-02-26 -0.78129047
8  2003-03-12 -1.72634717
9  2003-03-28  0.29721521
10 2003-04-02 -0.48914840

我了解到您正在使用 fGarch 包。 library(fGarch) 应该包含在问题中以确保所提供的代码是独立的。

garchFit 从该包中生成一个 timeSeries 对象,可以将其制作成这样的数据框:

> data.frame(date = as.Date(time(c2)), as.matrix(c2), row.names = NULL)
         date       garch
1  2014-12-26 -0.44551300
2  2014-12-27  0.62672096
3  2014-12-28 -0.64959826
4  2014-12-29 -0.87408593
5  2014-12-30 -0.19780425
6  2014-12-31 -0.49756846
7  2015-01-01  1.00466087
8  2015-01-02 -0.87103564
9  2015-01-03 -2.33823488
10 2015-01-04 -0.08257981

注意: 我们同时将日期列转换成了Date class。如果您希望它作为 POSIXct 对象,请省略 as.Date 部分。