1

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.

Egodym
  • 453
  • 1
  • 8
  • 23

2 Answers2

1

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)
Thorsten
  • 331
  • 2
  • 6
  • Great, it works. Any thought about the AR(1) model? By using p=0 the function works, whereas by using p=1 I get the following error message: Error in parse(text = x, keep.source = FALSE) : :1:19: unexpected input 1: ~.+ftse.ret[2:360]_ – Egodym Aug 18 '15 at 14:56
1

When you set p = 1, msmFit model will add an AR(1) coefficient for you. So you can simply pass in the model with only intercept (mod) and just set p = 1. The following code should work.

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
msm_intercept <- msmFit(mod, k=2, sw=c(T,T), p=0)

#Fit regime-switching model with AR(1) model
msm_ar1 <- msmFit(mod, k=2, sw=c(T,T,T), p=1)
pk.119
  • 11
  • 2
  • Does it mean if I want to do Markov Switching with ARMA(1,1), I just need to specify `p=3`? If not, can you please help show me how to do this in R? – user177196 Mar 05 '19 at 06:45