【问题标题】:How to solve the as.data.frame issue in R (forecasting)如何解决 R 中的 as.data.frame 问题(预测)
【发布时间】:2017-06-04 12:31:28
【问题描述】:

我是这个论坛的新手,我要感谢大家在这里提供的有用和有价值的解决方案...... 所以我正在尝试使用 GARCH(1,1) 模型计算提前 h 天的方差预测。

我尝试将as.data.frame() 函数用作:

MSFT.fcst.df = as.data.frame(MSFT.garch11.fcst)

但我收到以下错误消息:

Error in as.data.frame.default(MSFT.garch11.fcst) : 
cannot coerce class "structure("uGARCHforecast", package = "rugarch")" to a data.frame*

MSFT.garch11.fcst:

*------------------------------------*
*       GARCH Model Forecast         *
*------------------------------------*

Model: sGARCH

Horizon: 100

Roll Steps: 0

Out of Sample: 0

0-roll forecast [T0=2012-04-02]:

         Series   Sigma
T+1   [0.0001436] 0.01143

T+2   [0.0001436] 0.01163

T+3   [0.0001436] 0.01182
...   ... ... 

T+100 [0.0001436] 0.01941

知道如何避免这个问题吗?非常感谢提前

【问题讨论】:

  • 如何获得您的MSFT.garch11.fcst ?如果你这样做attributes(MSFT.garch11.fcst),你会得到什么?
  • 试试MSFT.fcst.df = as.data.frame(MSFT.garch11.fcst$show)。有关这些对象的结构,请参阅 rugarch 的文档。
  • MSFT.garch11.fcst = ugarchforecast(MSFT.garch11.fit, n.ahead=100) 其中:MSFT.garch11.fit = ugarchfit(spec=garch11.spec, data=MSFT.ret, solver.control=list(trace = 1))
  • @Andrew MSFT.fcst.df = as.data.frame(MSFT.garch11.fcst$show) MSFT.garch11.fcst$show 中的错误:未为此 S4 类定义 $ 运算符跨度>

标签: r dataframe forecasting


【解决方案1】:
> attributes(forc1)[1] %>% data.frame %>% tbl_df
# A tibble: 500 x 6
       forecast.n.ahead forecast.N forecast.n.start forecast.n.roll forecast.1991.02.20 forecast.1991.02.20.1
 *            <dbl>      <dbl>            <dbl>           <dbl>               <dbl>                 <dbl>
 1              500       1000                0               0          0.01153849          6.403991e-05
 2              500       1000                0               0          0.01085877          1.317389e-04
 3              500       1000                0               0          0.01082964          1.925801e-04
 4              500       1000                0               0          0.01080124          2.472580e-04
 5              500       1000                0               0          0.01077354          2.963971e-04
 6              500       1000                0               0          0.01074652          3.405584e-04
 7              500       1000                0               0          0.01072018          3.802461e-04
 8              500       1000                0               0          0.01069448          4.159135e-04
 9              500       1000                0               0          0.01066942          4.479678e-04
10              500       1000                0               0          0.01064497          4.767750e-04
# ... with 490 more rows
> attributes(forc1)[[1]] %>% data.frame %>% tbl_df
# A tibble: 500 x 6
   n.ahead     N n.start n.roll X1991.02.20 X1991.02.20.1
 *   <dbl> <dbl>   <dbl>  <dbl>       <dbl>         <dbl>
 1     500  1000       0      0  0.01153849  6.403991e-05
 2     500  1000       0      0  0.01085877  1.317389e-04
 3     500  1000       0      0  0.01082964  1.925801e-04
 4     500  1000       0      0  0.01080124  2.472580e-04
 5     500  1000       0      0  0.01077354  2.963971e-04
 6     500  1000       0      0  0.01074652  3.405584e-04
 7     500  1000       0      0  0.01072018  3.802461e-04
 8     500  1000       0      0  0.01069448  4.159135e-04
 9     500  1000       0      0  0.01066942  4.479678e-04
10     500  1000       0      0  0.01064497  4.767750e-04
# ... with 490 more rows

我尝试使用默认示例并通过attributes() 检索它:http://www.unstarched.net/r-examples/rugarch/a-short-introduction-to-the-rugarch-package/

fit = ugarchfit(spec, sp500ret[1:1000, , drop = FALSE], solver = 'hybrid')

forc1 = ugarchforecast(fit, n.ahead = 500)
forc2 = ugarchforecast(spec, n.ahead = 500, data = sp500ret[1:1000, , drop = FALSE])
forc3 = ugarchforecast(spec, n.ahead = 1, n.roll = 499, data = sp500ret[1:1500, , drop = FALSE], out.sample = 500)
f1 = as.data.frame(attributes(forc1)[[1]])
f2 = as.data.frame(attributes(forc2)[[1]])
f3 = t(as.data.frame(attributes(forc3)[[1]], which = 'sigma', rollframe = 'all', aligned = FALSE))
U = uncvariance(fit)^0.5

这是一个示例,您也可以使用 methods 包中的 setAs 进行转换。 (来源:使用 R.pdf 进行财务风险建模和投资组合优化第 19 页:R 中的简要课程)

methods::setAs(from = 'PortWgt', to = 'data.frame', function(from){
+     anames <- names(from@forecast)
+     if(is.null(anames)){
+         N <- length(from)
+         anames <- paste('Asset', 1:N)
+     }
+     ans <- data.frame(from@Date, t(weights(from)))
+     colnames(ans) <- c('Date', anames)
+     ans
+ })
> as(forc1, 'data.frame')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-24
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多