我正在尝试在R中模拟齐次有限状态马尔可夫链。在给定初始概率和跃迁概率矩阵的情况下,我想获得特定长度的输出。这是我到目前为止所拥有的
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")
但是,此代码未使用初始概率。我只说t0的状态应该是b。在这里如何使用初始概率?
答案 0 :(得分:1)
您可以使用初始概率来创建初始状态。
M0 = sample(words, 1, prob=initial)
markovchainSequence(10, markovChain, t0=M0)