[R] fitting non linear data

Jonathan Baron baron at cattell.psych.upenn.edu
Wed May 22 12:54:47 CEST 2002

On 05/22/02 12:21, gregory lefebvre wrote:
>I would like to trace a curve which fits these two vectors:
> x<-c( 2,3,4,5,6,7,8,10 )
> y<-c( 20, 12, 8, 6, 5, 4.5, 4, 3.8)

The library nls (not included in the base) is one way to fit
nonlinear models.  You need to have some idea of the kind of
function you want to fit.  Without any idea of what these data
are, I thought an exponential function might do the trick, and I
took wild (and very wrong, because I forgot the minus sign for B)
guesses at the starting parameters:

> nls(y~A*exp(B*x),start=list(A=20,B=1))
Nonlinear regression model
  model:  y ~ A * exp(B * x) 
   data:  parent.frame 
         A          B 
35.6929652 -0.3290034 
 residual sum-of-squares:  16.28236 

Warning: In my experience nls does not always converge on a
solution (although it did this time).  Sometimes you need to fool
around with the starting parameters and other parameters, and
sometimes even then it won't work.  An alternative is optim(),
which seems to converge on a solution all the time (in my
experience) but which requires more study before you can use it.

Jonathan Baron, Professor of Psychology, University of Pennsylvania
Home page: http://www.sas.upenn.edu/~baron
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list