Learn R Programming

HiddenMarkov (version 1.8-13)

simulate: Simulate Hidden Markov Process

Description

These functions provide methods for the generic function simulate.

Usage

# S3 method for dthmm
simulate(object, nsim = 1, seed = NULL, ...)
# S3 method for mchain
simulate(object, nsim = 1, seed = NULL, ...)
# S3 method for mmglm0
simulate(object, nsim = 1, seed = NULL, ...)
# S3 method for mmglm1
simulate(object, nsim = 1, seed = NULL, ...)
# S3 method for mmglmlong1
simulate(object, nsim = 1, seed = NULL, ...)
# S3 method for mmpp
simulate(object, nsim = 1, seed = NULL, ...)

Arguments

object

an object with class "dthmm", "mchain", "mmglm0", "mmglm1", "mmglmlong1" or "mmpp".

nsim

number of points to simulate.

seed

seed for the random number generator.

other arguments.

Value

The returned object has the same class as the input object and contains the components that were in the input object. Additional components depend on the class as below:

"dthmm": it will also have a vector x containing the simulated values;

"mmglm0": it will also contain a dataframe x about the glm; and

"mmpp": tau contains times of the simulated Poisson events (plus time 0), ys is a vector of states at the time of each event, y is the sequence of states visited, and x is the time spent in each state.

Details

Below details about particular methods are given where necessary.

simulate.mmglm0

If the covariate x1 is NULL, then uniform (0,1) variables are generated as the values for x1. When the family is "binomial" and size is NULL (i.e. the number of Bernoulli trials are not specified), then they are simulated as 100+rpois(nsim, lambda=5).

The code for the methods "dthmm", "mchain", "mmglm0", "mmglm1","mmglmlong1" and "mmpp" can be viewed by appending simulate.dthmm, simulate.mchain, simulate.mmglm0, simulate.mmglm1, simulate.mmglmlong1 or simulate.mmpp, respectively, to HiddenMarkov:::, on the R command line; e.g. HiddenMarkov:::dthmm. The three colons are needed because these method functions are not in the exported NAMESPACE.

Examples

Run this code
# NOT RUN {
#    The hidden Markov chain has 5 states with transition matrix:

Pi <- matrix(c(1/2, 1/2,   0,   0,   0,
               1/3, 1/3, 1/3,   0,   0,
                 0, 1/3, 1/3, 1/3,   0,
                 0,   0, 1/3, 1/3, 1/3,
                 0,   0,   0, 1/2, 1/2),
             byrow=TRUE, nrow=5)

#--------------------------------------------
#   simulate a Poisson HMM

x <- dthmm(NULL, Pi, c(0, 1, 0, 0, 0), "pois",
           list(lambda=c(1, 4, 2, 5, 3)), discrete = TRUE)

x <- simulate(x, nsim=2000)

#    check Poisson means
for (i in 1:5) print(mean(x$x[x$y==i]))

#--------------------------------------------
#   simulate a Gaussian HMM

x <- dthmm(NULL, Pi, c(0, 1, 0, 0, 0), "norm",
           list(mean=c(1, 4, 2, 5, 3), sd=c(0.5, 1, 1, 0.5, 0.1)))

x <- simulate(x, nsim=2000)

#    check means and standard deviations
for (i in 1:5) print(mean(x$x[x$y==i]))
for (i in 1:5) print(sd(x$x[x$y==i]))
# }

Run the code above in your browser using DataLab