# [R] Curve Fitting

Gabor Grothendieck ggrothendieck at gmail.com
Sat May 1 00:06:47 CEST 2010

```You can use nls2 to try many starting values. It works just like nls but:

- if you give it a two row data frame as the start value it will
create a grid between the upper and lower values of each parameter and
then run an optimization starting at each such point on the grid
returning the best

- it has all the algorithms that nls has (because it calls nls) plus
an additional algorithm called "brute-force" which simply evaluates
the formula at the start value

Thus you can use it in two ways:

1. perform a brute force run and take the best to get a good starting
value and using that starting value run nls, or
2. run an nls from each starting value

Obviously the second approach takes more run time but has some
advantages in certain cases.  In many cases either approach will work.

> library(nls2)
> # 1. First approach
> fo <- y ~ a * x ^ b + c
> start <- data.frame(a = c(-10, 10), b = c(-10, 10), c = c(-10, 10))
> fm.brute <- nls2(fo, start = start, alg = "brute")
> fm <- nls2(fo, start = fm.brute)
> fm
Nonlinear regression model
model:  y ~ a * x^b + c
data:  <environment>
a      b      c
3.1865 0.5025 7.0900
residual sum-of-squares: 5.677e-05

Number of iterations to convergence: 7
Achieved convergence tolerance: 4.437e-06

> # 2. Second approach
> fm2 <- nls2(fo, start = start, control = nls.control(warnOnly = TRUE))
There were 32 warnings (use warnings() to see them)
> fm2
Nonlinear regression model
model:  y ~ a * x^b + c
data:  <environment>
a      b      c
3.1865 0.5025 7.0900
residual sum-of-squares: 5.677e-05

Number of iterations to convergence: 9
Achieved convergence tolerance: 1.149e-07

On Fri, Apr 30, 2010 at 4:30 PM, Thomas Bschorr <bschorr at phys.ethz.ch> wrote:
> I am having troubles in fitting functions of the form
>
> y~a*x^b+c
>  to data, for example
>
> x<-c(0.1,0.36,0.63,0.90,1.166,1.43, 1.70, 1.96, 2.23)
> y<-c(8.09,9.0,9.62,10.11,10.53,10.9, 11.25, 11.56, 11.86)
>
> I tried for example with nls, which did only work with really good initial guessed values.
> Any suggestion, what I should use?
> Thanks a lot
> Thomas
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

```