# [R] rzinb (VGAM) and dnbinom in optim

Tim Howard tghoward at gw.dec.state.ny.us
Fri Apr 18 16:44:29 CEST 2008

```Dear R-help gurus (and T.Yee, the VGAM maintainer) -
I've been banging my head against the keyboard for too long now, hopefully someone can pick up on the errors of my ways...

I am trying to use optim to fit a zero-inflated negative binomial distribution.  No matter what I try I can't get optim to recognize my initial parameters. I think the problem is that dnbinom allows either 'prob' or 'mu' and I am trying to pass it 'mu'.  I've tried a wrapper function but it still hangs.  An example:

#set up dummy data,load VGAM, which is where I am getting the zero-inflated neg binom
#I get the same problem without VGAM, using dnbinom in the wrapper instead.
library(VGAM)
phi <- 0.09
size <- 0.7
munb <- 72
x <- rzinb(200, phi=phi, size=size, munb=munb)

#VGAM can fit using vglm... but I'd like to try optim
> fit <- vglm(x~1, zinegbinomial,trace=TRUE)
VGLM    linear loop  1 :  loglikelihood = -964.1915
VGLM    linear loop  2 :  loglikelihood = -964.1392
VGLM    linear loop  3 :  loglikelihood = -964.1392
> Coef(fit)
phi       munb          k
0.1200317 62.8882874  0.8179183

> #build my wrapper function for dzinb
> fncZiNB <- function(phi, size, munb){
+         -sum(dzinb(x=x,phi=phi, size=size, munb=munb,log=TRUE))}
>
> #test the wrapper, seems to work.
> fncZiNB(phi=0.09, size=0.7, munb=72)
[1] 969.1435

#try it in optim
> solut <- optim(fn=fncZiNB,par=c(phi=0.09, munb=72, size=0.7),method = "L-BFGS-B", hessian=TRUE)
Error in dnbinom(x = x, size = size, prob = prob, log = log.arg) :
argument "size" is missing, with no default

I have the same problem with dnbinom.
I'm sure I'm doing something obvious.... any suggestions?
Session info below. Thanks in advance.
Tim Howard
New York Natural Heritage Program

> sessionInfo()
R version 2.6.2 (2008-02-08)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] stats4    splines   stats     graphics  grDevices utils     datasets  methods
[9] base

other attached packages:
[1] VGAM_0.7-6          randomForest_4.5-23

```