我正在尝试使用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()
生成具有泊松分布的正两位小数。
我知道Poisson分布用于计数,而计数是正整数,但我也相信Poisson分布可用于模拟速率。这些速率可能只是正整数除以标量。
答案 0 :(得分:11)
如果缩放泊松分布以改变其均值,则结果不再是泊松,和均值和方差不再相等 - 如果您将均值缩放一个因子{{1然后方差改变因子s
。
您可能想要使用Gamma发行版。 Gamma的平均值为s^2
,方差为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