[R] Error message handling

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jun 24 08:37:52 CEST 2004

On Wed, 23 Jun 2004 Kang.Changku at epamail.epa.gov wrote:

> Dear, R experts.
> Does anybody have experience with 'optim' function?


> I have an error message as the following.
> Error in optim(transcoefs, fn = hfdeviance, gr = hfdeviance.grad, method
> = "BFGS",  :
> initial value in vmmin is not finite
> I want to make a comment when this happen.

     Function 'fn' can return 'NA' or 'Inf' if the function cannot be
     evaluated at the supplied value, but the initial value must have a
     computable finite value of 'fn'. (Except for method '"L-BFGS-B"'
     where the values should always be finite.)

It's your error so you can control it (by not making the error).

> Is there way I can put *my* message after this error occur?

You can use try/tryCatch and similar constructs.  But it would be better
to use a valid starting value as optim asks, and you can so that by
calling hfdeviance(transcoefs) and checking it is finite.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list