Q:

Creating a string of specific length using Markov chain

I'm trying to simulate a homogeneous finite state Markov chain in R. I want to get an output of some specific length given initial probability and transition probability matrix. This is what I have so far
 library(markovchain)
 words <- c("h", "e", "l", "o")

 ###Initial probability
 initial <- cbind(c(0.25,0.25,0.25,0.25))

# define the transition matrix (each row sums to 1)
transitions <-  rbind(c(0.1, 0.2, 0.3, 0.4),
                  c(0.1, 0.2, 0.3, 0.4),
                  c(0.1, 0.2, 0.3, 0.4),
                  c(0.1, 0.2, 0.3, 0.4))

rownames(transitions) <- colnames(transitions) <- words

library(markovchain)
markovChain <- new("markovchain", states=words, 
               transitionMatrix = transitions)

markovchainSequence(10, markovChain, t0="h")

This code, however, is not using initial probability. I only state that the state at t0 the state should be b. How can I use initial probability over here?

A:

You can create an initial state using your initial probabilities. 
M0 = sample(words, 1,  prob=initial)
markovchainSequence(10, markovChain, t0=M0)

