在 R 中使用 predict_rolling 进行 n 步提前预测

问题描述

我目前正在尝试使用 R 包 tsDyn 中的 predict_rolling 进行滚动预测。

具体来说,例如,我想做一个提前 2 步的滚动预测,并希望重新估计每一步的 VAR。我已经有一个可以运行的代码,它可以为我完成,但我想避免 for 循环。

当我尝试使用带有 n.ahead = 1 的 predict_rolling 复制代码时,我得到了正确的结果,但是它们在 n.ahead > 1 时有所不同。我不太确定当我增加地平线。有人可以帮我吗?

非常感谢!

library(tsDyn)
library(vars)

data <- structure(list(beta_1 = c(6.35815059908977,6.26392397241388,5.75584304551968,5.79468441571414,5.79866753735267,5.93702141823254,5.9802644025435,5.86669864820461,6.34140417225121,6.22403010817456,6.04156005554047,5.99436622944328,6.21876461132087,6.12521892832898,6.28785501504558,6.58416859326718,6.60860111153987,6.56413440110398,6.48242438403672,6.37777673701131,6.66598764007499,6.40439103841177,6.68523656665178,6.84779996196864,6.65828185836342,6.59294917836496,6.58247770373732,6.58151962670905,6.54178934287671,6.63472984194905
),beta_2 = c(-0.693981338882855,-0.490176628667609,0.273659053491197,0.239555376236275,0.207269178550827,0.251612610758922,0.407559789903953,0.569886012132651,0.203137745309771,0.409355931195391,0.52231134837746,0.0989628040415202,-1.12460228733712,-1.1741087534917,-1.89827153876211,-2.60891196464883,-3.07816904157986,-3.02987212289999,-3.02564037697751,-3.05186295573602,-4.31703470296141,-4.46324605358408,-5.23741582126552,-5.53763792734524,-5.21652056632509,-5.18677802920265,-5.14017527231271,-5.1638258167503,-5.15084057539836,-5.26464530948111),beta_3 = c(2.07909176259473,1.76313003375624,1.53657531433667,2.14690291607906,2.40466018945562,0.654662517991894,0.0325387409714942,-0.53746641768277,-2.10960016880694,-2.1786830671857,-2.8803651841676,-3.45821461372104,-3.09352055010635,-3.38860120189217,-3.24589594863622,-2.24785473097812,-1.52644445065669,-1.35977678028773,-2.6903096555247,-2.80397737054071,-3.75658639513462,-3.89616413931621,-1.87965311921739,-0.955127483103369,-0.754754537400041,-1.06798338362736,1.25463230677968,-0.289233763457811,-0.585463719717287,-1.73110982341087)),row.names = c(NA,30L),class = "data.frame")
  
# 1- step ahead forecast:

horizon <- 1
  
# Old code I want to get rid of:
fore_factors <- matrix(NA,20,3)
  for(j in 1 : 20)
  {
    # 1. Data preparation
    fit_factors <- data[1 : (9 + j),]
    # 2. State equation
    #   2.1 Estimation
    fitting <- VAR(fit_factors,p = 1,type = "const")
    #   2.2 Factor Forecasting
    x_t1_mu <- as.numeric(fit_factors[nrow(fit_factors),])
    pred <- predict(fitting,n.ahead = horizon,newdata = x_t1_mu)
    fore_factors[j,] <- c(pred$fcst$beta_1[horizon],pred$fcst$beta_2[horizon],pred$fcst$beta_3[horizon])
  }

# New code:
  
  fit_var <- lineVar(data,lag = 1,include = "const",model = "VAR")
  predicted_values <- predict_rolling(fit_var,nroll = 20,refit = 1,n.ahead = horizon) $ pred

# Compare:
predicted_values
  fore_factors

  
  # 2 horizon forecasting:
  
  horizon <- 2
  
  # Old code I want to get rid of:
  fore_factors <- matrix(NA,pred$fcst$beta_3[horizon])
  }
  
  # New code:
  
  fit_var <- lineVar(data,n.ahead = horizon) $ pred

# Compare:
predicted_values
 fore_factors

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...