2

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

Jespar
  • 1,017
  • 5
  • 16
  • 29
  • 1
    Looking at the `?nlm` help page, I don't see a data argument. Also think the parameter list would basically throw away any values if it were not named, because there is no positional matching after `(f, p, ...)` – IRTFM Feb 20 '17 at 02:49
  • You just gave me an idea, let me try something. The data argument supposed to be dat followed by params. I put them in the nlm on the same sequence to match the negbinll – Jespar Feb 20 '17 at 02:51
  • Ok, I feel completely stupid. I thought that no matter the `(f, p, ...)` sequence the function would work. Apparently, I should have constructed the first code like that: `negbinll <- function(params, dat)` instead of dat then params – Jespar Feb 20 '17 at 02:55

1 Answers1

2

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
Sathish
  • 12,453
  • 3
  • 41
  • 59