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
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]]

