[R] Question regarding to maxNR

Arne Henningsen arne.henningsen at googlemail.com
Fri Mar 12 07:21:39 CET 2010


Hi Kate!

On Fri, Mar 12, 2010 at 6:20 AM, kate <yhsu6 at illinois.edu> wrote:
> Hi R-users,
>
> Recently, I use maxNR function to find maximizer. I have error appears as follows
> Error in maxNRCompute(fn = fn, grad = grad, hess = hess, start = start,  :
>  NA in the initial gradient
>
> My code is
>
> mu=2
> s=1
> n=300
> library(maxLik)
> set.seed(1004)
> x<-rcauchy(n,mu,s)
> loglik<-function(mu)
> {
> log(prod(dcauchy(x,mu,s)))
> }
> maxNR(loglik,start=median(x))$estimate
>
>
> Does anyone know how to solve this problem?

Yes :-)

Algebraically, "log(prod(z))" is equal to "sum(log(z))" but these two
expressions might return different numbers on digital computers:

R> ll<-function(mu) { sum(log(dcauchy(x,mu,s))) }
R> ll(2)
[1] -754.4928
R> loglik(2)
[1] -Inf

R> maxNR(ll,start=median(x))$estimate
[1] 2.075059

Best wishes from Copenhagen,
Arne

-- 
Arne Henningsen
http://www.arne-henningsen.name



More information about the R-help mailing list