简体   繁体   English

R生成聚类的伪随机数

[英]R generate clustered pseudo-random numbers

In R: I am trying to figure out a way to generate vectors with values 0 or 1. Rather than drawing each 0 and 1 independtly from a uniform distribution I would like the 1s to come clustered eg (1,0,0,0,0,0,1,0,1,1,1,1,0,1,0,0,0,0,1,0,0,0,...). 在R中:我试图找出一种生成值为0或1的向量的方法,而不是希望从一个均匀分布中独立绘制每个0和1,我希望1聚类,例如((1,0,0,0, 0,0,1,0,1,1,1,1,0,1,0,0,0,0,1,0,0,0,...)。 In its most simple form something like: "if the previous number was 1 then increase the likelihood of drawing 1". 最简单的形式是:“如果先前的数字为1,则增加绘制1的可能性”。 Or make the chance of drawing 1 be dependent of the sum of the last say 5 numbers drawn. 或者使抽奖1的机会取决于最后说出的5个数字的总和。 Is there an efficient way of doing this, maybe even a package. 有没有一种有效的方法可以做到这一点,甚至可以打包。 Would be reminiscent of rbinom(n,1,prob) with variable prob . 会让人联想到带有可变probrbinom(n,1,prob)

You can try the following method using a loop. 您可以使用循环尝试以下方法。 First you can create a variable called "x" using sample which will assign an initial value of 0 or 1. 首先,您可以使用sample创建一个名为“ x”的变量,该变量将分配初始值0或1。

Within the loop you can use the sample function again, but this time you assign values to the prob option. 在循环中,您可以再次使用sample函数,但是这次您将值分配给prob选项。 For this purpose I've set the probability to 70/30 split (ie if your previous number was a 0, there is a 70% chance that the next number will be a 0 and vice versa if your previous value was 1.) 为此,我已将概率设置为70/30分割(即,如果您之前的数字为0,则如果您先前的值为1,则下一个数字有70%的可能性为0,反之亦然)

x = sample(c(0,1),1)
for(i in 2:100){
  if(x[i-1] == 0){
    x[i] = sample(c(0,1),1,prob=c(0.7,0.3))
  } else {
    x[i] = sample(c(0,1),1,prob=c(0.3,0.7))
  }
}

x[1:20]
[1] 1 1 1 0 0 0 0 0 1 1 1 0 1 0 0 0 1 1 0 0

So I took good inspiration from Colin Charles, and added a little adjustability. 因此,我从Colin Charles获得了很好的启发,并增加了一些可调整性。 There are obviously many ways to compute prob as being influenced by prior draws. 显然,有许多方法可以计算出受先前抽奖影响的概率。 I ended up using a cutoff m of the sum of the last w draws to determine whether to use low prob p0 or high prob p1 for each 0/1 to make vector of length l . 我最后使用了最后w的总和的截止m来确定对于每个0/1是使用低概率p0还是高概率p1来生成长度为l向量。

f <- function (l, w, m, p0, p1){

  v = rbinom(w,1,p0) #Initilize with p0

    for (i in w:(l-1)){
      v[i+1] <- ifelse(sum(v[(i-w+1):i]) > m, 
                       rbinom(1,1,p1),  
                       rbinom(1,1,p0))
    }

    return(v)
  }

#Test:
set.seed(8)
plot(f(100, 5, 1, 0.1, 0.6)) #Clustered
plot(f(100, 5, 2, 0.1, 0.4)) #Less clustered

Gives: 给出:

聚类

and (less clustered): 和(较少聚类):

集群较少

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM