[R] piecewise nls?

Derek Ogle DOgle at northland.edu
Fri Apr 16 17:18:11 CEST 2010


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.



More information about the R-help mailing list