简体   繁体   中英

Fitting Markov Switching Models to data in R

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.

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM