Xin <jasonshi510 <at> hotmail.com> writes:
>
> Dear All:
>
> I used optim() to minimise the loglikelihood function for fitting data
to
negative binomial> distribution. But there initial value of log-likelihood and iteration 10
value
are reasonable. for example:> initial value 1451657.994524
> iter 10 value 47297.534905
> iter 20 value -623478636.8236478
>
> Then the iter 20 vlaue suddelnly changes to a negative value and in the end
the error mesage is > "non finite finite different [13]"
>
> Has any one have this experience of what the wrong is?
>
> Thanks!
>
> Xin
>
> My function is:
>
> function (parameters,y,x1,x3)
> {
>
> alpha<-parameters[1:10];
> beta<-parameters[11];
> g<-parameters[12];
> theta<-parameters[13];
>
> j=x3
>
> p=alpha[j]*(x1^beta)*exp(-g*x1)
>
> ifelse(x1>0,
>
> L<-lgamma(y+p)+p*log(theta)+y*(log(1-theta))-lfactorial(y)-lgamma(p)
>
> ,Inf)
>
> L
>
> }
>
It would help if you could provide self-contained/reproducible
code, which this is not (we don't have the data, x1). There are
several slightly odd things about this code, but I'm guessing
that the problem is that you don't restrict alpha, beta, theta
to be positive. You could also be getting overflow errors.
Other points about the code:
- you probably want the _sum_ of the log-likelihoods
(unless you just have one data point -- i.e.
length(x1) and length(y) are both 1 ! )
- you can use dnbinom(...,log=TRUE) instead of spelling
out the probability distribution.
- you probably want
L <- ifelse(x1>0,...,Inf)
instead of what you have written here.
- A way of doing further debugging is to use cat() statements
to print out parameter values and negative log-likelihoods for
each function call.
good luck
Ben Bolker