문제

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!

도움이 되었습니까?

해결책

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

y ~ ifelse(z, x, 0)

다른 팁

You can do this by just fitting the interaction:

fit <- lm( y ~ x:z )

This will multiply x by z so that when z is 0 the value of x will have no influence and when z is one it will just fit x.

Your problem can be tackled in two ways:

a) First create two dummies when z=0 and when z=1 (lets say this is z0 and z1 : with(mydata,ifelse (z==1,z0,z1)) and include both in the model and run the following model without intercept:

lm(y~as.factor(z)+x-1,data=mydata) or lm(y~z0+z1+x-1,data=mydata) #model includes two dummies without intercept to avoid dummy variable trap

y=b0z0+b1z1+b2x

b)Second include only one dummy (z=1) and run the following model with intercept

lm(y~z1+x,data=mydata) #model includes one dummy with intercept

y=intercept+b1z1+b2x #coefficient on z1 gives incremental value over z=0

Expected value of y when z1=0 is intercept+ b2x and expected value of y when z1=1 is intercept+ b1z1+b2x. The difference is b1z1.

Note: This is more related to statistics rather than to programming. So, you will be better of asking these type of questions in CV.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top