我怎样才能得到" prob" glm.nb()中的参数?

时间:2016-04-06 16:05:13

标签: r parameters glm data-fitting

在生成具有等于.007的概率的负二项数据之后,我从glm.nb()拟合得到该数字,但仅通过作弊。

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

我不想欺骗以获得&#34; prob&#34;超出拟合的模型。谁能解释为什么r /(r + mu)= prob?

1 个答案:

答案 0 :(得分: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) ...

得到了正确的结果