将马尔可夫切换模型拟合到R中的数据 [英] Fitting Markov Switching Models to data in 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屋!