Search code examples
roptimizationnlm

Non-Linear Minimization (nlm) in R with error: incorrect number of dimensions


I have this code where it calculates the negative binomial distribution, with parameters:

ce <- c(0, 1, 2, 3, 4, 5)
ce <- as.integer(ce)
comp <- c(257, 155, 64, 17, 5, 2)
comp <- as.integer(comp)
data.cells <- data.frame(ce, comp)

params <- c(5, 1.5) # vector of 2 params, x and κ. 
dat <- data.cells

And the function:

negbinll <- function(dat,params) {
  if (missing(dat)||missing(params)) {
    print('Plese add values for the model')
  } else if (params[1]<0||params[2]<0) {
    print('Plese make sure your parameters are >0')
  } else {
    p <- params[1]/(params[1]+params[2])
    N <- params[1] + dat[,1] - 1
    log.l <- sum(dat[2]*dnbinom(dat[,1], size = N, prob = p, log = TRUE))
    return(log.l)
  }
}

Now, the result from this code is

> negbinll(dat, params)
[1] -591.024

The next step is to use nlm (Non-Linear Minimization) to find the maximum likelihood estimates of x and κ, assume params = (x, κ)

nlm(negbinll, dat, p = params)
Error in dat[, 1] : incorrect number of dimensions

But, if I change in the initial function the dat[,1] to dat[1] I get an error: Non-numeric argument to mathematical function

Any suggestions? Thanks


Solution

  • dat and params values are matched incorrectly inside negbinll function. To debug it, put browser() inside the negbinll function and call the nlm line of code. Inside the debug mode, you can print the values of dat and params and see that they are not matched properly.

    Note: after debugging, remove the browser() command from negbinll function

    negbinll <- function(dat,params) {
      browser()
      if (missing(dat)||missing(params)) {
        print('Plese add values for the model')
      } else if (params[1]<0||params[2]<0) {
        print('Plese make sure your parameters are >0')
      } else {
        p <- params[1]/(params[1]+params[2])
        N <- params[1] + dat[,1] - 1
        log.l <- sum( dat[2] *dnbinom(dat[,1], size = N, prob = p, log = TRUE))
        return(log.l)
      }
    }
    
    Browse[2]> params
    # ce comp
    # 1  0  257
    # 2  1  155
    # 3  2   64
    # 4  3   17
    # 5  4    5
    # 6  5    2
    Browse[2]> dat
    # [1] 5.0 1.5