关于r:使用带有外生变量的VECM进行预测

Predict with a VECM with exogen variables

在估计了带有固定外生变量的VECM模型之后,我想使用预测函数和newdata参数来计算预测。我正在使用Dynts库,该库提供了使用外生变量计算VECM模型的可能性,但是我看不到如何将带有新数据的预测函数用于集成变量和外生变量。
以下代码不起作用。任何想法 ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
library(tsDyn)
Fact1<-rnorm(100,0,10)
x<-rnorm(100,0,10)
y<-rnorm(100,0,15)
i<-1:100
Yniv2<-sapply(i,function(k) sum(x[1:k]))
Facti1<-Yniv2+y
Yniv2<-Yniv2[1:99]
plot(Yniv2,type="l")#variable macro que l'on cherche ? pr??voir ? l'instant t
lines(Facti1,col="red")#variable macro coint??gr??e avec Y dont on dispose l'obs en t
lines(Fact1,col="green")#variable stationnaire qui explique ??galement Y

exog_met1v1<-Fact1[2:99]
exog_i1<-cbind(Yniv2[1:98],Facti1[1:98])
mdl<-VECM(exog_i1, 1, r=1, include ="const", estim ="ML", LRinclude ="const", exogen = exog_met1v1)
newexogi1 <-cbind(Yniv2[1:99],Facti1[1:99])
new <- Fact1[2:100]
newdata<-cbind(newexogi1,new)
Prev_H_1<-data.frame(predict(mdl, newdata))[,1] #pbbb

如果我要全局拟合,则会出现第一个错误

1
Please provide newdata with nrow=lag+1 (note lag=p in VECM representation corresponds to p+1 in VAR rep)

如果我仅提供最后的观察结果,则会出现第二个错误

1
2
3
4
5
6
newexogi1 <-cbind(Yniv2[98:99],Facti1[98:99])
new <- Fact1[99:100]
newdata<-cbind(newexogi1,new)
Prev_H_1<-data.frame(predict(mdl, newdata))[,1] #pbbb

Erreur dans TVAR.gen(B = B, nthresh = 0, type ="simul", n = n, lag =    lag,  :

矩阵B的指定不正确:预期为5个元素((lagK n inc)(nthresh 1))但具有6


我进行了一些修改(视将来的更改而定!),但是您可以进行以下操作:

1
2
3
4
5
6
## install development version:
library(devtools)
install_github("MatthieuStigler/tsDyn", ref="Dev94", subdir="tsDyn")  

## use these arguments:
predict(mdl, newdata=newexogi1, exoPred=new, n.ahead=2)