Let's say you set up your data like this:
set.seed(1)
x = seq(-2, 8, .01)
y = rnbinom(length(x), mu=exp(x), size=10)
fit = glm.nb(y ~ x)
and you have a new point: you want to find the probability of y=100
given x=5
.
You can get the predicted value of y
from x
using predict
(with type="response"
to tell it you want it after the inverse of the link function has been applied):
predicted.y = predict(fit, newdata=data.frame(x=5), type="response")
Then you could find out the probability with:
dnbinom(100, mu=predicted.y, size=fit$theta)
(This is using fit$theta
, the maximum likelihood estimate of the "size" parameter of the negative binomial).
So in one function:
prob = function(newx, newy, fit) {
dnbinom(newy, mu=predict(fit, newdata=data.frame(x=newx), type="response"), size=fit$theta)
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…