生成随机泊松值

huangapple go评论49阅读模式
英文:

r generate random poisson values

问题

如何随机生成50%的1、30%的2、15%的3和剩余的4?

最后,当我创建一个表格时,table(x, useNA = "ifany") 应该如下所示:

    1    2    3    4
   50   30   15   5
我不确定如何使用 `rpois` 来生成这个。
英文:

How do I randomly generate 50% 1s, 30% 2s, 15% 3s and remaining 4s ?

Finally when I do a table , table(x, useNA = "ifany"), it should be

                         1      2      3    4
                         50     30     15   5

I am not sure how to use rpois to generate this.

答案1

得分: 3

使用 sample.int

    set.seed(42)
    x <- sample.int(n=4, size=1e4, replace=TRUE, prob=c(.5, .3, .15, 1 - sum(c(.5, .3, .15))))
    
    proportions(table(x))
    # x
    #      1      2      3      4 
    # 0.5001 0.3001 0.1506 0.0492 

如果你依赖于 rpois,你可能需要用 optimize 创造一些东西。

英文:

Using sample.int.

set.seed(42)
x &lt;- sample.int(n=4, size=1e4, replace=TRUE, prob=c(.5, .3, .15, 1 - sum(c(.5, .3, .15))))

proportions(table(x))
# x
#      1      2      3      4 
# 0.5001 0.3001 0.1506 0.0492 

If you depend on rpois, you probably need to invent something with optimize.

huangapple
  • 本文由 发表于 2023年2月19日 15:45:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定