TVARsim=function(nobs,  A=NULL, B=NULL, sigma1=NULL,sigma2=NULL,r,skip = 200)
{
nT = nobs + skip
k=nrow(sigma1)
# Generate noise 
e1=mvtnorm::rmvnorm(nT, rep(0, k), sigma1)
e2=mvtnorm::rmvnorm(nT, rep(0, k), sigma2)
et1=t(e1)
et2=t(e2)
# Create space for x
  xt = matrix(0, nT, 2)
xt=t(xt)
for( i in 2: nT)
{ 
  if(xt[1,i-1]<r|all(xt[2,])
      { xt[,i]=A%*%xt[,i-1]+et1[,i-1]
       }
     else
      {xt[,i]=B%*%xt[,i-1]+et2[,i-1]}
xt=t(xt)
   xt = xt[(1 + skip):nT, ]
return(xt)
}
}
ph1=matrix(c(0.7,0.3,0,0.7),2)
ph2=matrix(c(-0.7,-0.3,0,-0.7),2)
sig1=matrix(c(1,0.2,0.2,1),2)
sig2=matrix(c(1,-0.3,-0.3,1),2)
data=TVARsim(300,A=ph1,B=ph2,sigma1=sig1,sigma2=sig2,r=0)
输出结果居然是
> data=TVARsim(300,A=ph1,B=ph2,sigma1=sig1,sigma2=sig2,r=-1)
> data
       [,1] [,2]
  [1,]    0    0
  [2,]    0    0
  [3,]    0    0
  [4,]    0    0
  [5,]    0    0
  [6,]    0    0
  [7,]    0    0
  [8,]    0    0
  [9,]    0    0
 [10,]    0    0
 [11,]    0    0
 [12,]    0    0
 [13,]    0    0
 [14,]    0    0
。。。。。。。