[R-sig-eco] k-truncated negative binomial

Ben Bolker bbolker at gmail.com
Sat May 17 00:39:22 CEST 2014


Lee, Laura <laura.lee at ...> writes:

> 
> Hi all!
> 
> Is there a package or function to model data using
> a k-truncated negative binomial distribution? I know the
> truncated function in the 'aster' package can be used 
> to simulate data, but I need to model.
> 
> Thanks!
> 
> Laura


  You could write a k-truncated distribution function, e.g.

ktruncNB <- function(x,mu,size,k,log=FALSE) {
  if (any(x<k)) stop("bad input") ## or set these to zero
  r <- dnbinom(x,mu=mu,size=size,log=TRUE)-
        pnbinom(k,mu=mu,size=size,log.p=TRUE)
  if (log) exp(r) else r
}

And then use it with bbmle:mle2, e.g.

   mle2(y~ktruncNB(mu=exp(logmu),size=exp(logs),k=4),data=mydat,
      start=list(logmu=...,logs=...))

see ?mle2 and in particular the parameters argument for modeling
logmu and/or logs as linear functions of covariates etc. ...

  I hope you don't need to estimate k -- that will be difficult.

  (warning, I haven't actually checked that any of this is right!)



}



More information about the R-sig-ecology mailing list