## Not run:
# # Example: simulate an MS(h)-VAR(p) model with two equations.
# # Have h = 2, m=2, and p=1, simplest case
#
# # VAR simulation parameters
# bigt <- 500 # number of observations
# m <- 2 # number of endogenous variables
# p <- 1 # lag length
# h <- 2 # number of regimes
#
# # setup transition matrix with two states
#
# Q <- matrix(c(.98, .02,
# .05, .95), nrow=h, byrow=TRUE)
#
# # theta stores paramater values
# # 1st column is intercept
# # 2:m*p are the AR coefficients
# # (mp+2)'th columns are variance
#
# # regime 1
# var.beta0.st1 <- c(1,2) # intercepts
# var.betas.st1 <- matrix(c(.7, .1,
# .1, .7), m, byrow=TRUE)
# # regime 2
# var.beta0.st2 <- c(0,0) # intercepts
# var.betas.st2 <- matrix(c(.2, .1,
# .2, .1), m, byrow=TRUE)
#
# # Build the array
# var.beta0 <- array(NA, c(m,1,h))
# var.betas <- array(NA, c(m,p*m,h))
# var.beta0[,,1] <- var.beta0.st1
# var.beta0[,,2] <- var.beta0.st2
# var.betas[,,1] <- var.betas.st1
# var.betas[,,2] <- var.betas.st2
#
# # Variance-Covariance Matrix for
# # MVN distributed disturbances
# # regime 1
# e.vcv.st1 <- matrix(c(.3, .1,
# .1, .3), 2)
# # regime 2
# e.vcv.st2 <- matrix(c(.1, .05,
# .05, .1), 2)
# # combine
# e.vcv <- array(NA, c(m, m, h))
# e.vcv[,,1] <- e.vcv.st1
# e.vcv[,,2] <- e.vcv.st2
#
# # hold true values of parameters for easy comparison to estimates
# theta.true.var <- array(NA, c(m, 1+m*p+m, h))
# theta.true.var[,1,] <- var.beta0
# theta.true.var[,2:(1+p*m),] <- var.betas
# theta.true.var[,(1+m*p+1):ncol(theta.true.var),] <- e.vcv
#
# simdata <- simulateMSVAR(bigt, m, p, var.beta0, var.betas, e.vcv, Q)
#
# # Plot
# plot(as.ts(simdata[[1]]))
#
# # Fit a simple model
#
# model <- msvar(Y=simdata[[1]], p=1, h=2, niterblkopt=50)
#
# # Plot regime estimates and compare to true simulated values
# par(mfrow=c(2,1))
# plot(ts(model$fp))
# plot(ts(simdata$st))
# ## End(Not run)
Run the code above in your browser using DataLab