simulate: Simulate Hidden Markov Process

Description Usage Arguments Details Value Examples

Description

These functions provide methods for the generic function simulate.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
## S3 method for class 'dthmm'
simulate(object, nsim = 1, seed = NULL, ...)
## S3 method for class 'mchain'
simulate(object, nsim = 1, seed = NULL, ...)
## S3 method for class 'mmglm0'
simulate(object, nsim = 1, seed = NULL, ...)
## S3 method for class 'mmglm1'
simulate(object, nsim = 1, seed = NULL, ...)
## S3 method for class 'mmglmlong1'
simulate(object, nsim = 1, seed = NULL, ...)
## S3 method for class '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.

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.

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.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#    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]))

Example output

[1] 1.038781
[1] 3.968566
[1] 2.044397
[1] 4.963054
[1] 2.960159
[1] 0.9769983
[1] 4.053241
[1] 2.032573
[1] 4.970545
[1] 3.004621
[1] 0.4766802
[1] 1.004878
[1] 1.045068
[1] 0.5459314
[1] 0.1011329

HiddenMarkov documentation built on April 27, 2021, 5:06 p.m.