Short version: How to build separate models for each category (without splitting the data). (I am new to R)
Long version: consider the following synthetic data
housetype,ht1,ht2,age,price
O,0,1,1,1000
O,0,1,2,2000
O,0,1,3,3000
N,1,0,1,10000
N,1,0,2,20000
N,1,0,3,30000
We can model the above using two separate models
if(housetype=='o')
price = 1000 * age
else
price = 10000 * age
i.e. a separate model based on category type?
This is what I have tried
model=lm(price~housetype+age, data=datavar)
and
model=lm(price~ht1+ht2+age, data = datavar)
Both the above models (which is essentially the same) does not produce the result I seek.
Any help is appreciated
Use interaction. Let age
be a numeric variable and housetype
be a factor variable, consider the following:
Same slope different intercept:
price ~ housetype + age
Same intercept different slope
price ~ housetype:age
Different intercept different slope
price ~ housetype * age
来源:https://stackoverflow.com/questions/43189497/r-build-separate-models-for-each-category