将马尔可夫切换模型拟合到R中的数据 [英] Fitting Markov Switching Models to data in R

查看:267
本文介绍了将马尔可夫切换模型拟合到R中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R中的包MSwM将两种Markov切换模型拟合为对数返回的时间序列.我正在考虑的模型是仅具有截距和AR的回归模型(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)

在两种情况下,功能msmFit均不起作用.这是我收到的错误消息:

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"’

我不知道为什么会收到此错误消息,因为我将lm对象用作函数msmFit的第一个参数,并且这是该函数的参数的合适类.

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.

推荐答案

在将数据传递到msmFit时,您不需要使用不必要的参数.数据已经包含在mod中.以下代码为我运行:

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)

这篇关于将马尔可夫切换模型拟合到R中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆