【问题标题】:I can't make a VECM prediction with tsDyn package (number of test data rows different of number of lags in VAR/VECM(???))我无法使用 tsDyn 包进行 VECM 预测(测试数据行数与 VAR/VECM 中的滞后数不同(???))
【发布时间】:2020-09-28 04:41:42
【问题描述】:

我正在尝试在我的 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 的长度),但始终得到相同的结果

可能出了什么问题?

【问题讨论】:

    标签: r var predict


    【解决方案1】:

    您尝试使用 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 年 9 月 28 日创建

    【讨论】:

    • 我不知道为什么第一次尝试我做不到。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2018-03-04
    • 2014-03-21
    • 1970-01-01
    • 2019-03-15
    • 2021-01-25
    • 2021-02-28
    相关资源
    最近更新 更多