[R] specifying a function in nls

Sundar Dorai-Raj sundar.dorai-raj at PDF.COM
Fri Jul 16 17:28:18 CEST 2004



Bill Shipley wrote:
> Hello.  I am trying to understand the syntax of the nonlinear least
> squares function (nls) when the function definition is made outside of
> the call.  Here is the context.
> 
> 1.	If I specify the following command, it works fine:
> 
>  
> 
> 
>>fit2<-nls(
> 
> 
> + A~Am*(1-exp(-alpha*(I-LCP))),data=dat1,
> 
> + start=list(Am=3.6,alpha=0.01,LCP=20))
> 
>  
> 
> 2.	Now, I want to be able to specify the function definition
> outside of nls.  I do the following:
> 
>  
> 
> 
>>Mitscherlich<-function(Am,alpha,I,LCP,...){
> 
> 
> Am*(1-exp(-alpha*(I-LCP)))
> 
> }
> 
>  
> 
> and then:
> 
>  
> 
> 
>>fit3<-nls(
> 
> 
> + A~Mitscherlich,data=dat1,
> 
> + start=list(Am=2.7,alpha=0.006,LCP=45))
> 
>  
> 
> and I get the error message: Error in lhs - rhs : non-numeric argument
> to binary operator
> 
>  
> 
> What am I doing wrong?
> 

Hi Bill,

I think you want:

fit3 <- nls(A ~ Mitscherlich(Am, alpha, I, LCP),
             data = dat1,
             start = list(Am = 2.7, alpha = 0.006, LCP = 45))

Also, I don't see where you supply "I" (which is a bad choice for a 
variable name).

There is an example of this in ?nls.

--sundar




More information about the R-help mailing list