[R] Piecewise nls w/ boundary as a fitting parameter

Primoz PETERLIN primozz.peterlin at gmail.com
Wed May 19 14:19:07 CEST 2010


Fitting a piecewise smooth curve to a set of points (and a piecewise
linear function in particular) seems to be a recurring question on
this list. Nevertheless, I was not able to find an answer to a
question that bothers me.

Suppose I have the following data set, and would want to fit it with a
piecewise smooth curve, In this model data, one curve is valid for up
to 3 and another one for onwards. However, suppose I don't have this

x <- 0:8
y <- c(0.5, 0.5, 0.5, 0.5, 1.914214, 2.5, 2.949490, 3.328427, 3.662278)

plot(x, y)

# Neither of these seem to work
f <- function(x, x0, y0, k) { ifelse(x < x0, y0, y0 + sqrt(k*(x-x0))) }
# f <- function(x, x0, y0, k) { (x < x0)*y0 + (x >= x0)*(y0 + sqrt(k*(x-x0))) }

fmodel <- nls(y ~ f(x, x0, y0, k), start = list(x0 = 1, y0 = 1, k = 1))

What's the proper way to deal with such a case? Am I supposed to
somehow obtain the break point separately, and then fit only y0 and k?
What is the preferred method for obtaining break point (I am not even
sure if I am using this term correctly).

Thanks in advance,

Primož Peterlin,   Inštitut za biofiziko, Med. fakulteta, Univerza v Ljubljani
Lipičeva 2, SI-1000 Ljubljana, Slovenija.        primoz.peterlin at mf.uni-lj.si
Tel +386-1-5437612, fax +386-1-4315127,  http://biofiz.mf.uni-lj.si/~peterlin/
F8021D69 OpenPGP fingerprint: CB 6F F1 EE D9 67 E0 2F  0B 59 AF 0D 79 56 19 0F

More information about the R-help mailing list