在其他讨论或阅读中,我无法找到答案。
carVal
假设我mpg
正在type
使用glm()
. 我已经读过,如果我使用某种算法来选择“最佳”模型特征,那么删除一些因子变量但保留其他变量是不合适的(即,carVal ~ mpg + type1
无效,它必须是carVal ~ mpg + type1 + type2 + type3
)。
我的问题是,如果我在mpg
和之间包含一个交互项type
,是否只对特定级别的mpg
和进行交互type
,但不包括type
交互的所有级别。
例如,这是一个有效的模型:
carVal ~ mpg + type1 + type2 + type3 + type1:mpg
或者,公式是否必须如下:
carVal ~ mpg + type1 + type2 + type3 + type1:mpg + type2:mpg + type3:mpg
这是我在 R 4.0.2 版本中使用的代码示例:
library(leaps)
carVal = c(1000, 15000, 1500, 2000, 2500, 5000, 8000, 9500, 11000)
mpg = c(29, 45, 20, 28, 30, 40, 35, 38, 47)
type = as.factor(c(1, 2, 2, 3, 1, 0, 1, 0, 0))
car.data = data.frame(carVal, mpg, type)
subset.model = regsubsets(x = as.formula('carVal ~ mpg + type + type:mpg'), data = car.data, method = 'exhaustive')
summary(subset.model)