# Re: [R] Ifs in formula

From: Otto Kässi <otto.kassi_at_gmail.com>
Date: Mon, 14 Mar 2011 10:53:19 +0200

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.
>
>

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. Received on Mon 14 Mar 2011 - 08:55:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 14 Mar 2011 - 09:00:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.