# How to simulate a bivariate VAR(2) model in R? [closed]

hopefully someone can help!

I want to simulate a bivariate VAR(2) model in R using the package containing var.sim (this package is called tsDYN)

It is quite obvious how this is done with just one variable and a single matrix but when it comes to two I can't seem to work out the code?

var1<-VAR.sim(B=A,n=100,lag= 2,include="none")
ts.plot(var1, type="l", col=c(1,2))


but I want to simulate data for the following model.

Any help would be greatly appreciated

Thank you !

Var is nothing more than a dynamical system with random shocks along the way, so here is a little code to manually do what you are looking for. Many things might be improved here and you will have to supply some starting point which I just guessed.

 A = cbind(c(0.2,0),c(-0.3,0.4))
B = cbind(c(-0.1,0.1),c(0.2,-0.3))

varstep <- function(A,B,x,y) {
e = rnorm(2,0,1)
A%*%x +B%*%y + e
}

x1 = c(1,1)
y2 = c(.5,5)

results = cbind(y2,x1)
for (t in seq(1,100))
{
temp <- x1
x1 <- varstep(A,B,x1,y2)
results <- cbind(results, x1)
y2 <- temp
}

xt = results[1,1:100]
yt = results[2,1:100]
plot(1:100,xt,type = "line")
lines(1:100,yt,col="red")


Here is a sample trajectory: