depmixs4状态估计列表

问题描述

我已经使用软件包depmixs4中的fit(depmix())函数将数据的时间序列拟合到了隐马尔可夫模型。我想获得一个估计状态的时间序列,其中对状态x的估计是分配给状态x的平均值。目前,我只得到一个表示状态索引的时间序列值(例如1,3,5,2,5 ...)。

这是我当前的代码

set.seed(9)

hmm9

fitted_hmm9

摘要(fitted_hmm9)

state_ests9

state_ests9 [,1]

最后一部分state_ests9[,1]是状态索引的时间序列,而状态的实际期望值存储在summary(fitted_hmm9)中的某个位置。

解决方法

通过“估计状态”,我假设您想要根据每个状态进行预测的响应(即“音量”)。要获取响应的状态预测值,可以使用predict方法,但是需要在响应子模型上调用该方法,该子模型隐藏在合适的depmix对象中。>

在合适的depmix对象中,有一个称为“响应”的插槽,它是列表的列表,结构为

fitted_model@response[[state_id]][[response_id]]

在您的情况下,我认为“体积”是单变量的,因此只有一个响应,response_id始终为1。对于具有9个状态的模型,state_id的范围为1到9

以下代码(包括随机生成的“ volume”值以使其可重现)为您提供了所需的内容(我认为):

set.seed(123)
volume <- rnorm(10000)

hmm9 <- depmix(volume ~ 1,data=data.frame(volume),nstates=9)

fitted_hmm9 <- fit(hmm9)

summary(fitted_hmm9)

state_ests9 <- posterior(fitted_hmm9)

state_ests9[,1]

# construct matrix for state-dependent predictions
pred_resp9 <- matrix(0.0,ncol=9,nrow=nrow(state_ests9))
# fill matrix column-wise by using the "predict" method of
# corresponding response model
for(i in 1:9) {
  pred_resp9[,i] <- predict(fitted_hmm9@response[[i]][[1]])
}
## use MAP state estimates to extract a single time-series
## with predictions
pred_resp9[cbind(1:nrow(pred_resp9),state_ests9[,1])]