[Rd] Parameter names in nls()

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jul 22 11:50:07 CEST 2008


They are not 'stripped': nothing says that because you named the starting 
value that other values will be named.

You are misusing nls(): the documented way would be to have

data.x <- 1:50
data.y <- pi*data.x + rnorm(50,sd=20)
fitting.fn <-function(x, a, b) a + b*x
nls(data.y ~ fitting.fn(data.x, a, b),data=data.frame(data.x,data.y),
     start=list(a=0, b=0),trace=TRUE,control=nls.control(tol=1e-8))


On Tue, 22 Jul 2008, Mark Payne wrote:

> Dear R-dev,
>
> I have been having some problems with regards to names in the parameter
> vector being stripped when passed to the objective function when using
> nls(). I was relieved to find that it wasn't me, and that this behaviour has
> previously been reported in optim() also. See eg
> https://stat.ethz.ch/pipermail/r-devel/2006-March/036710.html

Note that you are quoting a comment about a different function in a long 
obsolete version of R, with a very different interface.  That's called 
'clutching at straws'.

> The solution at that time was to make a change so that vector passed into
> the objective function was named (eg see following two discussions).
>
> The problem that I am having is virtually identical, but is occuring with
> nls() instead of optim(). To illustrate the problem, I have put together the
> following example code (see bottom). Basically, it is doing a linear least
> squares by hand, but it also displays the names associated on the parameters
> vector - if you run it, you will see that the names are there for the first
> few function evaluations, but after the first "step", the names are dropped.
>
> I was wondering if I could please ask for a similar fix as that applied to
> optim() to be carried across to nls() also?
>
> Many thanks,
>
> Mark
>
>
> #Setup environment
> rm(list=ls())
> counter <- 1
>
> #Objective function for nls
> fitting.fn <-function(x,params) {
>  #The model - so that it works
>  y <- params[1] + x*params[2]
>  #Referring to the parameters this way stops working after the first "step"
> #  y <- params["a"] + x*params["b"]
>
>  #Display information about function eval and parameter names
>  cat(paste("Evaluation # :",counter,"\t Names :"))
>  print(names(params))
>  counter <<- counter +1
>  return(y)
> }
>
> #Synthetic data
> data.x <- 1:50
> data.y <- pi*data.x + rnorm(50,sd=20)
>
> #Fit objective function
> ips <-  c(a=0,b=0)
> nls("data.y~fitting.fn(data.x,params)",data=data.frame(data.x,data.y),
>      start=list(params=ips),trace=TRUE,control=nls.control(tol=1e-8))
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
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-devel mailing list