[R] Ifs in formula

Otto Kässi otto.kassi at gmail.com
Fri Mar 11 12:10:01 CET 2011


Dear r-helpers,

This might be an elementary question, but I have a hard time getting
my head around it, so all help is much appreciated.

I am working on a nonlinear regression model of the form

if z > 0
y = f1(x,y),
else
y = f2(x,t) .

In other words, the functional form of f(.) changes according to some
criteria z. Natural approach would be to fit two models, i.e.
model1 <- nlm(y ~ ..., data=data[data$z > 0,]),
model2 <- nlm(y ~ ..., data=data[data$z <= 0,]).
But this is unfeasible since both models contain the same variable x
and I would like to estimate the coefficient on x using both subsets
of the data.

My first shot would be to do something like this:
formula <- ifelse(z > 0, "y ~ f(x,y).", "y ~ f(x,t)")
nlm(formula, data=data)

.. but preliminary testing indicates that this might not work. The
coefficient on t does not get estimated.

Thanks in advance for all helpful suggestions!

With kind regards,
Otto

----
Otto Kassi
University of Helsinki
Dept. of Economics



More information about the R-help mailing list