从 rpois() 生成正实数
Generate positive real numbers from rpois()
我正在尝试使用 rpois()
创建泊松模拟。我有一个小数点后两位利率的分布,我想知道它们是否服从泊松分布而不是正态分布。
rpois()
函数returns正整数。我希望它用 return 两位小数代替正数。我尝试了以下
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)
我正在尝试使用 rpois()
创建泊松模拟。我有一个小数点后两位利率的分布,我想知道它们是否服从泊松分布而不是正态分布。
rpois()
函数returns正整数。我希望它用 return 两位小数代替正数。我尝试了以下
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)