Search code examples
rregressionindicator

Regression with indicator


I have a dataset with one dependent variable y, and two independent x(continuous) and z(indicator with 0 or 1). The model I would I like to fit is

y = a*1(z==0) + b*x*1(z==1),

in other words, if z==0 then the estimate should simply be the intercept, otherwise the estimate should be the intercept plus the b*x part.

The only thing I have come up with is to do it in 2 steps, ie first take the mean of y for which z==0 (this is the estimate of the intercept), and then subtract this value from the rest of the ys and run a simple regression to estimate the slope.

I am (almost) sure this will work, but ideally I would like to get the estimates in a one-liner in R using lm or something similar. Is there a way to achieve this? Thanks in advace!


Solution

  • You can define a new variable which is 0 if z is 0, and equal to x otherwise:

    y ~ ifelse(z, x, 0)