How to properly optimize negative binomial?

I have some problems optimizing negative binomial. I always get the same error. I tried to play around with positive initial values, but that doesn't solve the problem.

initial value in 'vmmin' is not finite

      

What I've done:

  • I have determined the log likelihood

    sum(lgamma((theta/(sigma2 - 1)) + 1) - lgamma(outcome+1) -lgamma(theta/(sigma2 -1))  + outcome*log((sigma2-1)/sigma2) -   (theta/(sigma2 - 1)) *log(sigma2))
    
          

  • I have defined the whole function as follows. This seems to work:

    ll.negbin <- function(par, covariates, outcome){
    if(!all(covariates[,1] == 1))# here I add intercept{
    covariates <- as.matrix(cbind(1,covariates))}
    theta <- covariates%*%par[1:ncol(covariates)]
    gamma <- par[(ncol(covariates)+1)]
    sigma2 <- exp(gamma)+1
    out <- sum(lgamma((theta/(sigma2 - 1)) + 1) - lgamma(outcome+1) -lgamma(theta/(sigma2 -1)) + outcome*log((sigma2-1)/sigma2) -   (theta/(sigma2 - 1)) *log(sigma2))
    }
    
          

  • optimized with optimization:

    opt.negbin <- optim(par = rep(0, ncol(data[,2:6]) + 1),# here you can  put your in.var. inside
    fn = ll.negbin,
    covariates = data[,2:6], # here you can put your columns of in.var.(,2:6).
    outcome = data$,         # here you can put your dependent variable
    control = list(fnscale = -1),
    hessian = T,
    method = "BFGS")
    
          

+3


source to share





All Articles