R: build separate models for each category
Asked Answered
T

1

2

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

Teresaterese answered 3/4, 2017 at 16:13 Comment(1)
subset is your friend: lm(price~ht1+ht2+age, data=datavar, subset=(housetype=='O'))Alvaalvan
S
4

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
Suicide answered 3/4, 2017 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.