Fitting Markov Switching Models to data in R

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

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.

回答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) 


回答2:

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) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!