I hope it will be here, not a math forum.
I am trying to calculate the negative binomial density in R. My parameters are currently in the form mu(average number of expected failures) and the parameter is overdispersion k. Since I call dnbinomin the compiled C code, I need to convert these parameters to the default parameterization dnbinom, which expects the probability of success in each test pand the number of successes nuntil the count stops, I used the following equations to solve pboth nin terms of muand k:
n = mu*p/(1-p)
mu + mu^2/k = n(1-p)/p^2
After a bit of algebra I get
p = k/mu + 1
n = -(mu^2+k*mu)/k
Checking these equations with various parameterizations dnbinomin R reveals the problem:
> k = 1.2
> mu = 15
> p = k/mu+1
> n = -(mu*k+mu^2)/k
> dnbinom(10,size=n,prob=p)
[1] NaN
Warning message:
In dnbinom(x, size, prob, log) : NaNs produced
> dnbinom(10,mu=mu,size=k)
[1] 0.03560668
What about the parameterization of R that I will skip? I am pretty sure my algebra is correct. Alternatively, is there a way to stick to the original parameterization (in terms of muand k) when called dnbinomfrom C?
Sarah source
share