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