Re: [R] nls problem with R
I am sorry,Andrew,I don't get you. Please forgive my poor English. -- View this message in context: http://r.789695.n4.nabble.com/nls-problem-with-R-tp3494454p3508131.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] nls problem with R
Thanks Mike. Your suggestion is really helpful.I did with the your instruction , it really works out. What's more,can you use this package http://cran.r-project.org/web/packages/minpack.lm/index.html it use Levenberg-Marquardt algorithm. Can this package do with four parameters? Thanks again -- View this message in context: http://r.789695.n4.nabble.com/nls-problem-with-R-tp3494454p3508126.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] nls problem with R
> Date: Thu, 5 May 2011 01:20:33 -0700 > From: sterles...@hotmail.com > To: r-help@r-project.org > Subject: Re: [R] nls problem with R > > ID1 ID2 t V(t) > 1 1 0 6.053078443 > 2 1 0.3403 5.56937391 > 3 1 0.4181 5.45484486 > 4 1 0.4986 5.193124598 > 5 1 0.7451 4.31386722 > 6 1 1.0069 3.645422269 > 7 1 1.5535 3.587710965 > 8 1 1.8049 3.740362689 > 9 1 2.4979 3.699837726 > 10 1 6.4903 2.908485019 > 11 1 13.5049 1.888179494 > 12 1 27.5049 1.176091259 > 13 1 41.5049 1.176091259 > > The model > (1) V(t)=V0[1-epi+ epi*exp(-c(t-t0))] A=Vo, B-Vo*epi, C=exp(-c*t0) V(t)=A-B+B*C*exp(-ct) or further, D=A-B, F=B*C, V(t)=D+F*exp(-ct) this model only really has 3 attriubtes: initial value, final value, and decay constant yet you ask for 4 parameters. There is no way to get a unique answer. For some reason this same form comes up a lot here, I think this is about third time I've sene this in last few weeks. I guess when fishing or shopping for forms to fit, it is tempting to throw a bunch of parameteres into your model but this can create intractable ambiguities. Indeed, if I just remove t0 and use your first 8 points I get this ( random starting values, but convewrged easily you still need to plot etc) [1] "1 v= 8.77181162126362 epi= 0.672516376478598 cl= 1.90973175223917 t0= 0 .643481321167201" > summary(nls2) Formula: V2 ~ v0 * (1 - epi + epi * exp(-cl * (T2))) Parameters: Estimate Std. Error t value Pr(>|t|) v0 6.2901 0.3384 18.585 8.3e-06 *** epi 0.5430 0.1373 3.955 0.0108 * cl 0.9684 0.5491 1.763 0.1381 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 0.3579 on 5 degrees of freedom Number of iterations to convergence: 11 Achieved convergence tolerance: 4.057e-06 > (2) V(t)=V0{A*exp[-lambda1(t-t0)]+(1-A)*exp[-lambda2(t-t0)]} > > in formula (2) lambda1=0.5*{(c+delta)+[(c-delta)^2+4*(1-epi)*c*delta]^0.5} > > lambda2=0.5*{(c+delta)-[(c-delta)^2+4*(1-epi)*c*delta]^0.5} > A=(epi*c-lambda2)/(lambda1-lambda2) > > The regression rule : > for formula (1):(t<=2,that is) first 8 rows are used for non-linear > regression > epi,c,t0,V0 parameters are obtained > for formula (2):all 13 rows of results are used for non-linear regression > lambda1,lambda2,A (with these parameters, delta can be calculated from them) > > Thanks for help > Ster Lesser > > -- > View this message in context: > http://r.789695.n4.nabble.com/nls-problem-with-R-tp3494454p3497825.html > Sent from the R help mailing list archive at Nabble.com. > > __ > 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. __ 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.
Re: [R] nls problem with R
Apologies, but I don't see a question here ... am I missing something obvious? Andrew On Thu, May 05, 2011 at 01:20:33AM -0700, sterlesser wrote: > ID1 ID2 t V(t) > 1 1 0 6.053078443 > 2 1 0.3403 5.56937391 > 3 1 0.4181 5.45484486 > 4 1 0.4986 5.193124598 > 5 1 0.7451 4.31386722 > 6 1 1.0069 3.645422269 > 7 1 1.5535 3.587710965 > 8 1 1.8049 3.740362689 > 9 1 2.4979 3.699837726 > 101 6.4903 2.908485019 > 111 13.5049 1.888179494 > 121 27.5049 1.176091259 > 131 41.5049 1.176091259 > > The model > (1) V(t)=V0[1-epi+ epi*exp(-c(t-t0))] > (2) V(t)=V0{A*exp[-lambda1(t-t0)]+(1-A)*exp[-lambda2(t-t0)]} > > in formula (2) lambda1=0.5*{(c+delta)+[(c-delta)^2+4*(1-epi)*c*delta]^0.5} > > lambda2=0.5*{(c+delta)-[(c-delta)^2+4*(1-epi)*c*delta]^0.5} >A=(epi*c-lambda2)/(lambda1-lambda2) > > The regression rule : > for formula (1):(t<=2,that is) first 8 rows are used for non-linear > regression > epi,c,t0,V0 parameters are obtained > for formula (2):all 13 rows of results are used for non-linear regression > lambda1,lambda2,A (with these parameters, delta can be calculated from them) > > Thanks for help > Ster Lesser > > -- > View this message in context: > http://r.789695.n4.nabble.com/nls-problem-with-R-tp3494454p3497825.html > Sent from the R help mailing list archive at Nabble.com. > > __ > 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. -- Andrew Robinson Program Manager, ACERA Department of Mathematics and StatisticsTel: +61-3-8344-6410 University of Melbourne, VIC 3010 Australia (prefer email) http://www.ms.unimelb.edu.au/~andrewpr Fax: +61-3-8344-4599 http://www.acera.unimelb.edu.au/ Forest Analytics with R (Springer, 2011) http://www.ms.unimelb.edu.au/FAwR/ Introduction to Scientific Programming and Simulation using R (CRC, 2009): http://www.ms.unimelb.edu.au/spuRs/ __ 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.
Re: [R] nls problem with R
ID1 ID2 t V(t) 1 1 0 6.053078443 2 1 0.3403 5.56937391 3 1 0.4181 5.45484486 4 1 0.4986 5.193124598 5 1 0.7451 4.31386722 6 1 1.0069 3.645422269 7 1 1.5535 3.587710965 8 1 1.8049 3.740362689 9 1 2.4979 3.699837726 10 1 6.4903 2.908485019 11 1 13.5049 1.888179494 12 1 27.5049 1.176091259 13 1 41.5049 1.176091259 The model (1) V(t)=V0[1-epi+ epi*exp(-c(t-t0))] (2) V(t)=V0{A*exp[-lambda1(t-t0)]+(1-A)*exp[-lambda2(t-t0)]} in formula (2) lambda1=0.5*{(c+delta)+[(c-delta)^2+4*(1-epi)*c*delta]^0.5} lambda2=0.5*{(c+delta)-[(c-delta)^2+4*(1-epi)*c*delta]^0.5} A=(epi*c-lambda2)/(lambda1-lambda2) The regression rule : for formula (1):(t<=2,that is) first 8 rows are used for non-linear regression epi,c,t0,V0 parameters are obtained for formula (2):all 13 rows of results are used for non-linear regression lambda1,lambda2,A (with these parameters, delta can be calculated from them) Thanks for help Ster Lesser -- View this message in context: http://r.789695.n4.nabble.com/nls-problem-with-R-tp3494454p3497825.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] nls problem with R
the dataset's form is changed after my post so I repost it here t 0 0.3403 0.4181 0.4986 0.7451 1.0069 1.5535 1.8049 2.4979 6.4903 13.5049 27.5049 41.5049 V(t) 6.053078443 5.56937391 5.45484486 5.193124598 4.31386722 3.645422269 3.587710965 3.740362689 3.699837726 2.908485019 1.888179494 1.176091259 1.176091259 -- View this message in context: http://r.789695.n4.nabble.com/nls-problem-with-R-tp3494454p3497827.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] nls problem with R
> Date: Wed, 4 May 2011 07:07:44 -0700 > From: sterles...@hotmail.com > To: r-help@r-project.org > Subject: Re: [R] nls problem with R > > Thanks Andrew. > I am sorry for some typos that I omit some numbers of T2. > Based on your suggestion,I think the problem is in the initial values. > And I will read more theory about the non-linear regression. there is unlikely to be any magic involved, unlike getting hotmail to work. As a tool for understanding your data, you should have some idea of the qualitiative properties of model and data and the error function you use to reconcile the two. If you can post your full data set I may post an R example of somethings to try. I was looking for an excuse to play with nls, I'm not expert here, and curious to see what I can do with your example for critique by others. If you want to fully automate this for N contnuous parameters, you can take a shotgun approach but not sure it helps other htna to find gross problems in model or data. I actually wrote a loop to keep picking random parameter values and calculate and SSE between predicted and real data. What you soon find is that this is like trying to decode a good crypto algorithm by guessing- you can do the math to see the problem LOL. > > -- > View this message in context: > http://r.789695.n4.nabble.com/nls-problem-with-R-tp3494454p3495672.html > Sent from the R help mailing list archive at Nabble.com. > > __ > 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. __ 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.
Re: [R] nls problem with R
In addition to the suggestion about finding a good initial value, you should also scale your response V2 (and, of course, V0). Divide V2 by 10^4, for example. Now your V0 should also be scaled by this factor. This would likely help with convergence. Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of sterlesser Sent: Wednesday, May 04, 2011 10:08 AM To: r-help@r-project.org Subject: Re: [R] nls problem with R Thanks Andrew. I am sorry for some typos that I omit some numbers of T2. Based on your suggestion,I think the problem is in the initial values. And I will read more theory about the non-linear regression. -- View this message in context: http://r.789695.n4.nabble.com/nls-problem-with-R-tp3494454p3495672.html Sent from the R help mailing list archive at Nabble.com. __ 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. __ 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.
Re: [R] nls problem with R
Thanks Andrew. I am sorry for some typos that I omit some numbers of T2. Based on your suggestion,I think the problem is in the initial values. And I will read more theory about the non-linear regression. -- View this message in context: http://r.789695.n4.nabble.com/nls-problem-with-R-tp3494454p3495672.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] nls problem with R
Thanks Ruben. Your suggestion about more deeper analysis about the model itself is really helpful. I am trying out some new initial values based on the analysis of the special T2 in the model. -- View this message in context: http://r.789695.n4.nabble.com/nls-problem-with-R-tp3494454p3495663.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] nls problem with R
In addition to Andrew's advice, you should get more familiar with your nonlinear model. >From what you wrote, as T2 tends to infinity, V2 tends to v0*(1-epi). There you have a baseline on the Y-axis towards which your model tends, and this will give you sensible starting values for v0 and epi. Also, as T0 tends to 0, V2 tends to v0(1-epi(1+exp(cl*t0))). There you have another higher point on the Y-axis, and this one will give you additional sensible starting values for cl and t0. Plot the data and the predicted model with your initial values and sends the model-data combination to the optimizer once you see that the predicted line is close to the observed response. V2 <- c(371000, 285000 ,156000, 20600, 4420, 3870, 5500 ) T2 <- c(0.3403 ,0.4181 ,0.4986 ,0.7451 ,1.0069 ,1.553, 1.333) #last value inserted for illustration. #nls2 <- nls(V2~v0*(1-epi+epi*exp(-cl*(T2-t0))),start=list(v0=10^7,epi=0.9 ,cl=6.2,t0=8.7)) v0.ini <- 10^7 epi.ini <- 0.9 cl.ini <- 6.2 t0.ini <- 8.7 V2.pred.ini <- v0.ini*(1-epi.ini+epi.ini*exp(-cl.ini*(T2-t0.ini))) plot(T2,V2) lines(T2,V2.pred.ini) As you can see, with your initial values the line doesn't even show on the plot. No wonder the gradients are singular. So go find better initial values by trial and error and check the results on the plot. Then the optimizer called by nls will finish the job (hopefully). Then you repeat your plot this time with the estimates instead of the initial values. This may get you started in the business of estimating nolinear models. HTH Rubén Dr. Rubén Roa-Ureta AZTI - Tecnalia / Marine Research Unit Txatxarramendi Ugartea z/g 48395 Sukarrieta (Bizkaia) SPAIN > -Mensaje original- > De: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] En nombre de Andrew Robinson > Enviado el: miércoles, 04 de mayo de 2011 9:15 > Para: sterlesser > CC: r-help@r-project.org > Asunto: Re: [R] nls problem with R > > The fact that T2 and V2 are of different lengths seems like a > likely culprit. Other than that, you need to find start > points that do not lead to a singular gradient. There are > several books that provide advice on obtaining initial > parameter estimates for non-linear models. Google Books > might help you. > > Cheers > > Andrew > > > > > On Tue, May 03, 2011 at 09:08:03PM -0700, sterlesser wrote: > > the original data are > > V2 =c(371000,285000 ,156000, 20600, 4420, 3870, 5500 ) T2=c( 0.3403 > > ,0.4181 ,0.4986 ,0.7451 ,1.0069 ,1.553) > > > nls2=nls(V2~v0*(1-epi+epi*exp(-cl*(T2-t0))),start=list(v0=10^7,epi=0.9 > > ,cl=6.2,t0=8.7)) > > after execution error occurs as below > > > > Error in nlsModel(formula, mf, start, wts) : > > singular gradient matrix at initial parameter estimates Error in > > nlsModel(formula, mf, start, wts) : > > singular gradient matrix at initial parameter estimates > In addition: > > Warning messages: > > 1: In lhs - rhs : > > longer object length is not a multiple of shorter object length > > 2: In .swts * attr(rhs, "gradient") : > > longer object length is not a multiple of shorter object length > > > > could anyone help me ?thansks > > > > -- > > View this message in context: > > > http://r.789695.n4.nabble.com/nls-problem-with-R-tp3494454p3494454.htm > > l Sent from the R help mailing list archive at Nabble.com. > > > > __ > > 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. > > -- > Andrew Robinson > Program Manager, ACERA > Department of Mathematics and StatisticsTel: > +61-3-8344-6410 > University of Melbourne, VIC 3010 Australia > (prefer email) > http://www.ms.unimelb.edu.au/~andrewpr Fax: > +61-3-8344-4599 > http://www.acera.unimelb.edu.au/ > > Forest Analytics with R (Springer, 2011) > http://www.ms.unimelb.edu.au/FAwR/ > Introduction to Scientific Programming and Simulation using R > (CRC, 2009): > http://www.ms.unimelb.edu.au/spuRs/ > > __ > 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. > __ 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.
Re: [R] nls problem with R
The fact that T2 and V2 are of different lengths seems like a likely culprit. Other than that, you need to find start points that do not lead to a singular gradient. There are several books that provide advice on obtaining initial parameter estimates for non-linear models. Google Books might help you. Cheers Andrew On Tue, May 03, 2011 at 09:08:03PM -0700, sterlesser wrote: > the original data are > V2 =c(371000,285000 ,156000, 20600, 4420, 3870, 5500 ) > T2=c( 0.3403 ,0.4181 ,0.4986 ,0.7451 ,1.0069 ,1.553) > nls2=nls(V2~v0*(1-epi+epi*exp(-cl*(T2-t0))),start=list(v0=10^7,epi=0.9,cl=6.2,t0=8.7)) > after execution error occurs as below > > Error in nlsModel(formula, mf, start, wts) : > singular gradient matrix at initial parameter estimates > Error in nlsModel(formula, mf, start, wts) : > singular gradient matrix at initial parameter estimates > In addition: Warning messages: > 1: In lhs - rhs : > longer object length is not a multiple of shorter object length > 2: In .swts * attr(rhs, "gradient") : > longer object length is not a multiple of shorter object length > > could anyone help me ?thansks > > -- > View this message in context: > http://r.789695.n4.nabble.com/nls-problem-with-R-tp3494454p3494454.html > Sent from the R help mailing list archive at Nabble.com. > > __ > 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. -- Andrew Robinson Program Manager, ACERA Department of Mathematics and StatisticsTel: +61-3-8344-6410 University of Melbourne, VIC 3010 Australia (prefer email) http://www.ms.unimelb.edu.au/~andrewpr Fax: +61-3-8344-4599 http://www.acera.unimelb.edu.au/ Forest Analytics with R (Springer, 2011) http://www.ms.unimelb.edu.au/FAwR/ Introduction to Scientific Programming and Simulation using R (CRC, 2009): http://www.ms.unimelb.edu.au/spuRs/ __ 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.