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]]
Thomas, I think the issue of having reasonable starting values is inherent in all nonlinear optimization problems (unless they have some additional properties like convexity, for example). Using a different algorithm may or may not help. In fact, a vast majority of existing algorithms guarantees only local convergence provided that the starting point is within a certain distance from the solution. If you are interested in this particular model, the following procedure seems to give reasonable starting point. 1. Take a guess at c. This is relatively easy since c is the value of y when a=0. For your data a crude guess could be c=8. 2. For a fixed value of c your model can be linearized by the following transformations y1 <- log(y-8) x1 <- log(x) 3. Do summary(lm(y1~x1)) and obtain values for intecept and slope 4. Your starting values for nls are (a=exp(Intercept), b=slope, c=8) Hope this helps, Andy __________________________________ Andy Jaworski 518-1-01 Process Laboratory 3M Corporate Research Laboratory ----- E-mail: apjaworski@mmm.com Tel: (651) 733-6092 Fax: (651) 736-3122 From: Thomas Bschorr <bschorr@phys.ethz.ch> To: r-help@r-project.org Date: 04/30/2010 03:33 PM Subject: [R] Curve Fitting Sent by: <r-help-bounces@r-project.org> 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@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. [[alternative HTML version deleted]]
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) > fmNonlinear 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)> fm2Nonlinear 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. >
Maybe Matching Threads
- Greek letters and formatted text
- text with greek letters
- Non-linear curve fitting (nls): starting point and quality of fit
- How using the weights argument in nls2?
- Need help with self-defined function to perform nonlinear regression and get prediction interval