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
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:
[hidden email] UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide
http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.