[R] piecewise nls?

Derek Ogle DOgle at northland.edu
Sun Apr 18 22:30:07 CEST 2010


Christian,

Thank you for the response and hint.  This is essentially what I ended up doing but I ran into several problems using nls().  I have since switched to optim() and seem to be having better luck.  Again, thank you for your help.

> -----Original Message-----
> From: Christian Ritz [mailto:ritz at life.ku.dk]
> Sent: Saturday, April 17, 2010 2:45 PM
> To: Derek Ogle
> Cc: r-help at r-project.org
> Subject: Re: [R] piecewise nls?
> 
> Hi Derek,
> 
> have a look at the following made-up example:
> 
> f1 <- function(x){2*x}
> f2 <- function(x){-10*x+1}
> 
> x<-rnorm(10)
> x
> (x<0)*f1(x)
> (x>=0)*f2(x)
> (x<0)*f1(x) + (x>=0)*f2(x)
> 
> 
> 
> Therefore I suggest you should specify the model as follows:
> 
> yourNLSmodel <- nls(Y ~ (X<Z) * f(X,a,b,c) + (X>=Z) * g(X,a,d,e), data
> = myData, ...)
> 
> 
> 
> Christian



More information about the R-help mailing list