Re: [R] question about non-linear least squares in R
Here is one way of getting a reasonable fit: 1. Scale your y's by dividing all values by 1e6. 2. Plot x vs. y. The plot looks like a quadratic function. 3. Fit a quadratic const. + B*x^2 - this a linear regression problem so use lm. 4. Plot the predictions. 5. Eyball the necessary shift - MA is around 0.01. Refit const. + B*(x-.01)^2. Should get const.=1.147 and B=139.144 6. Use start=list(const.= 1.147, A=0, B=1.147, MA=.01). nls should converge in 4 iterations. In general, good starting points may be crucial to nls convergence. Scaling the y's to reasonable values also helps. Hope this helps, Andy __ Andy Jaworski 518-1-01 Process Laboratory 3M Corporate Research Laboratory - E-mail: [EMAIL PROTECTED] Tel: (651) 733-6092 Fax: (651) 736-3122 "Yu (Warren) Wang" <[EMAIL PROTECTED]> To Sent by: "r-help@stat.math.ethz.ch" [EMAIL PROTECTED] at.math.ethz.chcc Subject 09/05/2007 02:51 [R] question about non-linear least AMsquares in R Hi, everyone, My question is: It's not every time that you can get a converged result from the nls function. Is there any solution for me to get a reasonable result? For example: x <- c(-0.06,-0.04,-0.025,-0.015,-0.005,0.005,0.015,0.025,0.04,0.06) y <- c(1866760,1457870,1314960,1250560,1184850,1144920,1158850,1199910,1263850,1452520) fitOup<- nls(y ~ constant + A*(x-MA)^4 + B*(x-MA)^2, start=list(constant=1000, A=1, B=-100, MA=0), control=nls.control(maxiter=100, minFactor=1/4096), trace=TRUE) For this one, I cannot get the converged result, how can I reach it? To use another funtion or to modify some settings for nls? Thank you very much! Yours, Warren __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] question about non-linear least squares in R
Below is one possibility: If you knew MA you would get a regular linear least-squares for parameters A,B and constant which can be easily solved. So now you can define a function f(MA) which returns that value. Now you must minimize that f - a function of one argument. It can have several local minima and so you must be careful but I believe that minimizing (even "bad") function of one argument should be easier than your original problem. Regards, Moshe. P.S. if you do this I would be interested to know whether this works. --- "Yu (Warren) Wang" <[EMAIL PROTECTED]> wrote: > Hi, everyone, > My question is: It's not every time that you can > get a converged > result from the nls function. Is there any solution > for me to get a > reasonable result? For example: > > x <- > c(-0.06,-0.04,-0.025,-0.015,-0.005,0.005,0.015,0.025,0.04,0.06) > > y <- > c(1866760,1457870,1314960,1250560,1184850,1144920,1158850,1199910,1263850,1452520) > > fitOup<- nls(y ~ constant + A*(x-MA)^4 + B*(x-MA)^2, > > start=list(constant=1000, A=1, > B=-100, MA=0), > control=nls.control(maxiter=100, minFactor=1/4096), > trace=TRUE) > > > > For this one, I cannot get the converged result, > how can I reach it? To > use another funtion or to modify some settings for > nls? > > Thank you very much! > > Yours, > > Warren > > __ > R-help@stat.math.ethz.ch 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. > __ R-help@stat.math.ethz.ch 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.
[R] question about non-linear least squares in R
Hi, everyone, My question is: It's not every time that you can get a converged result from the nls function. Is there any solution for me to get a reasonable result? For example: x <- c(-0.06,-0.04,-0.025,-0.015,-0.005,0.005,0.015,0.025,0.04,0.06) y <- c(1866760,1457870,1314960,1250560,1184850,1144920,1158850,1199910,1263850,1452520) fitOup<- nls(y ~ constant + A*(x-MA)^4 + B*(x-MA)^2, start=list(constant=1000, A=1, B=-100, MA=0), control=nls.control(maxiter=100, minFactor=1/4096), trace=TRUE) For this one, I cannot get the converged result, how can I reach it? To use another funtion or to modify some settings for nls? Thank you very much! Yours, Warren __ R-help@stat.math.ethz.ch 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.