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? Thanks, Kate [[alternative HTML version deleted]]
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
Your problem is numerical. Try replacing log(prod(dcauchy(x,mu,s))) by sum(log(dcauchy(x,mu,s))) and see the difference. Here's what I get:> mu <- 2 > s <- 1 > n <- 300 > library(maxLik) > set.seed(1004) > x <- rcauchy(n,mu,s) > loglik <- function(mu) {+ sum(log(dcauchy(x,mu,s))) + }> maxNR(loglik,start=median(x))$estimate[1] 2.075059 which looks about right to me. Bill Venables CSIRO/CMIS Cleveland Laboratories -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of kate Sent: Friday, 12 March 2010 3:21 PM To: r-help at r-project.org Subject: [R] Question regarding to maxNR 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? Thanks, Kate [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Maybe Matching Threads
- maxNR in maxLik package never stops
- maxNR - Error in p(a, b) : element 1 is empty; the part of the args list of '*' being evaluated was: (b, t)
- Splitting up the micEcon package?
- [External] Re: unable to access index for repository...
- Taking Integral and Optimization using Integrate, Optim and maxNR