Examples for hidden Markov models with longitudinal data

I have an example here with continuous-valued random effects in the state-dependent distributions: https://github.com/vianeylb/WhiteSharkGuadalupe/blob/master/WhiteShark_StepTurn_RandEff.stan

The only hierarchical part is in the means of the state-dependent distributions.

\mu_{ni} \sim N(\mu_n, \tau_n)

In the code I include the number of individuals (NindivID) and a vector indicating which observations corresponds to which shark indivID in the data block.

  //hierarchical steps
  int<lower=1> NindivID; // individual IDs
  int<lower=1, upper=NindivID> indivID[Tlen]; // individual IDs

In the parameter block I include

positive_ordered[N_gps] mu_step[NindivID];

so that Stan knows that there are individual-specific means for each individual. This can easily be extended into the transition probability matrix as well.

Hope that helps get you started!

4 Likes