我无法使用 tsDyn 包进行 VECM 预测(测试数据行数与 VAR/VECM(???) 中的滞后数不同) [英] I can't make a VECM prediction with tsDyn package (number of test data rows different of number of lags in VAR/VECM(???))

查看:17
本文介绍了我无法使用 tsDyn 包进行 VECM 预测(测试数据行数与 VAR/VECM(???) 中的滞后数不同)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用固定数据和虚拟变量作为外生变量在我的 VECM 上运行每月注册数据.我希望它提前 2 年预测.所以我使用了最后的 24 个观察

I'm trying to run a monthly registered data on my VECM with stationary data and dummy variable as exogenous variables. I want it to predict 2 years ahead. So I use the last 24 observations

library(tsDyn)
exogen1<-rnorm(120,0,10)
exogen2<-rnorm(120,0,10)
dc <- rep(0, 120)
dc[60:80] <- 1 #dummy variable representation
x<-rnorm(120,0,10)
y<-rnorm(120,0,15)
i<-1:120
x1<-sapply(i,function(k) sum(x[1:k]))
x2<-x1+y

plot(x1,type="l")#non-stationary macro variable x1 to predict on the model
lines(x2,col="red")#non-stationary macro variable x2 cointegrated with x1
lines(exogen1,col="green")#stationary variable exogen1 that explains the other variables 
lines(exogen2,col="blue")#stationary variable exogen2 that explains the other variables 

endogen<-cbind(x1,x2)
exogen<- cbind(exogen1, exogen2, dc) 

mdl<- VECM(endogen, lag=1, estim = "ML", r=1, exogen = exogen)
new_endogen <-tail(cbind(x1,x2),24)
new_exogen <- tail(cbind(exogen1,exogen2,dc),24)
predict(mdl, newdata=new_endogen, exoPred = new_exogen, n.ahead=24)

我在运行最后一行代码时收到此错误消息:Error in predict.VAR(mdl, newdata = new_endogen, exoPred = new_exogen, : Please provide newdata with nrow=lag

I get this error message when I run the last code line: Error in predict.VAR(mdl, newdata = new_endogen, exoPred = new_exogen, : Please provide newdata with nrow=lag

为什么测试数据(newdata)的长度应该和VECM的lag一样???

Why should the test data (newdata) have the same length as the lag of the VECM???

我尝试将 lag 更改为 24(newdata 中的行数)或 48(newdata 的总长度)只是为了查看如果它会改变结果.但它保持不变

I tried to change the lag to 24 (number of rows in newdata) or 48 (total length of newdata) just to see if it would change the result. But it kept the same

我还尝试将 newdata 的长度更改为 1(vecm 的 lag 的长度)和 2(var 模型的 lag 的长度>) 但一直得到相同的结果

I also tried to change the length of newdata to 1 (length of the vecm's lag) and 2 (length of the var model's lag) but kept getting the same result

可能有什么问题?

推荐答案

您尝试使用 2 行是正确的(这是模型在级别中的滞后数,这确实有点棘手).不知道为什么它不起作用?

You were right to try with 2 rows (which is the number of lags in the model in levels, this is admittedly slightly tricky). Not sure why it didn't work?

library(tsDyn)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
packageVersion("tsDyn")
#> [1] '0.9.48.999'
exogen1<-rnorm(120,0,10)
exogen2<-rnorm(120,0,10)
dc <- rep(0, 120)
dc[60:80] <- 1 #dummy variable representation
x<-rnorm(120,0,10)
y<-rnorm(120,0,15)
i<-1:120
x1<-sapply(i,function(k) sum(x[1:k]))
x2<-x1+y


endogen<-cbind(x1,x2)
exogen<- cbind(exogen1, exogen2, dc) 

mdl<- VECM(endogen, lag=1, estim = "ML", r=1, exogen = exogen)
#> Warning: tail(., addrownums = V) is deprecated.
#> Use tail(., keepnums = V) instead.
new_endogen <-tail(cbind(x1,x2),24)
new_exogen <- tail(cbind(exogen1,exogen2,dc),24)
predict(mdl, newdata=new_endogen[1:2,,drop=FALSE], exoPred = new_exogen, n.ahead=24)
#>            x1        x2
#> 121 -121.6248 -120.4420
#> 122 -124.3986 -121.1053

reprex 包 (v0.3.0) 于 2020 年 9 月 28 日创建

Created on 2020-09-28 by the reprex package (v0.3.0)

这篇关于我无法使用 tsDyn 包进行 VECM 预测(测试数据行数与 VAR/VECM(???) 中的滞后数不同)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆