Search code examples
rhidden-markov-modelspredict

Hidden Markov Model in R - Predict the next observation with RHmm


This is my first post on StackOverflow and I could use a little help... Please forgive me if I am not following the correct posting protocols.

There is another example in the StackOverflow for which I am heavily basing my work off of but I cant quite figure out how to adapt the code. Most importantly, I am looking at the solution to the question provided.

Here is the link:

Getting the next observation from a HMM gaussian mixture distribution

Some background:

RHmm - version 2.1.0 downloaded from R Forge.

RStudio - 0.98.953

R - 3.0.2 32 bit

I am trying to figure out the following issues with my code:

  1. How do I amend the solution from the link above (prediction of the next observation) to work with my Baum-Welch model? Ex. hm_model <- HMMFit(obs=TWII_Train, nStates=5)

  2. The R / RStudio session aborts when I run the Baum-Welch version of the hm_model <- HMMFit(obs=TWII_Train, dis="MIXTURE", nStates=5, nMixt=4). Can you recreate the error and propose a workaround?

Here is my R code:

library(quantmod) 

library(RHmm)

getSymbols("^TWII")

TWII_Subset <- window(TWII, start=as.Date("2012-01-01"), end = as.Date("2013-04-01"))

TWII_Train <- cbind(TWII_Subset$TWII.Close - TWII_Subset$TWII.Open,
                    TWII_Subset$TWII.Volume)


hm_model <- HMMFit(obs=TWII_Train, nStates=5)

VitPath <- viterbi(hm_model, TWII_Train)

Solution

  • I'm not a user of this package and this is not really an answer, but a comment would obscure some of the structures. It appears that the "proportion" value of your model is missing (so the structures are different. The "mean" value looks like this:

    $ mean   :List of 5
      ..$ : num [1:2] 6.72 3.34e+06
      ..$ : num [1:2] -12.4 2420174.5
      ..$ : num [1:2] -2.4 1832546.5
      ..$ : num [1:2] -10.4 1432636.1
      ..$ : num [1:2] 5.02 1.96e+06
    

    I also suspect that you should be using 2 and 5 rather than 4 and 5 for m and n. Look at the rest of the model with:

    str(hm_model)