[R] Error reporting in R

Duncan Murdoch murdoch at stats.uwo.ca
Mon Oct 20 13:03:25 CEST 2008


On 20/10/2008 5:39 AM, A.Noufaily wrote:
> Hello,
> 
> I am hoping someone can help me with the following:
> 
> I am applying the function "mle" on a single data set X n times, each
> time using a different set of initial values v[i] (i=1,...,n).
> The initial values are all finite. Two cases arise:
> A- For some sets of initial values mle is giving parameter estimates.
> B- For some other initial values, mle is reporting the following error: 
> 
> Error in optim(start,f,method=method,hessian=TRUE,...):
> Initial value in vmin is not finite
> 
> My aim is to tell R to return the estimates whenever case A, and to
> return the word "Error" whenever case B (using an "if" statement inside
> a loop).
> 
> How to tell R to return the word "Error" whenever mle reports the error
> given above?
> Is there a function similar to is.nan which tests the existence of an
> error?

Normally an error halts evaluation, but see ?try.  The basic pattern is

result <- try(mle(...))
if (inherits(result, "try-error")) return("Error")
else return(result)

Duncan Murdoch



More information about the R-help mailing list