R: Simulate Hidden Markov Process

simulate {HiddenMarkov} | R Documentation

## Simulate Hidden Markov Process

### Description

These functions provide methods for the generic function `simulate`.

### Usage

    ## 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"`, `"mmglm0"`, `"mmglm1"`,`"mmglmlong1"`, and `"mmpp"` can be viewed by typing `simulate.dthmm`, `simulate.mmglm0`, `simulate.mmglm1`,
`simulate.mmglmlong1` or `simulate.mmpp`, respectively, on the R command line.

### Value

The returned object has the same class as the input object and contains the components that were in the input object. Where `object` is of class `"dthmm"` it will also have a vector `x` containing the simulated values; and when the class is `"mmglm0"` `x` will be a dataframe. When the class is `"mmpp"` the times of the simulated Poisson events are contained in `tau`. Other variables are also added like the sequence of Markov states, and the time spent in each state (`"mmpp"`).

### Examples

    #    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]))

[Package HiddenMarkov version 1.8-8 Index]