2015-04-16 66 views
4

我正在嘗試使用rpois()創建泊松模擬。我有兩個小數位利率的分佈,我想知道這些是否具有泊松而不是正態分佈。從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()生成具有泊松分佈至正二位小數號碼。

我知道泊松分佈用於計數,計數是正整數,但我也相信泊松分佈可以用來模擬速率。這些利率可能只是正數,而不是標量。

回答

11

如果你擴展的泊松分佈,以改變其平均值,其結果是不再泊松,的均值和方差不再是平等的 - 如果你的一個因素s規模均值,然後方差通過改變一個因素s^2

您可能想要使用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

+0

非常感謝Ben這麼全面的回答。非常感激 !問候,馬克。 – markthekoala

+0

如果答案解決了您的問題,建議您點擊複選標記以接受它。 –