Here is an example code for identifying states in timeseries data using Hidden Markov Models in R.
main.r801 chars28 lines
The above code assumes that the timeseries data is stored in a vector x
. We first create a simulated data with 3 different states. We then define the number of states we want to identify and build an HMM model with transition probabilities between the states. We normalize the observation data and fit the HMM model using the Baum-Welch algorithm. Finally, we apply the Viterbi algorithm to the normalized data, which assigns the most likely state to each observation. We plot the original data with the identified states.
gistlibby LogSnag