Learn R Programming

RcppHMM (version 1.0.1)

viterbi: Viterbi algorithm for hidden state decoding

Description

Function used to get the most likely path of hidden states that generated the observed sequence.

Usage

viterbi(hmm, sequence)

Arguments

hmm

a list that contains all the necesary variables to define a hidden Markov model.

sequence

a vector that contains a sequence of observations to be decoded.

Value

A vector that contains the hidden states traveled that generated the observed sequence.

Details

The Viterbi algorithm is based in a greedy approach, therefore it may no give the real path traveled but the most likely.

References

Cited references are listed on the RcppHMM manual page.

See Also

generateObservations , verifyModel , forwardBackward

Examples

Run this code
# NOT RUN {
# Set the model parameters
n <- c("First","Second")
m <- c("A","T","C","G")
A <- matrix(c(0.8,0.2,
              0.1,0.9),
            nrow = 2,
            byrow = TRUE)

B <- matrix(c(0.2, 0.2, 0.3, 0.3,
              0.4, 0.4, 0.1, 0.1),
            nrow = 2,
            byrow = TRUE)

Pi <- c(0.5, 0.5)

params <- list( "Model" = "HMM",
                "StateNames" = n,
                "ObservationNames" = m,
                "A" = A,
                "B" = B,
                "Pi" = Pi)

HMM <- verifyModel(params)

# Data simulation
set.seed(100)
length <- 100
observationSequence <- generateObservations(HMM, length)

#Sequence decoding
hiddenStates <- viterbi(HMM, observationSequence$Y)
# }

Run the code above in your browser using DataLab