Learn R Programming

markovchain (version 0.4.4)

rmarkovchain: Function to generate a sequence of states from homogeneous or non-homogeneous Markov chains.

Description

Provided any markovchain or markovchainList objects, it returns a sequence of states coming from the underlying stationary distribution.

Usage

rmarkovchain(n, object, what="data.frame",useRCpp = TRUE,...)
markovchainSequence(n, markovchain, t0 = sample(markovchain@states, 1), 
include.t0 = FALSE,useRCpp = TRUE)

Arguments

n
Sample size
object
Either a markovchain or a markovchainList object.
what
It specifies whether either a data.frame or a matrix (each rows represent a simulation) or a list is returned.
...
additional parameters passed to the internal sampler
markovchain
The markovchain object
t0
The initial state.
include.t0
Specify if the initial state shall be used.
useRCpp
Boolean. Should RCpp fast implementation being used? Default is yes.

Value

  • Either a character vector or a data frame

Details

When an homogeneous process is assumed (markovchain object) a sequence is sampled of size n. When an non - homogeneous process is assumed, n samples are taken but the process is assumed to last from the begin to the end of the non-homogeneous markov process.

References

A First Course in Probability (8th Edition), Sheldon Ross, Prentice Hall 2010

See Also

markovchainFit

Examples

Run this code
#define the Markov chain
statesNames=c("a","b","c")
mcB<-new("markovchain", states=statesNames, transitionMatrix=matrix(c(0.2,0.5,0.3,
0,0.2,0.8,0.1,0.8,0.1),nrow=3, byrow=TRUE, dimnames=list(statesNames,statesNames)
                 ))
#show the sequence
outs<-markovchainSequence(n=100,markovchain=mcB, t0="a")
outs2<-rmarkovchain(n=20, object=mcB)

Run the code above in your browser using DataLab