[R] Ifs in formula

Otto Kässi otto.kassi at gmail.com
Mon Mar 14 09:53:19 CET 2011


Dennis,
this works perfectly!

Thanks,
Otto

On Fri, Mar 11, 2011 at 2:08 PM, Dennis Murphy <djmuser at gmail.com> wrote:
> Hi:
>
> Perhaps something like
>
> f1(x, y) * I(z > 0) + f2(x, y) * I(z <= 0)      ??
>
> HTH,
> Dennis
>
> On Fri, Mar 11, 2011 at 3:10 AM, Otto Kässi <otto.kassi at gmail.com> wrote:
>>
>> 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
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list