Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用sample()模拟马尔可夫链的概率数不正确_R_Simulation_Markov Chains - Fatal编程技术网

R 使用sample()模拟马尔可夫链的概率数不正确

R 使用sample()模拟马尔可夫链的概率数不正确,r,simulation,markov-chains,R,Simulation,Markov Chains,我想模拟一个马尔可夫链;如果没有,在运行“for”之后,我会收到错误概率数的警告。我不知道我为什么会得到它,也不知道如何解决它 提前谢谢。 这是我的密码: X <- NULL; n <- 100; P <- matrix(c(0, 0, 0, 0, 1, 0, 8/13, 3/13, 1/13, 1/13, 1/16, 3/16, 3/8, 1/4, 1/8, 0, 1/11, 4/11, 5/

我想模拟一个马尔可夫链;如果没有,在运行“for”之后,我会收到错误概率数的警告。我不知道我为什么会得到它,也不知道如何解决它

提前谢谢。 这是我的密码:

X <- NULL; n <- 100;
P <- matrix(c(0, 0, 0, 0, 1,
              0, 8/13, 3/13, 1/13, 1/13,
              1/16, 3/16, 3/8, 1/4, 1/8,
              0, 1/11, 4/11, 5/11, 1/11,
              0, 1/8, 1/2, 1/8, 1/4),
            byrow=T, nrow=5)

X[1] <- sample(c(0,0,0,0,1), 1);

for (i in 2:n){
   X[i] <- sample(c(0,0,0,0,1), 1, prob = P[X[i-1],])
}

X[i-1]
可以是0。那么
P[X[i-1],]
将是一个空矩阵。如果我的初始分布是由
(0,0,0,0,0,1)
@Roland谢谢你,我没有想到这一点,我该怎么解决呢
Error in sample.int(length(x), size, replace, prob) : 
  incorrect number of probabilities