rhidden-markov-models

depmixS4 Hiden Markov Model HMM data formula


I am attempting to use the depmixS4 package in R for an HMM analysis, but am struggling with how to properly format my data for use in the depmix() function. My goal is to identify two states within the provided train data.

Here is the code I am using to generate the train data:

state1 <- rnorm(500, mean = 0.5, sd = 0.5)
state2 <- rnorm(500, mean = -1, sd = 1.5)

train <- c(state1[1:100], state2[1:100], state1[101:200], state2[101:200])

X.tr <- embed(train, dimension = 5)[,5:1] |> as.data.frame()
plot(X.tr$V5, t = "l", main = "state1 and state2")

The resulting plot is as follows:

enter image description here

I am unsure how to format my response in the depmix() function for multi-dimensional X.tr data. Here is the code I currently have:

library(depmixS4)

# Set up the model
mod <- depmix( ___??___ , data = X.tr, nstates = 2)

# Fit the model
hmm <- fit(mod)

What should I use for ___??___ to properly format the response variable for the depmix() function?


Solution

  • You can use V1 + V2 + V3 + V4 + V5 ~ 1 as the formula to the response. It specifies that all columns (V1 to V5) in X.tr are considered as response variables, and the ~ 1 indicates that there are no covariates.