我无法使用 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(???))
我正在尝试 运行 在我的 VECM 上使用固定数据和虚拟变量作为外生变量的月度注册数据。我想让它预测未来 2 年。所以我使用最近 24 次观察
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
为什么测试数据(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,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 package (v0.3.0)
于 2020-09-28 创建
我正在尝试 运行 在我的 VECM 上使用固定数据和虚拟变量作为外生变量的月度注册数据。我想让它预测未来 2 年。所以我使用最近 24 次观察
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
为什么测试数据(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,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 package (v0.3.0)
于 2020-09-28 创建