Learn R Programming

rarhsmm (version 1.0.7)

hmm.sim: Simulate a Gaussian hidden Markov series with / without autoregressive structures

Description

Simulate a Gaussian hidden Markov series with / without autoregressive structures

Usage

hmm.sim(ns, mod)

Arguments

ns

length of the simulated series

mod

list consisting of at least the following items: mod$m = number of states, mod$delta = vector of prior probabilities, mod$gamma = matrix of state transition probabilies. mod$mu = list of means, mod$sigma = list of covariance matrices. For autoregressive hidden markov models, we also need the additional items: mod$auto = list of autocorrelation matrices. mod$arp = order of autoregressive.

Value

a list containing simulated series and states

References

Rabiner, Lawrence R. "A tutorial on hidden Markov models and selected applications in speech recognition." Proceedings of the IEEE 77.2 (1989): 257-286.

Examples

Run this code
# NOT RUN {
set.seed(135)
#Gaussian HMM 3 hidden states (no autoregressive structure)
m <- 3
mu <- list(c(3),c(-2),c(0))
sigma <- list(as.matrix(1), as.matrix(0.8),as.matrix(0.3))
delta <- c(0.3,0.3,0.4)
gamma <- matrix(c(0.8,0.1,0.1,0.1,0.8,0.1,0.1,0.1,0.8),3,3,byrow=TRUE)
mod1 <- list(m=m,mu=mu,sigma=sigma,delta=delta,gamma=gamma)
sim1 <- hmm.sim(1000,mod1)
y1 <- sim1$series
fit1 <- em.hmm(y=y1, mod=mod1)

#AR(2) Gaussian HMM with 3 hidden states
m <- 2
mu <- list(c(3,4,5),c(-2,-3,-4))
sigma <- list(diag(1.3,3), 
            matrix(c(1,-0.3,0.2,-0.3,1.5,0.3,0.2,0.3,2),3,3,byrow=TRUE))
delta <- c(0.5,0.5)
gamma <- matrix(c(0.8,0.2,0.1,0.9),2,2,byrow=TRUE)
auto <- list(matrix(c(0.3,0.2,0.1,0.4,0.3,0.2,
                     -0.3,-0.2,-0.1,0.3,0.2,0.1,
                      0,0,0,0,0,0),3,6,byrow=TRUE),
            matrix(c(0.2,0,0,0.4,0,0,
                      0,0.2,0,0,0.4,0,
                     0,0,0.2,0,0,0.4),3,6,byrow=TRUE))
mod2 <- list(m=m,mu=mu,sigma=sigma,delta=delta,gamma=gamma,auto=auto,arp=2)
sim2 <- hmm.sim(2000,mod2)
y2 <- sim2$series
fit2 <- em.hmm(y=y2, mod=mod2, arp=2)
# }

Run the code above in your browser using DataLab