Why you get into trouble?
If you do lnlike(c(10, 0.3))
, you get -Inf
. That's why your error message is complaining lnlike
, rather than optim
.
Often, n
is known, and only p
needs be estimated. In this situation, either moment estimator or maximum likelihood estimator is in closed form, and no numerical optimization is needed. So, it is really weird to estimate n
.
If you do want to estimate, you have to be aware that it is constrained. Check
range(xi) ## 5 15
You observations have range [5, 15]
, therefore, it is required that n >= 15
. How can you pass an initial value 10? The searching direction for n
, should be from a large starting value, and then gradually searching downward till it reaches max(xi)
. So, you might try 30
as the initial value for n
.
Additionally, you don't need to define lnlike
in the current way. Do this:
lnlike <- function(theta, x) -sum(dbinom(x, size = theta[1], prob = theta[2], log = TRUE))
optim
is often used for minimization (though it can do maximization). I have put a minus sign in the function to get negative log likelihood. In this way, you are minimizing lnlike
w.r.t. theta
.
- You should also pass your observations
xi
as additional argument to lnlike
, rather than taking it from global environment.
Naive try with optim
:
In my comment, I already said that I don't believe using optim
to estimate n
will work, because n
must be integers while optim
is used for continuous variables. These errors and warnings shall convince you.
optim(c(30,.3), fn = lnlike, x = xi, hessian = TRUE)
Error in optim(c(30, 0.3), fn = lnlike, x = xi, hessian = TRUE) :
non-finite finite-difference value [1]
In addition: There were 15 or more warnings (use warnings() to see the
first 15
> warnings()
Warning messages:
1: In dbinom(x, size = theta[1], prob = theta[2], log = TRUE) : NaNs produced
2: In dbinom(x, size = theta[1], prob = theta[2], log = TRUE) : NaNs produced
3: In dbinom(x, size = theta[1], prob = theta[2], log = TRUE) : NaNs produced
4: In dbinom(x, size = theta[1], prob = theta[2], log = TRUE) : NaNs produced
5: In dbinom(x, size = theta[1], prob = theta[2], log = TRUE) : NaNs produced
Solution?
Ben has provided you a way. Instead of letting optim
to estimate n
, we manually do a grid search for n
. For each candidate n
, we perform a univariate optimization w.r.t. p
. (Oops, in fact, there is no need to do numerical optimization here.) In this way, you are getting a profile likelihood of n
. Then, we find n
on the grid to minimize this profile likelihood.
Ben has provided you full details, and I shall not repeat that. Nice (and swift) work, Ben!