【发布时间】:2021-09-01 13:10:01
【问题描述】:
我有一个线性模型,重要的是要按特定顺序输入术语,因为我计划使用 I 型方差分析。我希望模型包含前两个主效应及其在第三个主效应之前的相互作用。
但是,当我将其作为公式输入 lm() 时,它仍然会先给出所有三个主要效果的输出,然后是交互。这个可以改吗?
## Example data:
df <- structure(list(x1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,3L, 3L, 3L, 3L, 3L),
.Label = c("A", "B", "C"), class = "factor"),
x2 = c(0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L,
0L, 1L, 0L, 1L, 1L, 0L, 0L),
x3 = c(1L, 3L, 4L, 5L, 2L, 3L, 3L, 4L, 5L, 1L, 3L, 4L, 5L, 6L, 4L, 3L, 4L,
1L, 2L, 2L, 1L, 1L, 3L, 2L),
y = c(49.5, 62.8, 46.8, 57, 59.8, 58.5, 55.5, 56, 62.8, 55.8, 69.5, 55, 62,
48.8, 45.5, 44.2, 52, 51.5, 49.8, 48.8, 57.2, 59, 53.2, 56)),
class = "data.frame", row.names = c(NA, -24L))
## formula using desired order:
mod1 <- lm(y ~ x1 + x2 + x1:x2 + x3 + x1:x3 + x2:x3 + x1:x2:x3, data=df)
## standard formula:
mod2 <- lm(y ~ x1*x2*x3, data=df)
## same order in anova output:
anova(mod1)
anova(mod2)
## test to show coefficient order affects outputs:
## (but can only change main effects around)
anova(mod2 <- lm(y ~ x1*x2*x3, data=df))
anova(mod3 <- lm(y ~ x1*x3*x2, data=df))
【问题讨论】:
-
你能展示你正在运行的依赖于某个系数阶数的代码吗?
-
感谢@MrFlick - 与示例不同,我的设计是不平衡的,并且在不同级别的因素中有不同数量的观察。所以根据这些帖子,(我认为)顺序对
anova(mod)很重要。抱歉,我只是为了方便而放了 npk 数据,因为我认为我只需要语法,但如果需要可以尝试创建一个不平衡的示例? -
好吧,我捏造了一个不平衡的例子 :-)
标签: r regression lm