Re: [R] Parameter names in nls

From: Rolf Turner <r.turner_at_auckland.ac.nz>
Date: Tue, 22 Jul 2008 08:30:36 +1200

On 22/07/2008, at 3:49 AM, rhelp.20.trevva_at_spamgourmet.com 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_at_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_at_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. Received on Mon 21 Jul 2008 - 20:33:30 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 22 Jul 2008 - 11:31:53 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive