Search code examples
rvarpredict

I can't make a VECM prediction with tsDyn package (number of test data rows different of number of lags in VAR/VECM(???))


I'm trying to run a monthly registered data on my VECM with stationary data and dummy variable as exogenous variables. I want it to predict 2 years ahead. So I use the last 24 observations

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)

I get this error message when I run the last code line: Error in predict.VAR(mdl, newdata = new_endogen, exoPred = new_exogen, : Please provide newdata with nrow=lag

Why should the test data (newdata) have the same length as the lag of the VECM???

I tried to change the lag to 24 (number of rows in newdata) or 48 (total length of newdata) just to see if it would change the result. But it kept the same

I also tried to change the length of newdata to 1 (length of the vecm's lag) and 2 (length of the var model's lag) but kept getting the same result

What could be wrong?


Solution

  • You were right to try with 2 rows (which is the number of lags in the model in levels, this is admittedly slightly tricky). Not sure why it didn't work?

    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
    

    Created on 2020-09-28 by the reprex package (v0.3.0)