gpt4 book ai didi

r - 从 rpois() 生成正实数

转载 作者:行者123 更新时间:2023-12-02 00:36:16 25 4
gpt4 key购买 nike

我正在尝试使用rpois()创建泊松模拟。我有小数点后两位利率的分布,我想知道这些利率是否具有泊松分布而不是正态分布。

rpois() 函数返回正整数。我希望它返回小数点后两位正数。我尝试过以下方法

set.seed(123)
trialA <- rpois(1000, 13.67) # generate 1000 numbers
mean(trialA)
13.22 # Great! Close enough to 13.67
var(trialA)
13.24 # terrific! mean and variance should be the same
head(trialA, 4)
6 7 8 14 # Oh no!! I want numbers with two decimals places...??????

# Here is my solution...but it has a problem

# 1) Scale the initial distribution by multiplying lambda by 100

trialB <- rpois(1000, 13.67 * 100)

# 2) Then, divide the result by 100 so I get a fractional component
trialB <- trialB / 100
head(trialB, 4) # check results
13.56 13.62 13.26 13.44 # terrific !

# check summary results
mean(trialB)
13.67059 # as expected..great!
var(trialB)
0.153057 # oh no!! I want it to be close to: mean(trialB) = 13.67059

如何使用 rpois() 生成具有泊松分布的正两位小数数字。

我知道泊松分布用于计数,并且计数是正整数,但我也相信泊松分布可用于建模速率。这些比率可能只是正整数除以标量。

最佳答案

如果您缩放泊松分布以更改其均值,结果将不再是泊松分布,并且均值和方差不再相等 - 如果您将均值缩放一个因子 s,则方差会改变 s^2 因子。

您可能想使用 Gamma 分布。 Gamma 的均值是 shape*scale ,方差是 shape*scale^2,因此您必须使用 scale=1 来获得均值和方差相等的实数、正数:

set.seed(1001)
r <- rgamma(1e5,shape=13.67,scale=1)
mean(r) ## 13.67375
var(r) ## 13.6694

您可以四舍五入到小数点后两位,而不会极大地改变均值和方差:

r2 <- round(r,2)
mean(r2) ## 13.67376
var(r2) ## 13.66938

与泊松分布比较:

par(las=1,bty="l")
curve(dgamma(x,shape=13.67,scale=1),from=0,to=30,
ylab="Probability or prob. density")
points(0:30,dpois(0:30,13.67),type="h",lwd=2)

enter image description here

关于r - 从 rpois() 生成正实数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29672403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com