这是一种答案;据我所知,没有办法直接通过公式来制定这个模型......
构造数据如上:
d <- data.frame(A = rep(c("a1", "a2"), each = 50),
B = c("b1", "b2"), value = rnorm(100))
options(contrasts=c('contr.sum','contr.poly'))
确认最初的发现,仅从公式中减去该因子是行不通的:
m1 <- lm(value ~ A * B, data = d)
coef(m1)
## (Intercept) A1 B1 A1:B1
## -0.23766309 0.04651298 -0.13019317 -0.06421580
m2 <- update(m1, .~. - A)
coef(m2)
## (Intercept) B1 Bb1:A1 Bb2:A1
## -0.23766309 -0.13019317 -0.01770282 0.11072877
制定新的模型矩阵:
X0 <- model.matrix(m1)
## drop Intercept column *and* A from model matrix
X1 <- X0[,!colnames(X0) %in% "A1"]
lm.fit
允许直接指定模型矩阵:
m3 <- lm.fit(x=X1,y=d$value)
coef(m3)
## (Intercept) B1 A1:B1
## -0.2376631 -0.1301932 -0.0642158
此方法仅适用于允许显式指定模型矩阵的少数特殊情况(例如lm.fit
, glm.fit
).
更普遍:
## need to drop intercept column (or use -1 in the formula)
X1 <- X1[,!colnames(X1) %in% "(Intercept)"]
## : will confuse things -- substitute something inert
colnames(X1) <- gsub(":","_int_",colnames(X1))
newf <- reformulate(colnames(X1),response="value")
m4 <- lm(newf,data=data.frame(value=d$value,X1))
coef(m4)
## (Intercept) B1 A1_int_B1
## -0.2376631 -0.1301932 -0.0642158
这种方法的缺点是它不会将多个输入变量识别为源自同一预测变量(即来自超过 2 级因子的多个因子级别)。