problem with viterbi() (Error in sub_obs_probs[1, ] : incorrect number of dimensions) #31
-
Hello, I was wondering if anyone had ran into this problem. After I fitted the model getting the decoded statesdecoded_states <- hmm$viterbi() I get the following error:
Any thoughts? Would be happy to share any info as needed. I fitted a 2-state hmm model with two obs data streams. (gamma and beta distribution) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi, could you try running the following example, which fits a 2-state HMM with gamma and beta distributions to simulated data? library(hmmTMB)
# Simulate data from 2-state HMM
n <- 1000
tpm <- matrix(c(0.9, 0.1, 0.1, 0.9), 2, 2)
S <- rep(1, n)
for(i in 2:n) S[i] <- sample(1:2, size = 1, prob = tpm[S[i-1],])
z1 <- rgamma(n = n, shape = c(0.5, 5)[S], scale = c(1/3, 1)[S])
z2 <- rbeta(n = n, shape1 = c(1, 3)[S], shape2 = c(0.5, 5)[S])
data <- data.frame(z1 = z1, z2 = z2)
# Define hidden state model
hid <- MarkovChain$new(data = data, n_states = 2)
# Define observation model
dists <- list(z1 = "gamma", z2 = "beta")
par0 <- list(z1 = list(shape = c(0.3, 4), scale = c(0.5, 1)),
z2 = list(shape1 = c(2, 4), shape2 = c(1, 4)))
obs <- Observation$new(data = data, dists = dists, n_states = 2, par = par0)
# Fit HMM
hmm <- HMM$new(obs = obs, hid = hid)
hmm$fit()
# Compare estimated states to true states
states <- hmm$viterbi()
table(true = S, estimated = states)
# estimated
# true 1 2
# 1 504 1
# 2 1 494 If this runs fine, then it seems that the problem is coming from something that is specific to your model (e.g., numerical problems during model fitting). Could you post the output of |
Beta Was this translation helpful? Give feedback.
-
So I figured it out - I was giving "estimated" for the initial state, but changed it to a potential state based on two observational data. I also couldn't give initial transitional probabilities for some reason, but the issue was resolved when I took the argument out, and the model seems fine. Sorry for the delayed response on this. Thank you so much for all your help and for creating this package! Best, |
Beta Was this translation helpful? Give feedback.
So I figured it out - I was giving "estimated" for the initial state, but changed it to a potential state based on two observational data. I also couldn't give initial transitional probabilities for some reason, but the issue was resolved when I took the argument out, and the model seems fine. Sorry for the delayed response on this. Thank you so much for all your help and for creating this package!
Best,
Aung