I am writing this up because it seems like there should be a standard, easy way to do this and I just haven’t found it.
I wrote up my Viterbi algorithm in Stan as shown here in the Stan User’s Guide, which gives me the most likely state sequence for my HMM at each iteration of the Markov chain. If I want a single most likely state sequence that doesn’t change with the iteration, I have a two ideas:
- Take the mode of the sequences across all iterations. So I will end up with the state sequence that appears most often. Note that it is not very fast in R to find the counts of unique rows to find the most common rows or to generally find the “mode” row, especially if the chain is very long.
- First find the posterior mean for model parameters and use that to calculate the Viterbi algorithm. I don’t know if this is commonly done. I’m also not completely sure how to do this in Stan and whether it should be done in the
generated quantities
block or elsewhere. I’d be fine with doing it in R after running the model in Stan if this is most appropriate.
Which of these would make more sense?
Thanks in advance!