R – Applying MLE and Methods of Moments for Negative Binomial Distribution

maximum likelihoodmethod of momentsnegative-binomial-distributionr

Question

Let's say we define the Negative Binomial as follows:
$$f(x) = {x+r-1 \choose x} p^x (1-p)^r$$

With mean and variance:
$$E(x) = \frac{rp}{1-p} \quad \quad V(x) = \frac{rp}{(1-p)^2}$$

We are given some set of data and need to get the maximum likelihood estimate and the method of moments estimate. How do we do this?

Attempt at Solution

For method of moments, I did:

M1 = mean(x)
M2 = mean(x^2)
p = 1 - (M1/(M2-M1^2))
r = (1-p)*M1/p
q1b = list(r = r, p = p)
print(q1b)

For MLE I did:

NBd_LLL <- function(x,par) {
  return(-sum(log(dnbinom(x,size = par[0], prob = par[1]))))
}
q1c = optim(par = c(0.85,0.35), NBd_LLL, x = x, method = "L-BFGS-B", lower = c(0.1,0.1), upper = c(1,1))
q1c$par

But these give very different answers, where did it go wrong?

Best Answer

It looks like you have made a mistake in your equations for the MOM estimators. It is best to first try solving these problems mathematically, rather than using computer code. For the MOM estimator, we can begin by observing that we have the following simple equations:

$$\frac{\mathbb{E}(X)}{\mathbb{V}(X)} = 1-p \quad \quad \quad \frac{\mathbb{E}(X)^2}{\mathbb{V}(X)} = rp.$$

Solving these equations for $p$ and $r$ gives:

$$p = 1-\frac{\mathbb{E}(X)}{\mathbb{V}(X)} \quad \quad \quad r = \frac{\mathbb{E}(X)^2}{\mathbb{V}(X)-\mathbb{E}(X)}.$$

Substituting the sample mean $\bar{X}$ and the sample variance $s_X^2$ gives the MOM estimators:

$$\hat{p} = 1-\frac{\bar{X}}{s_X^2} \quad \quad \quad \hat{r} = \frac{\bar{X}^2}{s_X^2-\bar{X}}.$$