# Error when trying to estimate a Markov-switching Var model in R

I'm trying to estimate a Markov-switching VAR in R using the command msvar. These are the first 10 entries of my two time series. I have 798. When I try to run this I get an Error message

a <- c(1.998513, 1.995302, 2.030693, 2.122130, 2.236770, 2.314639, 2.365214, 2.455784, 2.530696, 2.596537)
b <- c(0.6421369, 0.6341437, 0.6494933, 0.6760939, 0.7113511, 0.7173038, 0.7250545, 0.7812490, 0.7874657, 0.8275209)
x <- matrix (NA,10,2)
x[,1] <- a
x[,2] <- b
time.seriesx <- ts(x)
markov.switchingx <- msvar(time.seriesx, p = 2, h = 2, niterblkopt = 10)


The error message I get is the following:

Error in optim(par = c(beta0.it), fn = llf.msar, Y = Yregmat, X = Xregmat, : initial value in 'vmmin' is not finite

Anyone who could help me? Thanks

I think your time series is too short. Yours has lenght 10 and you estimate with parameter niterblkopt = 10.

E.g. if you have a time series twice as long then it works:

library(MSBVAR)

a <- c(1.998513, 1.995302, 2.030693, 2.122130, 2.236770, 2.314639, 2.365214, 2.455784, 2.530696, 2.596537)
b <- c(0.6421369, 0.6341437, 0.6494933, 0.6760939, 0.7113511, 0.7173038, 0.7250545, 0.7812490, 0.7874657, 0.8275209)
x <- matrix (NA,10,2)
x[,1] <- a
x[,2] <- b
x = rbind(x,x)
time.seriesx <- ts(x)
markov.switchingx <- msvar(time.seriesx, p = 2, h = 2, niterblkopt = 10)


Can you use more data? It seems that there is too little data for the model - or at least for the algorithm.

• I have 798 entries in my data set but it still give me the same error. Any idea why? – Enrico Forabosco Mar 2 '16 at 15:27
• What frequency do you have in your data? in the above example it is 1. What uf you cut the first 20 data and post it. – Ric Mar 2 '16 at 15:30
• The frequency is 1. a <- c(1.998513, 1.995302, 2.030693, 2.122130, 2.236770, 2.314639, 2.365214, 2.455784, 2.530696, 2.596537, 2.647573 2.735317, 2.705269, 2.699783, 2.659748, 2.641353, 2.641825, 2.613648, 2.627755, 2.627383) b <- c(0.6421369, 0.6341437, 0.6494933, 0.6760939, 0.7113511, 0.7173038, 0.7250545, 0.7812490, 0.7874657, 0.8275209, 0.9079720, 0.9455602, 0.9426856, 0.9234943, 0.9072791, 0.9194827, 0.9021116, 0.8971606, 0.9047334, 0.8965786) – Enrico Forabosco Mar 2 '16 at 15:36
• and the vector for b? – Ric Mar 2 '16 at 15:37
• it's there. sorry for my bad editing – Enrico Forabosco Mar 2 '16 at 15:39

For anyone looking for an answer to a similar question as the OP:

MS-VAR works only for stationary time series (as far as I understand). You need to de-trend your time series: either by subracting the mean, subtracting the linear trend, the moving average, a smoothed curve, etc - whatever works best for your current problem or is backed by theory.

The errors the code gives are very cryptic, but the basic idea is that the log-loss function (for ML estimation of the parameters) in your case was uninitialized because the model assumptions didn't hold.

The following R code does you what you want, it seems. I have only recently started working with MS-VAR models, so take this with a grain of salt, however this is at least an actual answer... (Subtracting linear trend gives a much more understandable Regime variable than just subtracting the mean).

library("MSBVAR")
a <- c(1.998513, 1.995302, 2.030693, 2.122130, 2.236770, 2.314639, 2.365214, 2.455784, 2.530696, 2.596537, 2.647573, 2.735317, 2.705269, 2.699783, 2.659748, 2.641353, 2.641825, 2.613648, 2.627755, 2.627383)
b <- c(0.6421369, 0.6341437, 0.6494933, 0.6760939, 0.7113511, 0.7173038, 0.7250545, 0.7812490, 0.7874657, 0.8275209, 0.9079720, 0.9455602, 0.9426856, 0.9234943, 0.9072791, 0.9194827, 0.9021116, 0.8971606, 0.9047334, 0.8965786)

library("pracma")
xa = detrend(a) #a - mean(a) #detrend this
xb = detrend(b) #b - mean(b) #detrend this

x <- matrix (NA,20,2)
x[,1] <- xa
x[,2] <- xb
ts_x <- ts(x)

set.seed(1)
m_x <- msvar(ts_x, p = 2, h = 2, niterblkopt = 10)
fp <- ts(m_x\$fp)

plot(ts_x)
plot(fp)