[R] Parameter names in nls

rhelp.20.trevva at spamgourmet.com rhelp.20.trevva at spamgourmet.com
Tue Jul 22 10:13:13 CEST 2008


Kiaora Rolf,

Thanks for the reply. The syntax that I am using is based on examples
from ?optim, where a very similar system is setup with the rosenbrook
banana function. Given that nls() is basically a wrapper for optim(),
it seems reasonable that the logic should carry across. Furthermore,
the formula that I have provided ie "data.y~fitting.fn(data.x,params)"
is recognised and accepted by nls, and gives the intended result. It
seems to me that the approach I have taken is not that daft (is it?).

One of my collegues also tells me that there was a similar problem a
while back with optim() stripping parameter names...

Cheers,

Mark


2008/7/21 Rolf Turner - r.turner at auckland.ac.nz
<+rhelp+trevva+97aab9053a.r.turner#auckland.ac.nz at spamgourmet.com>:
>
> 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 confidential. If you are not
> theintended recipient please delete the message and notify the sender.Any
> views or opinions presented are solely those of the author.
>
> This e-mail has been scanned and cleared by
> MailMarshalwww.marshalsoftware.com
> ######################################################################
>



More information about the R-help mailing list