[R] Parameter names in nls

Ben Bolker bolker at ufl.edu
Tue Jul 22 16:14:52 CEST 2008


 <rhelp.20.trevva <at> spamgourmet.com> writes:

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

  I don't know about "daft", but I think you're a little bit
mistaken about nls -- it is NOT a wrapper for optim.
Because nls accepts formulas with arbitrary variables,
it has to do some funky things with names and evaluation ...

  The following seems a little bit more idiomatic to me,
and maybe does what you want (or can be adapted)?

fitting.fn <-function(a,b,x) {
    a + x*b
}

data.x <- 1:50
data.y <- pi*data.x + rnorm(50,sd=20)
plot(data.x,data.y)
nls(y~fitting.fn(a,b,x),data=data.frame(x=data.x,y=data.y),
      start=list(a=0,b=0))

  Ben Bolker



More information about the R-help mailing list