2014-04-16 46 views
1

如果此問題很簡單,道歉。想象一下,我們有20個球的選擇概率爲0.5+1:20/50與binom和R中的其他方法的差異

P=0.5+1:20/50 
ONE=function(M,n) { 
     N=1:n 
     m=length(M) 
     for(j in 1:n) { 
      N[j]=sum(runif(m)<M) 
     } 
     N 
    } 

mean.1=mean(ONE(P,100000)) 
mean.2=mean(rbinom(100000,size=20,prob=P)) 

在這裏很容易看到,兩者都使用這兩種方法是平等的。但是,當我想要計算P(X < = 5),其中X是指所選擇的球的數量,一些奇怪發生了:

ONE.p=function(M,n) { 
     N=1:n 
     m=length(M) 
     for(j in 1:n) { 
      N[j]=sum(runif(m)<M) 
     } 
     sum(N<=5)/n 
    } 
p.1=ONE.p(P,100000) 
p.2=sum(rbinom(100000,size=20,prob=P)<=5)/100000 

這裏,p.1幾乎0,但p.2估計爲e-3水平。使用hist(),我們可以看到:

hist(ONE(P,100000)) 
hist(rbinom(100000,size=20,prob=P)) 

第二個更寬。我認爲這兩個提供了相同的計算。但我對結果感到困惑。任何援助表示讚賞。

+0

從這個:'rbinom(30,10,代表(C(0, 1),每個= 5)'我有一些線索,但是我的頭腦只會傷害思想這麼久,所以這會有所幫助,但我不清楚。 – Zander

回答

0

觀察次數與每次觀察試驗次數之間存在差異。 rbinom假定在每次觀測中,概率是相同的,但不同觀測之間的概率可能不同。在你的ONE函數中,你完全假設它是相反的。下面是這個區別的一個小例子:

# 2 observations of size 2. 
# in the first observation, both trials have 0 probability, 
# in the second observation, both trials have probability 1. 
rbinom(2, 2, 0:1) 
# replicate 2 observations of size 1 twice. 
replicate(2, sum(rbinom(2, 1, 0:1))) 

意思是相同的,因爲總和是一樣的。

Nsims <- 100000 
sum(rbinom(Nsims*20, 1, prob=P)) 
sum(rbinom(20, Nsims, prob=P)) 
sum(rbinom(Nsims, 20, prob=P)) 
# this is different because it only uses P[1] 
sum(rbinom(1, 20*Nsims, prob=P)) 

所以,如果你想與做你rbinom函數所做的事情,這裏是如何做到這一點的樣子:

require(ggplot2) 
require(reshape2) 
Nsims <- 100000 
df <- data.frame(v.ONE = ONE(P,Nsims), 
       v.replicate = replicate(Nsims, sum(rbinom(20, size=1, prob=P))), 
       v.size20 = rbinom(Nsims, size=20, prob=P)) 
df.melt <- melt(df, measure.vars=c("v.ONE", "v.replicate", "v.size20")) 
ggplot(df.melt, aes(factor(value), fill=variable)) + geom_histogram(position="dodge")