2

我对在 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,但我不知道正确的函数调用)。所以,我的问题是:

  1. 从概念上讲,为什么 anova() 不尊重分配的变量编码?(大概这都是预期的行为,但原因对我来说是不透明的。)

  2. 实际上,在 lm 对象上调用 anova() 时使用了哪些变量编码?

  3. 如何使用 lm 对象执行我想要的那种显着性测试?(我在上面使用了 df=1 的示例,因为它们可以在模型输出和 anova() 输出之间进行比较,但当然我真正想做的是测试 df>1 的效果。)

4

1 回答 1

0

我还没有回答我的前两个问题,但在回答第三个问题时,似乎我可以通过创建子模型(每个模型都删除一个因子)并使用 anova 将每个模型与完整模型进行比较来获得我想要的结果( )。对于上面给出的示例(iris.lm.treatment),我可以执行以下操作。(在我的示例中,我首先使用明确的数字预测变量重新拟合模型,否则在使用 anova() 比较模型时会遇到困难。)

# create numeric columns with the same contrast codings as the nominal factor
Species.numeric <- as.data.frame(model.matrix(~ Species, data=iris))

# drop Intercept column
Species.numeric <- Species.numeric[,2:ncol(Species.numeric)]

# rename columns as Species.num1 & Species.num2 and append to iris
names(Species.numeric) <- paste0("Species.num", 1:ncol(Species.numeric))
iris <- cbind(iris, Species.numeric)

# re-fit lm with all numeric predictors
iris.lm.treatment.num <- lm(Sepal.Length ~ Petal.Length * (Species.num1 + Species.num2), data=iris)

# for each factor, create a subset model that has that factor removed
iris.lm.treatment.num.noPetalLength <- update(iris.lm.treatment.num, . ~ . - Petal.Length                              )
iris.lm.treatment.num.noSpecies     <- update(iris.lm.treatment.num, . ~ . - (Species.num1 + Species.num2)             )
iris.lm.treatment.num.noInteraction <- update(iris.lm.treatment.num, . ~ . - Petal.Length:(Species.num1 + Species.num2))

# use anova() to compare each subset model to the full model
anova(iris.lm.treatment.num.noPetalLength, iris.lm.treatment.num)   # p =  .052
anova(iris.lm.treatment.num.noSpecies,     iris.lm.treatment.num)   # p = 7.611e-06
anova(iris.lm.treatment.num.noInteraction, iris.lm.treatment.num)   # p =  .1895

花瓣长度的主要影响产生 0.052 的 p 值,与 iris.lm.treatment 中的结果相匹配。

于 2021-10-25T02:11:47.993 回答