如何从 glm.nb() 中获取 "prob" 参数?
How can I get the "prob" parameter out of glm.nb()?
生成概率设置为 .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
我不想作弊以从拟合模型中获取 "prob" 的值。谁能解释为什么 r/(r + mu) = prob?
如果你比较维基百科的定义
C(k+r-1,k) (1-p)^r p^k
与?NegBinomial
中给出的定义
Gamma(x+n)/(Gamma(n) x!) p^n (1-p)^x
你会看到p
和1-p
的角色互换了;如果我们将 NB 定义为 "probability of n successes occurring before one failure",那么维基百科将 p
定义为 "failure" 的概率,而 R 将 p
定义为 "success" 的概率。我从 r/(r+mu)
而不是 mu/(r+mu)
...
得到正确的结果
生成概率设置为 .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
我不想作弊以从拟合模型中获取 "prob" 的值。谁能解释为什么 r/(r + mu) = prob?
如果你比较维基百科的定义
C(k+r-1,k) (1-p)^r p^k
与?NegBinomial
Gamma(x+n)/(Gamma(n) x!) p^n (1-p)^x
你会看到p
和1-p
的角色互换了;如果我们将 NB 定义为 "probability of n successes occurring before one failure",那么维基百科将 p
定义为 "failure" 的概率,而 R 将 p
定义为 "success" 的概率。我从 r/(r+mu)
而不是 mu/(r+mu)
...