I am looking into fitting a so-called double von Bertalanffy function to fish length-at-age data. Attempting to simplify the situation, the model looks like this ... Y ~ f(X; a,b,c) if x < Z Y ~ g(X; a,d,e) if x >= Z where * f and g are non-linear functions (the "traditional" "single" von Bertalanffy growth function), * Y (length) and X (age) are observed variables, * a,b,c,d,e are parameters to be estimated, and * Z is not a parameter but is a constant computed from b,c,d,e. I usually fit the "traditional" "single" model with nls() but am unsure of how to fit this model with the "if" statement. I tried search the archives with "piecewise" and either "nls", "nonlinear", or "regression" but did not find anything that seemed to fit this situation. One thought I had was to do something like this (mostly pseudo-code) ... nls(Y~ifelse(X<Z,1,0)*f(X;a,b,c)+ifelse(X>=Z,1,0)*g(X;a,d,e), ...) but am unsure if this makes sense. If anyone can offer some help I would be very appreciative. Thank you in advance.
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
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