0

After having estimated a VECM model with stationary exogen variables, I would like to compute a prediction with the predict function and the newdata argument. I'm using the Dynts library that offers the possibility to compute VECM models with exogeneous variables, but I don't see how I can use the predict function with newdata for the integrated variables AND the exogeneous ones. The following code doesn't work. Any idea ?

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

First error if I want the global fit

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

Second error if I provide just the last observations

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,  : 

Matrix B badly specified: expected 5 elements ( (lagK+ n inc) (nthresh+1) ) but has 6

M--
  • 25,431
  • 8
  • 61
  • 93
Stéphanie C
  • 809
  • 8
  • 31
  • Please take the time to define all the variables in your code and provide some sample input to make your problem [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), otherwise it's much more difficult to help you.. – MrFlick Feb 23 '15 at 03:36
  • Yes, this is a bug. I will try to solve it soon. – Matifou Feb 24 '15 at 05:03

1 Answers1

1

I made some modifications, (subject to future changes!), but here you go:

## 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)
Matifou
  • 7,968
  • 3
  • 47
  • 52
  • Thank you for your quick answer, but it still doesn't work :(. I tried on the same example I explained above and here is the returned error : `> newexogi1 <-cbind(Yniv2[99],Facti1[99]) > new <- Fact1[100] > predict(mdl, newdata=newexogi1, exoPred=new, n.ahead=1) Erreur dans predict.VECM(mdl, newdata = newexogi1, exoPred = new, n.ahead = 2) : Please provide newdata with nrow=lag` – Stéphanie C Mar 08 '15 at 15:21
  • Plus, I don't really understand why the length of the input should be of the same length of "lag", it prevents the function to return the ajustment of the models on the whole estimation period, which is often what is commented (at least in macroeconomics). Keep me in touch if you procede to further modifications please and thank you for the work – Stéphanie C Mar 08 '15 at 15:24
  • Mmh. The point is in fact that the function converts the VECM to a VAR (lag+1), and then does a prediction in terms of levels, not differences! This explains why you should provide 2 rows (levels) and not 1 (the diff). I am not sure on the other hand to understand your second point regarding "prevent the adjustment on the model on the estimatoin period?". Do you want to generate n-ahead forecasts from multiple periods? – Matifou Mar 08 '15 at 22:20
  • Ok I got it, the endogeneous newdata are longer than the exogeneous newdata. It seems to work, thank you very much ! – Stéphanie C Mar 15 '15 at 17:27
  • Indeed! Exogeneous newdata should be of size n.ahead, while endogeneous data should be of size lags(+1 if vecm)! I'll document this better! – Matifou Mar 15 '15 at 18:04
  • @Matifou I had a similar problem, but I tried to change ``newdata`` to 2 observations (endogenous data that would match with the VAR lag(VECM lag(1) + 1) and ``exoPred`` to 2 observations as well (to match with ``n.ahead``) as you said. But I kept receiving the same problem. Could you see the code in https://stackoverflow.com/questions/64096043/i-cant-make-a-vecm-prediction-with-tsdyn-package-number-of-test-data-rows-diff ? – Apolo Reis Sep 28 '20 at 04:48