kathie
2008-Apr-26 23:04 UTC
[R] integration error when I use "optim" and "integrate" simultaneously
Dear R users, When I use two functions, 'optim' and 'integrate', simultaneously, I always get an error like this -------------------------------------------------------------------------- numint = function(z) { dlnorm(z,mu[1],sqrt(exp(g[1]))) * dnorm((z-mu[2])/sqrt(exp(g[2])))/sqrt(exp(g[2])) } integrate(numint,lower=0,upper=Inf)$value Error in integrate(numint, lower = 0, upper = Inf) : non-finite function value ----------------------------------------------------------------------------- Dr. Ripley said for this problem long time ago "You are trying to use a derivative-based optimization method without supplying derivatives. This will use numerical approximations to the derivatives, and your objective function will not be suitable as it is internally using adaptive numerical quadrature and hence is probably not close enough to a differentiable function (it may well have steps)." So, I am trying to avoid this 'integrate' in R, but I have no idea. Any suggestion will be greatly appreciated. Kathryn Lord -- View this message in context: http://www.nabble.com/integration-error-when-I-use-%22optim%22-and-%22integrate%22-simultaneously-tp16918109p16918109.html Sent from the R help mailing list archive at Nabble.com.
Charles C. Berry
2008-Apr-27 05:24 UTC
[R] integration error when I use "optim" and "integrate" simultaneously
On Sat, 26 Apr 2008, kathie wrote:> > Dear R users, > > When I use two functions, 'optim' and 'integrate', simultaneously, I always > get an error like this > > -------------------------------------------------------------------------- > numint = function(z) { > dlnorm(z,mu[1],sqrt(exp(g[1]))) * > dnorm((z-mu[2])/sqrt(exp(g[2])))/sqrt(exp(g[2])) > } > > integrate(numint,lower=0,upper=Inf)$value > > > Error in integrate(numint, lower = 0, upper = Inf) : non-finite function > value > ----------------------------------------------------------------------------- > > Dr. Ripley said for this problem long time ago > > "You are trying to use a derivative-based optimization method without > supplying derivatives. This will use numerical approximations to the > derivatives, and your objective function will not be suitable as it is > internally using adaptive numerical quadrature and hence is probably not > close enough to a differentiable function (it may well have steps)." > > > So, I am trying to avoid this 'integrate' in R, but I have no idea. > > Any suggestion will be greatly appreciated.The implied advice in the quotation above is to use the derivatives. The function you have is the product of a lognormal and a normal density. You need the derivatives of those w.r.t their means and variances. You can probably look these up someplace if the challenge of hacking them out is too daunting. (Hint: how do you find the MLEs for these densities?) You can appreciate why integrate() sometimes complains if you spend some time looking at curve( numint, 0, uplim ) for suitably chosen values of mu and g and uplim. Say mu <- c(1,1) g <- c(10,1) uplim <- 0.000001. But replacing integrate with a better quadrature rule for this particular problem probably will not smooth it out enough to substitute for derivatives. HTH, Chuck> > Kathryn Lord > -- > View this message in context: http://www.nabble.com/integration-error-when-I-use-%22optim%22-and-%22integrate%22-simultaneously-tp16918109p16918109.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901