问题描述
我在R MASS
包中使用glm.nb()
函数来估计负二项式回归模型的参数.给定新数据,我该如何计算预测概率(概率质量函数)?我可以使用哪个R函数?
I use glm.nb()
function in R MASS
package to estimate the parameters of a negative binomial regression model. How could I calculate the predicted probability (probability mass function) given new data, which R function can I use?
我的数据集如下. y遵循负二项式分布,x是协变量.我使用glm.nb(y ~ x, data=data)
来估计模型参数.给定新的x
和y
,我该如何计算预测概率.
My dataset is as follows. y follows negative binomial distribution and x is covariate. And I use glm.nb(y ~ x, data=data)
to estimate model parameters. Given new x
and y
, how can I calculate the predicted probability.
有没有一种使用Java进行计算的方法?
Is there a way to calculate it using Java?
y x
91 1.000000
79 1.000000
86 1.000000
32 1.000000
41 1.000000
29 0.890609
44 1.000000
42 1.000000
31 0.734058
35 1.000000
推荐答案
假设您像这样设置数据:
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)
有一个新的观点:给定x=5
的情况下,您想找到y=100
的概率.
and you have a new point: you want to find the probability of y=100
given x=5
.
您可以使用predict
从x
获取y
的预测值(在应用了链接函数的逆函数之后,可以使用type="response"
告诉它您想要它):
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)
(这是使用fit$theta
,即负二项式的大小"参数的最大似然估计).
(This is using fit$theta
, the maximum likelihood estimate of the "size" parameter of the negative binomial).
所以在一个函数中:
prob = function(newx, newy, fit) {
dnbinom(newy, mu=predict(fit, newdata=data.frame(x=newx), type="response"), size=fit$theta)
}
这篇关于如何计算负二项式回归模型的预测概率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!