Karl Knoblick
2004-Jun-23 16:07 UTC
[R] Fitting function with if-clause (nls; e.g. heaviside)
Hallo! I want to fit a function. The function is e.g.: y = c+m1*x if x<0, c+m2*x if x>=0 where m1, m2 and c is a parameter and x, y are variables of a data frame. I think using nls is appropriate. But I do not know, how to type this formula in nls. Can anybody help? (If there is a possibility to use a Heaviside-function this would be enough.) Karl ___________________________________________________________ Bestellen Sie Y! DSL und erhalten Sie die AVM "FritzBox SL" f??r 0. Sie sparen 119 und bekommen 2 Monate Grundgeb??hrbefreiung.
Prof Brian Ripley
2004-Jun-23 16:20 UTC
[R] Fitting function with if-clause (nls; e.g. heaviside)
On Wed, 23 Jun 2004, Karl Knoblick wrote:> I want to fit a function. The function is e.g.: > y = c+m1*x if x<0, c+m2*x if x>=0 > where m1, m2 and c is a parameter and x, y are > variables of a data frame. > > I think using nls is appropriate. But I do not know, > how to type this formula in nls. Can anybody help?It's a linear model (linear in the params) lm( y ~ I(x*(x>0) + I(x*(x<0))) although I would define some new variables to make that easier to read, e.g. xplus <- x*(x>0) xminus <- x*(x<0) lm(y ~ xplus + xminus) -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595