R:为每个类别建立单独的模型 [英] R: build separate models for each category
本文介绍了R:为每个类别建立单独的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
简短版本:如何为每个类别构建单独的模型(不拆分数据). (我是R新手)
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?
这是我尝试过的
model=lm(price~housetype+age, data=datavar)
和
model=lm(price~ht1+ht2+age, data = datavar)
以上两种模型(基本上相同)都无法产生我想要的结果.
Both the above models (which is essentially the same) does not produce the result I seek.
感谢您的帮助
推荐答案
使用交互.假设age
是数字变量,而housetype
是因子变量,请考虑以下因素:
Use interaction. Let age
be a numeric variable and housetype
be a factor variable, consider the following:
相同坡度的不同截距:
price ~ housetype + age
相同的截距不同的斜率
price ~ housetype:age
不同的截距不同的斜率
price ~ housetype * age
这篇关于R:为每个类别建立单独的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文