NegBinomial-extensions {countreg} | R Documentation |
Score function, hessian, mean, and variance
for the negative binomial distribution
with parameters mu
and size
.
snbinom(x, mu, size, parameter = c("mu", "size"), drop = TRUE) hnbinom(x, mu, size, parameter = c("mu", "size"), drop = TRUE) mean_nbinom(mu, size, drop = TRUE) var_nbinom(mu, size, drop = TRUE)
x |
vector of quantiles. |
mu |
_mean_ of distribution. |
size |
dispersion parameter. Must be strictly positive. |
parameter |
character. Derivatives are computed wrt this paramter. |
drop |
logical. Should the result be a matrix ( |
The negative binomial with mu
and size
(or theta
) has density
f(y | mu, theta) = Gamma(theta + y) / Gamma(theta) * y! * mu^y * theta^theta / (mu + theta)^(θ + y), y = 0, 1, 2, ...
Derivatives of the log-likelihood loglik wrt mu:
d loglik / d mu = y/mu - (y + theta)/(mu + theta)
d^2 loglik / d mu^2 = - y / mu^2 + (y + theta)/(mu + theta)^2
Derivatives wrt theta:
d loglik / d theta = psi0(y + theta) - psi0(theta) + log(theta) + 1 - log(mu + theta) - (y + theta)/(mu + theta)
d^2 loglik / d theta^2 = psi1(y + theta) - psi1(theta) + 1/theta - 2/(mu + theta) + (y + theta)/(mu + theta)^2
psi0 and psi1 denote the digamma and trigamma function, respectively.
The derivative wrt mu and theta:
d^2 loglik / d mu d theta = (y - mu)/(μ + θ)^2
snbinom
gives the score function, i.e., the 1st
derivative of the log-density wrt mu or theta and
hnbinom
gives the hessian, i.e., the 2nd
derivative of the log-density wrt mu and/or theta.
mean
and var
give the mean and
variance, respectively.
No parameter prob
—as in dnbinom
, pnbinom
,
qnbinom
and rnbinom
—is implemented in the
functions snbinom
and hnbinom
.
NegBinomial encompassing dnbinom
, pnbinom
,
qnbinom
and rnbinom
.
## Simulate some data set.seed(123) y <- rnbinom(1000, size = 2, mu = 2) ## Plot log-likelihood function par(mfrow = c(1, 3)) ll <- function(x) {sum(dnbinom(y, size = x, mu = 2, log = TRUE))} curve(sapply(x, ll), 1, 4, xlab = expression(theta), ylab = "", main = "Log-likelihood") abline(v = 2, lty = 3) ## Plot score function curve(sapply(x, function(x) sum(snbinom(y, size = x, mu = 2, parameter = "size"))), 1, 4, xlab = expression(theta), ylab = "", main = "Score") abline(h = 0, lty = 3) abline(v = 2, lty = 3) ## Plot hessian curve(sapply(x, function(x) sum(hnbinom(y, size = x, mu = 2, parameter = "size"))), 1, 4, xlab = expression(theta), ylab = "", main = "Hessian") abline(v = 2, lty = 3)