Fitting Markov Switching Models to data in R

Egodym picture Egodym · May 17, 2015 · Viewed 8k times · Source

I'm trying to fit two kinds of Markov Switching Models to a time series of log-returns using the package MSwM in R. The models I'm considering are a regression model with only an intercept, and an AR(1) model. Here is the code I'm using:

library(tseries)

#Prices
ftse<-get.hist.quote(instrument="^FTSE", start="1984-01-03", end="2014-01-01", quote="AdjClose", compression="m")

#Log-returns
ftse.ret<-diff(log(ftse))

library(MSwM)

#Model with only intercept
mod<-lm(ftse.ret ~ 1)

#Fit regime-switching model
msmFit(mod, k=2, sw=c(T,T), p=0, data=ftse.ret)

#AR(1) model
mod<-lm(ftse.ret[2:360] ~ ftse.ret[1:359])

#Fit regime-switching model
msmFit(mod, k=2, sw=c(T,T,T), p=1, data=ftse.ret)

In both cases the function msmFit doesn't work. Here is the error message I get:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘msmFit’ for signature ‘"lm", "numeric", "logical", "numeric", "zoo", "missing"’

I don't know why I get this error message, since I'm using as first argument of the function msmFit a lm object and this is a suitable class for the argument of the function.

Answer

Thorsten picture Thorsten · Aug 18, 2015

You have an unnecessary argument as you pass data to msmFit, which is not necessary. The data is already contained in mod. The following code runs for me:

library(tseries)

#Prices
ftse<-get.hist.quote(instrument="^FTSE", start="1984-01-03", end="2014-01-01", quote="AdjClose",     compression="m")

#Log-returns
ftse.ret<-diff(log(ftse))

library(MSwM)

#Model with only intercept
mod<-lm(ftse.ret ~ 1)

#Fit regime-switching model
mod.mswm=msmFit(mod, k=2, sw=c(T,T), p=0)
plot(mod.mswm)