[R] Exponential fit of form y=exp(a*x) and not of form y=l*e

(Ted Harding) ted.harding at wlandres.net
Sun Oct 23 22:22:56 CEST 2011


On 23-Oct-11 19:03:05, Carl Witthoft wrote:
> You misused nls().  Observe:
> 
> x<- 1:101
> y2 <- 5*exp(x/20) + runif(101)/100 # nls will NOT converge for perfect
> data.
> 
> nls(y2 ~ exp(A*x), start=list(A=.1))
> 
> Nonlinear regression model
>    model:  y2 ~ exp(A * x)
>     data:  parent.frame()
>        A
> 0.06709
>   residual sum-of-squares: 136703
> 
> Number of iterations to convergence: 7
> Achieved convergence tolerance: 2.694e-06
> 
> Which is a lousy fit.  Compare with
> 
> nls(y2~B*exp(A*x), start=list(A=.1,B=.3))
> 
> Nonlinear regression model
>    model:  y2 ~ B * exp(A * x)
>     data:  parent.frame()
>      A     B
> 0.050 5.001
>   residual sum-of-squares: 0.001398
> 
> Number of iterations to convergence: 13
> Achieved convergence tolerance: 5.073e-08
> 
> So either form works, but only one will give you a result
> that fits your original data.
> 
> <quote>
> Henri Mone <henriMone <at> gmail.com> writes:
> 
>  > I want to fit to my data an exponential function with following
>  > functional form:
>  > y=exp(a*x)
>  >
>  > I used the function "nls" but this gives me exponential fits with
>  > following functional form:
>  > y=l*exp(a*x)
>  >
>  > With "l" being an scaling factor. What do I need to change in my R
> code?
>  >
>  > t.dataFitModel=nls(t.dataForFitY ~exp(a*t.dataForFitX),
>  > data=t.dataForFit, start=list(a = 0.01242922), trace=TRUE, algorithm
> =
>  > "plinear")
>    Use an algorithm other than "plinear", I think (admittedly this is 
> not at all clear from ?nls -- you would really have to go to the 
> references to find out).
> -- 
> Sent from my Cray XK6
> "Pendeo-navem mei anguillae plena est."

Of course fitting y2 ~ 1.0*exp(A*x) to datatiplicativel generated by

  y2 <- 5*exp(x/20) + runif(101)/100

will result in a bad fit! Henri's original query stated
that he wanted to fit y ~ exp(A*x), and I presume he had
a reason for not including a multiplicative constant as in
your y2 ~ B*exp(A*x). It may well be that he knows, for
some reason, that, in  B*exp(A*x), B must be 1, though
he was certainly not explicit about this !

Generating the data with 1.0*exp(x/20) and then using nls
in the form nls(y2 ~ exp(A*x) works perfectly:

  x  <- 1:101
  y2 <- 1.0*exp(x/20) + runif(101)/100

  nls(y2 ~ exp(A*x), start=list(A=.1))

  # Nonlinear regression model
  #   model:  y2 ~ exp(A * x) 
  #    data:  parent.frame() 
  #    A 
  # 0.05 
  #  residual sum-of-squares: 0.002608
  #
  # Number of iterations to convergence: 9 
  # Achieved convergence tolerance: 5.962e-08

So I would not say he was "misusing nls", since we do not have
information about his data. Throwing up a counter-eaxample
where the data are deliberately generated so as to be impossible
to fit with the formula he wants to use (for whatever reason)
is not a good argument!

Hoping this helps,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 23-Oct-11                                       Time: 21:22:53
------------------------------ XFMail ------------------------------



More information about the R-help mailing list