[R] nls() help please

Prof J C Nash (U30A) nashjc at uottawa.ca
Wed May 14 15:02:10 CEST 2014


Someone might be able to come up with a neat expression, but my own
approach would be to write a residual function to create the vector res
from the parameters, for which core line would be

res<-
 D1/(p1*((E/(p2-E))^(1/p3)))+D2/(p6*((E/(p2-E))^(1/p4)))
+(p5*D1*D2)/(p1*p6*((E/(p2-E))^(0.5/p3+0.5/p4)))-1

and use either package nlmrt function nlfb or package minpack.lm
function nls.lm.

Note that if you can provide a Jacobian function (nls does this
internally, which is why it is nice), you will generally get much better
results and more quickly. Writing the residuals this way round helps
avoid sign errors in the derivatives.

You can also minimize the sum of squares -- I've examples of this in my
new book "Nonlinear parameter optimization using R tools" from Wiley.
For information, I'll be giving a tutorial on this topic at UseR 2014. I
believe this year the tutorials are included in conference registration.

Let me know off-list of your progress.

JN



On 14-05-14 06:00 AM, r-help-request at r-project.org wrote:
> Message: 29
> Date: Tue, 13 May 2014 13:20:01 -0700 (PDT)
> From: Andras Farkas <motyocska at yahoo.com>
> To: "r-help at r-project.org" <r-help at r-project.org>
> Subject: [R] nls() help please
> Message-ID:
> 	<1400012401.98480.YahooMailNeo at web161605.mail.bf1.yahoo.com>
> Content-Type: text/plain
> 
> Dear All,
>  
> please help with writing the function for the following:
>  
> we have data frame "raw"
>  
> D1 <-c(2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 50, 50, 50, 50, 50)
> D2 <-c(0.2, 0.5, 1, 2, 5, 0.2, 0.5, 1, 2, 5, 0.2, 0.5, 1, 2, 5, 0.2, 0.5, 1, 2, 5, 0.2, 0.5, 1, 2, 5)
> E <-c(76.3, 48.8, 44.5, 15.5, 3.21, 56.7, 47.5, 26.8, 16.9, 3.25, 46.7, 35.6, 21.5, 11.1, 2.94, 24.8, 21.6, 17.3, 7.78, 1.84, 13.6, 11.1, 6.43, 3.34, 0.89)
>  
> raw <-data.frame(D1,D2,E)
>  
> reasonable starting parameters for nls (to the best of my knowledge):
>  
> start <-c(p1=8,p2=80,p3=-0.7,p4=-2.5,p5=0.3,p6=0.7)
>  
> I would like to fit this model:
>  
> 1 = D1/(p1*((E/(p2-E))^(1/p3)))+D2/(p6*((E/(p2-E))^(1/p4)))+(p5*D1*D2)/(p1*p6*((E/(p2-E))^(0.5/p3+0.5/p4)))
>  
>  
> to the data in "raw" using nls(). The weighting of the fit in this case should be done using the inverse of the variance of raw$E. Having the model equal to 1 makes it quiet difficult for me to see or understand how this can be done (versus the "usual" nls(y~p1*x+p2,...)) using the software.
>  
> As always, your help is greatly appreciated.  
>  
> Sincerely,
>  
> Andras 
> 
> 	[[alternative HTML version deleted]]



More information about the R-help mailing list