[R] How should a SelfStart function handle illegal parameter values?

Keith Jewell k.jewell at campden.co.uk
Thu Sep 10 11:07:00 CEST 2009


Hi Everyone,

I'm trying to write selfStart non-linear models for use with nls. In these 
models some combinations of parameter values are illegal; the function value 
is undefined.

That's OK when calling the function directly [e.g.  SSmodel(x, pars...)]; I 
return an appropriate non-value such as NA or Inf.

However, when called from nls [e.g. nls(y~SSmodel(x, pars...), ...)] those 
non-values lead to errors such as (but not limited to):
    Error in numericDeriv(form[[3L]], names(ind), env) :
      Missing value or an infinity produced when evaluating the model
or (if I provide a gradient attribute)
    Error in qr.default(.swts * attr(rhs, "gradient")) :
      NA/NaN/Inf in foreign function call (arg 1)

A toy example demonstrating my problem (legal values of param are >1):
#-----------
SSexample<-selfStart(
 model=function(x, param) x^log(param-1),
 initial = function(mCall, data, LHS){
   val<- 1.001
   names(val) <- mCall[c("param")]
   val
   },
 parameters=c("param")
)
#----------------
nls(y~SSexample(x, par), data=data.frame(x=1:10,y=rnorm(10)))
#---------

(repeat the last line a few times and you'll get the error).

I can't see a way of making nls either stick to legal parameter values, or 
accept NA/NaN/Inf as indicating "bad" parameter values.

I really do want to use nls rather than a bounded optimisation tool (such as 
optim) because this fits into a much bigger picture predicated on nls.

I'd appreciate any suggestions.

Keith Jewell
----------------
> sessionInfo()
R version 2.9.1 (2009-06-26)
i386-pc-mingw32

locale:
LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United
Kingdom.1252;LC_MONETARY=English_United
Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252

attached base packages:
[1] stats     graphics  grDevices datasets  tcltk     utils     methods
base

other attached packages:
[1] xlsReadWrite_1.3.3 svSocket_0.9-43    svMisc_0.9-48      TinnR_1.0.3
R2HTML_1.59-1      Hmisc_3.6-1

loaded via a namespace (and not attached):
[1] cluster_1.12.0  grid_2.9.1      lattice_0.17-25 stats4_2.9.1
VGAM_0.7-9




More information about the R-help mailing list