我对在 lm 或 lmer 对象上调用的 summary() 输出中显示的显着性测试结果与在同一对象上调用的 anova() 输出中显示的结果之间的关系感到困惑。具体来说,我不明白(a)为什么对于 df=1 的因素(应该可以比较结果),结果并不总是一致;(b) 为什么 summary() 尊重分配给每个因素的对比权重,但 anova() 没有。
以下是 lm 的示例:
data(iris)
## Apply treatment coding to Species, and fit model
contrasts(iris$Species) <- contr.treatment(length(levels(iris$Species)))
iris.lm.treatment <- lm(Sepal.Length ~ Petal.Length * Species, data=iris)
# check Petal.Length p-value in lm() output
coef(summary(iris.lm.treatment))["Petal.Length","Pr(>|t|)"]
[1] 0.05199902
# check Petal.Length p-value in anova() output
as.data.frame(anova(iris.lm.treatment))["Petal.Length","Pr(>F)"]
[1] 1.244558e-56
## Apply sum coding to Species, and fit model
contrasts(iris$Species) <- contr.sum(length(levels(iris$Species)))/2
iris.lm.sum <- lm(Sepal.Length ~ Petal.Length * Species, data=iris)
# check Petal.Length p-value in lm() output
coef(summary(iris.lm.sum))["Petal.Length","Pr(>|t|)"]
[1] 2.091453e-12
# check Petal.Length p-value in anova() output
as.data.frame(anova(iris.lm.sum))["Petal.Length","Pr(>F)"]
[1] 1.244558e-56
当 Species 的对比编码发生变化时,拟合 lm 中 Petal.Length 的显着性检验会发生变化——这是有道理的,因为该模型评估了每个因子,正交因子保持为零。但是,anova 结果中 Petal.Length 的显着性检验是相同的,并且与任一 lm 的结果都不匹配。
lmer 的行为(通过 lmerTest 包完成显着性测试)在相关方面令人困惑:
library(lmerTest)
data(ham)
## Apply treatment coding to variables, and fit model
contrasts(ham$Information) <- contr.treatment(length(levels(ham$Information)))
contrasts(ham$Product ) <- contr.treatment(length(levels(ham$Product )))
ham.lmer.treatment <- lmer(Informed.liking ~ Information * Product + (1 | Consumer) + (1 | Consumer:Product), data=ham)
# check Information p-value in lmer() output
coef(summary(ham.lmer.treatment))["Information2","Pr(>|t|)"]
[1] 0.4295516
# check Information p-value in anova() output
as.data.frame(anova(ham.lmer.treatment))["Information","Pr(>F)"]
[1] 0.04885354
## Apply sum coding to variables, and fit model
contrasts(ham$Information) <- contr.sum(length(levels(ham$Information)))/2
contrasts(ham$Product ) <- contr.sum(length(levels(ham$Product )))/2
ham.lmer.sum <- lmer(Informed.liking ~ Information * Product + (1 | Consumer) + (1 | Consumer:Product), data=ham)
# check Information p-value in lmer() output
coef(summary(ham.lmer.sum))["Information1","Pr(>|t|)"]
[1] 0.04885354
# check Information p-value in anova() output
as.data.frame(anova(ham.lmer.sum))["Information","Pr(>F)"]
[1] 0.04885354
在这里,变量编码似乎仍然会影响 summary() 的输出中显示的结果,但不会影响 anova() 的输出中显示的结果。但是,两个 anova() 结果都与使用 sum 编码时获得的 lmer() 结果相匹配。
在我看来,在这两种情况下,anova() 都忽略了使用的变量编码并使用其他一些变量编码——在 lmer 的情况下,这似乎是总和编码——来评估重要性。我想知道如何执行使用分配的变量编码的统计测试。至少对于 lmer 来说,我可以使用 matchMD(); 例如,
# test Information significance while respecting contrast weights
contestMD(ham.lmer.treatment, as.numeric(names(fixef(ham.lmer.treatment))=="Information2"))[,"Pr(>F)"]
[1] 0.4295516 # matches output from summary(ham.lmer.treatment)
但是,我不知道如何对 lm 进行等效测试(大概使用 glht,但我不知道正确的函数调用)。所以,我的问题是:
从概念上讲,为什么 anova() 不尊重分配的变量编码?(大概这都是预期的行为,但原因对我来说是不透明的。)
实际上,在 lm 对象上调用 anova() 时使用了哪些变量编码?
如何使用 lm 对象执行我想要的那种显着性测试?(我在上面使用了 df=1 的示例,因为它们可以在模型输出和 anova() 输出之间进行比较,但当然我真正想做的是测试 df>1 的效果。)