R: build separate models for each category

女生的网名这么多〃 提交于 2019-12-20 03:49:05

问题


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


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!