R"non-random"数模拟

R "non-random" number simulaton

这个问题是关于 R 的。我想生成一个 "non-random" 1 和 0 的样本,其中生成的下一个值取决于前一个值。例如,如果向量中的第一个值为 1,则下一个值为 1 的可能性为 60%,如果下一个值为 0,则下一个值为 0 的可能性为 60%。我有附上向我提出的问题。我非常感谢任何帮助。

鉴于您的条件,我将使用 for 循环生成一个序列。

set.seed(111)

n = 10000                #As the Q said at least 10,000
seq = vector()
seq[1] = sample(0:1, 1)  #Also given in the Q

for(i in 2:n) {
  if(seq[i-1] == 0){
    seq[i] = sample(0:1, 1 , prob = c(0.6,0.4))
  }
  else{
    seq[i] = sample(0:1, 1 , prob = c(0.4,0.6))
  }
}

table(seq)
seq
0    1 
4961 5039