我无法使用tsDyn包进行VECM预测测试数据行数与VAR / VECM???中的滞后数不同

问题描述

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

library(tsDyn)
exogen1<-rnorm(120,10)
exogen2<-rnorm(120,10)
dc <- rep(0,120)
dc[60:80] <- 1 #dummy variable representation
x<-rnorm(120,10)
y<-rnorm(120,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,dc),24)
predict(mdl,newdata=new_endogen,exoPred = new_exogen,n.ahead=24)

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

为什么测试数据(newdata)与VECM的lag具有相同的长度?

我试图将lag更改为24(newdata中的行数)或48(newdata的总长度),只是看它是否会改变结果。但它保持不变

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

有什么问题吗?

解决方法

您正确地尝试了2行(这是模型中级​​别的延迟数,这确实有些棘手)。不确定为什么不起作用?

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,10)
exogen2<-rnorm(120,10)
dc <- rep(0,120)
dc[60:80] <- 1 #dummy variable representation
x<-rnorm(120,10)
y<-rnorm(120,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,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 package(v0.3.0)于2020-09-28创建