MatthieuStigler / tsDyn

tsDyn
tsdyn.googlecode.com
34 stars 20 forks source link

VECM.sim starting values #34

Open gergelybat opened 2 years ago

gergelybat commented 2 years ago

Hello,

I am fitting a VECM(lag=1) model with 2 variables, and than I run simulations using parameters of that model. My starting values for the simulation should be the last values of the original series.

According to the error messages of TVECM.sim starting values "Should have nrow = lag and ncol = number of variables". So I provide a 1 x 2 matrix as a starting value (I take the last row of the original timeseries).

In the results of the simulation I can see:

I've checked the code of TVECM.sim, and it seems, that indeed simulations begins from i=lag+2. I believe the problem is, that starting values should have rather lag+1 x k dimensions instead of lag x k dimensions.

I was able to make my simulation realistic by changing 2 rows in TVECM.sim function (but please don't assume it's a sure fix, I'm just rather following what dim VECMsim_unrealistic_simulation.R.txt ensions of matrices suggest).

The changes are in the initialization of simulation (changing p -> p+1):

if (!is.null(starting)) {
            if (all(dim(as.matrix(starting)) == c(nB, p+1))) 
                y[seq_len(p+1), ] <- starting
            else stop("Bad specification of starting values. Should have nrow = lag+1 and ncol = number of variables")
}

I have no issues simulating VAR models (VAR.sim). Converting VECM models to VAR (VARrep function) and then using VAR.sim also works.

Attached is an example of fitting VECM than simulating. EDIT: x4 should be x2 in the attached code (I'm not able to modify attachment).

Than you, Greg

VECMsim_unrealistic_simulation.R.txt

MatthieuStigler commented 2 years ago

Hi, thanks for reporting this. Can you please post instead a reprex, in particular with the TVECM code you have issues with?

Thanks

gergelybat commented 2 years ago

library(tsDyn)

> Warning: package 'tsDyn' was built under R version 3.6.3

> Registered S3 method overwritten by 'xts':

> method from

> as.zoo.xts zoo

> Registered S3 method overwritten by 'quantmod':

> method from

> as.zoo.data.frame zoo

set.seed(1234) N <- 1000 x1 <- cumsum(rnorm(N)) x2 <- 3*x1 + rnorm(N) m_lineVar <- tsDyn::lineVar(cbind(x1,x2), lag=1, include="none", model="VECM") VECM.sim(B=coef(m_lineVar), beta=coefB(m_lineVar), lag=m_lineVar$lag, include=m_lineVar$include, varcov=var(m_lineVar$residuals), starting=(m_lineVar$model[1000,1:2]), n=4)

> [,1] [,2]

> [1,] -26.5972 -80.9821

> [2,] 0.0000 0.0000

> [3,] -0.2882 -0.4300

> [4,] -0.3958 -0.0310

MatthieuStigler commented 2 years ago

ok, thanks! I'll have ot check this, might take some time unfortunately. Thanks for reporting!