On 22/07/2008, at 3:49 AM, [EMAIL PROTECTED] wrote:
Dear R-help,
Could you please examine the following code, and see if I have
discovered a bug or not, or am just doing something silly.
I am trying to create a package to do fish stock assessment using
the nls() function to fit the modelled stock size to the various
pieces of information that we have. The main problem with this sort
of task is that the number and type of parameters that go into the
model are highly variable between stocks, but the method needs to
be "intelligent" enough to handle this. The way I have chosen to
handle this is through the names in my parameter vector, and using
code inside the objective function to figure out which parameter is
which.
The problem I have encountered is that I don't think nls() always
passes a named vector - indeed, after the first set of function
evaluations, it drops the names from the parameters vector
altogether. I believe this to be a bug - it certaintly plays havoc
with my code!
As a demonstration of this problem, consider the piece of code
below. It is basically fitting a straight line to some synthetic
data (with noise). I have setup the objective function so that it
prints the names of the parameters every time that it is called. As
you can see, the names are there to begin with, but rapidly
disappear after the first "step" is made.
Is this a bug? Or is it intended behaviour? Or is this a completely
daft approach I am taking?
I think the latter. You are simply not using nls correctly. Try
fit <- nls(data.y ~ a + b*data.x, start=ips)
(and compare with the result of lm(data.y ~ data.x)).
cheers,
Rolf Turner
I look forward to your comments.
cheers,
Mark
rm(list=ls())
fitting.fn <-function(x,params) {
#The model - so that it works
y <- params[1] + x*params[2]
#How I would prefer it to work
# y <- params["a"] + x*params["b"]
#Display information about function eval
cat(paste("Evaluation # :",counter,"\t Names :"))
print(names(params))
counter <<- counter +1
return(y)
}
counter <<- 1
data.x <- 1:50
data.y <- pi*data.x + rnorm(50,sd=20)
plot(data.x,data.y)
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))
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-
guide.html
and provide commented, minimal, self-contained, reproducible code.
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.