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