2016-04-06 34 views
2

在生成prob概率設置爲.007的負二項數據後,我從glm.nb()中得到該數字,但只能通過作弊。如何從glm.nb()中獲取「prob」參數?

library(MASS) 
counts<-data.frame(as.matrix(rnbinom(10000, prob = .007, size = 247))) 
names(counts)<-"y" 

head(counts) 

fitted_model<-glm.nb(y ~ 1, data = counts, link="identity") 

#Theta is the shape parameter of the negative binomial distribution. So this is "r". 
r<-theta.ml(fitted_model$y, fitted(fitted_model))[1]  
# the parameter r is referred to as the 「dispersion parameter」 or 「shape parameter」 

mu<-coef(fitted_model) #This is the mean 

# mu=prob*r/(1-prob) according to https://en.wikipedia.org/wiki/Negative_binomial_distribution 
# so prob = 1/(r + mu) ? 
1/(r + mu) # Wrong! This isn't the prob I used to generate th data! 
r/(r + mu) # Right! But why does this get me the correct value of prob? 

#This has hints: http://www.wright.edu/~thaddeus.tarpey/ES714glm.pdf 

我不想欺騙從合適的模型中獲取「prob」的值。任何人都可以解釋爲什麼r /(r + mu)= prob?

回答

2

如果你比較維基百科的定義

C(k+r-1,k) (1-p)^r p^k 

?NegBinomial

Gamma(x+n)/(Gamma(n) x!) p^n (1-p)^x 

給出的定義,你會看到,p1-p角色切換;如果我們將NB定義爲「在一次失敗之前發生n次成功的概率」,那麼維基百科定義p爲「失敗」的概率,而R定義p爲「成功」的概率。我從r/(r+mu)而不是mu/(r+mu)得到了正確的結果...

+0

現在我已經學到了兩件事:解決這個問題的方法,以及總是比較兩個系統中的pdf公式的規則。 – rwinkel2000