NegBinomial-extensions | 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) = \frac{\Gamma(\theta + y)}{\Gamma({\theta}) \cdot y!} \cdot
\frac{\mu^y \cdot \theta^\theta}{(\mu + \theta)^{\theta + y}}, \quad y \in \{0, 1, 2, \dots\}
Derivatives of the log-likelihood \ell
wrt \mu
:
\frac{\partial \ell}{\partial \mu} = \frac{y}{\mu} - \frac{y + \theta}{\mu + \theta}
\frac{\partial^2 \ell}{\partial \mu^2} = - \frac{y}{\mu^2}
+ \frac{y + \theta}{(\mu + \theta)^2}
Derivatives wrt \theta
:
\frac{\partial \ell}{\partial \theta} = \psi_0(y + \theta) - \psi_0(\theta)
+ \log(\theta) + 1 - \log(\mu + \theta) - \frac{y + \theta}{\mu + \theta}
\frac{\partial^2 \ell}{\partial \theta^2} = \psi_1(y + \theta) - \psi_1(\theta)
+ \frac{1}{\theta} - \frac{2}{\mu + \theta} + \frac{y + \theta}{(\mu + \theta)^2}
\psi_0
and \psi_1
denote the digamma and
trigamma function, respectively.
The derivative wrt \mu
and \theta
:
\frac{\partial^2 \ell}{\partial\mu\partial\theta} =
= \frac{y - \mu}{(\mu + \theta)^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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.