[R] Code for generating states and observations for HMM

Rolf Turner r.turner at auckland.ac.nz
Wed Jun 4 23:27:18 CEST 2014


Sorry, I haven't the time at the moment to delve into this in detail, 
but at a quick reading, your question makes no sense.  If you are trying 
to simulate a hidden Markov chain, the states have a distribution as 
determined by the transition probability matrix and
the initial state probability distribution.

I have no idea what you mean by "state "0" to come from
Poisson and state "1" from NB distributions".  As far as I can discern 
this is complete gibberish.

Good luck.

On 05/06/14 08:22, Bukar Alhaji wrote:
> Dear Dr Rolf,
>
> Thank you for your response and suggestion. But i still find it very
> difficult to relate my own problem to the sim.hmm() function you
> suggested. Because the states and the observations should come from
> Poisson and Negative Binomial distributions where state "0" to come from
> Poisson and state "1" from NB distributions. However, i run the code and
> have the states and the observations but not sure if it has been done
> correctly.
>
> Please can you explain how i should make use of sim.hmm() function to
> apply to my case, or can improvement be made on my code?
>
> Thank you so much for givig me your time.
>
> Regards
> Zakir
>
>
>
> On Tuesday, June 3, 2014 11:32 PM, Rolf Turner <r.turner at auckland.ac.nz>
> wrote:
>
>
>
> You might find it helpful to look at the sim.hmm() function in the
> "hmm.discnp" package, or the simHMM() function in the "HMM" package.
>
> cheers,
>
> Rolf Turner
>
> On 03/06/14 21:17, Bukar Alhaji wrote:
>  > Dear R buddies,
>  >
>  > Sorry for this silly question but am new to R. I am trying to
>  > generate states and observations to be use for Bayesian Hidden Markov
>  > Models analysis where i intend using mixture of Poisson and Negative
>  > binomial as emulsion. I use the code below to generate states and
>  > observations for homogeneous HMM . I would like to know if i
>  > correctly generated the data.
>  >
>  >
>  > pii = c(0.6,0.4)
>  > p1 <- matrix(c(0.8,0.2,0.3,0.7),byrow=TRUE,nrow=2)
>  >
>  >
>  >      NUM = 200
>  >      theta<-rep(0, NUM)
>  >      x<-rep(0, NUM)
>  >
>  >      ## generating the states
>  >      # initial state
>  >      theta[1]<-rbinom(1, 1, pii[1])
>  >      # other states
>  >      for (i in 2:NUM)
>  >   {
>  >        if (theta[i-1]==0)
>  >          theta[i]<-rbinom(1, 1, p1[1, 1])
>  >        else
>  >          theta[i]<-rbinom(1, 1, p1[2, 1])
>  >      }
>  >
>  >      ## generating the observations
>  >
>  >      for (i in 1:NUM)
>  >      {
>  >        if (theta[i]==0)
>  >        {
>  >          x[i]<-rpois(1, 5)
>  >        }
>  >        else
>  >        {
>  > x[i]<-rnbinom(1, 3, 0.3)
>  >        }
>  >      }
>  >      data<-list(s=theta, o=x, p1 = p1, pii = pii)
>
>
>



More information about the R-help mailing list