代码之家  ›  专栏  ›  技术社区  ›  jpsmith

从具有上/下界的泊松分布中提取的样本数

  •  0
  • jpsmith  · 技术社区  · 6 年前

    在…工作 R ,我需要创建一个长度向量 n 从泊松分布中随机抽取值,λ=1,但下限为2,上限为6(即所有数字将为2、3、4、5或6)。

    我不知道怎么做。我试着创造一个 for 循环,该循环将用该范围内的值替换该范围外的任何值:

    seed(123)
    n<-25 #example length
    example<-rpois(n,1)
    test<-example #redundant - only duplicating to compare with original *example* values
     for (i in 1:length(n)){
       if (test[i]<2||test[i]>6){
         test[i]<-rpois(1,1)
       }
     }
    

    但这似乎不起作用(仍然得到0和1,等等) test ). 任何想法都将不胜感激!

    1 回复  |  直到 6 年前
        1
  •  0
  •   Ronak Shah    6 年前

    这里有一种生成 n 用泊松分布的数代替所有在范围外的数为范围内的随机数。

    n<-25 #example length
    example<-rpois(n,1)
    inds <- example < 2 | example > 6
    example[inds] <- sample(2:6, sum(inds), replace = TRUE)
    
    推荐文章