如何使用 R 中的 depmixS4 包预测样本外观察?

How to predict out-of-sample observations with depmixS4 package in R?

我有一系列单变量数据,我想使用 R 上的 depmixS4 包在其上拟合隐马尔可夫模型。我的最终目标是预测下一个 [= data 系列的 28=]k 个观测值(假设 k = 10)。我对预测新状态并不感兴趣(这很重要,但不是我的最终目标),但我想预测数据系列的下一个值。

是一段代码:

# My series
data = rnorm(10000)
df_1_col = data.frame(data)
colnames(df_1_col) <- c('obs')

# Model
mod <- depmix(obs ~ 1, data = draws, nstates = n_state)
fit.mod <- fit(mod)

此时我不知道如何预测下一个样本外值。我想要类似于 forecast 包中的 forecast 函数的东西。

我尝试使用以下代码:

state_ests <- posterior(fit.mod)
pred_resp <- matrix(0, ncol = n_state, nrow = 10)

for(i in 1:n_state) {
  pred_resp[,i] <- predict(fit.mod@response[[i]][[1]])
}

使用此代码,predict 函数生成的预测值数量等于 data 中的观察值数量,因此这是不正确的。

我怎样才能完成这些非常基本的操作?我是 HMM 的新手,但我已经尝试查看许多资源,但没有找到任何信息。谢谢:)

常规使用的 HMM,例如您调用的库,通常是一对一的。一对一,我的意思是:你已经注意到预测序列长度与输入(观察)长度相同。

图由Andrej Karpathy

对于一对多,您可能想尝试 LSTMs,它适用于一个[输入]-对多[输出]、多个[输入]- to-[many]输出等。这应该允许你做一些短期预测。有许多答案 () 直观地说明了这可能是如何工作的。如果您不想使用深度学习模型,也许您可​​以看看 卡尔曼滤波器

隐马尔可夫模型以隐藏状态为条件对观察到的变量进行建模。因此,预测观察到的变量需要一个预测隐藏状态的中间步骤。一旦你有了隐藏状态的预测概率,你就可以根据观察到的变量的边际分布来预测观察到的变量,例如

P(Y[T+k]|Y[1:T]) = \sum_i P(Y[T+k]|S[T+k] = i) * P(S [T+k] = i|Y[1:T])

你可以通过将P(S[T]|Y[1:T])与状态转移矩阵相乘得到预测的状态分布。

library(depmixS4)

n_state <- 2

# My series
draws <- data.frame(obs=rnorm(10000))

# Model
mod <- depmix(obs ~ 1, data = draws, nstates = n_state, stationary=TRUE)
fit.mod <- fit(mod)

# extract the state-transition matrix
transition_mat <- rbind(getpars(getmodel(fit.mod,"transition",1)),getpars(getmodel(fit.mod,"transition",2)))

# extract the probability of the states at the final time point in the data (t=T)
# this will act as a "prior" to compute the forecasted state distributions
prior_vec <- as.numeric(posterior(fit.mod)[1000,-1])

# state-wise predictions for the observed variables
pred_r_by_state <- c(getpars(getmodel(fit.mod,"response",1))[1],
                     getpars(getmodel(fit.mod,"response",2))[1])

# for T + 1
# the forecasted state distribution is (prior_vec %*% transition_mat)
# so hence the prediction of the observed variable is
sum(pred_r_by_state * (prior_vec %*% transition_mat))

# for T + 2
# the forecasted state distribution is (prior_vec %*% transition_mat %*% transition_mat)
# so hence the prediction of the observed variable is
sum(pred_r_by_state * (prior_vec %*% transition_mat %*% transition_mat))

# for T + 3
sum(pred_r_by_state * (prior_vec %*% transition_mat %*% transition_mat %*% transition_mat))

# etc

您可能想要使用 expm 包,其中包含 %^% 运算符,因此您可以使用

transition_mat %^% 3 

而不是

transition_mat %*% transition_mat %*% transition_mat

如果模型在观察到的预测变量的模型中包含协变量,您还需要考虑这些因素,即在计算 pred_r_by_state.

时尝试以某种方式预测这些值