我有一个带有多项式项的回归模型,我正在使用方差分析来测试各项的显着性。我被要求分别报告多项式项的线性项和二次项,但似乎无法在 R 中使用
stats::anova()
或 car::Anova()
来完成此操作。
模型
summary
函数分别报告每个术语的测试统计数据,有没有办法使用方差分析来做到这一点?或者 ANOVA 函数不这样做是否有充分的理由?
可重现的示例:
data(iris)
mod<-lm(Petal.Length ~ Species + poly(Petal.Width,2),data=iris)
Anova(mod) #gives output for "poly(Petal.Width, 2)"
summary(mod) #gives output for "poly(Petal.Width, 2)1" and "poly(Petal.Width, 2)2"
如果我将模型指定为
mod<-lm(Petal.Length ~ Species + Petal.Width + I(Petal.Width^2),data=iris)
,它确实有效,但在阅读之后,似乎 poly()
在大多数情况下更合适,所以如果可能的话,我更愿意使用它。
anova()
和 car::Anova()
将这些多项式项视为相同项(与“物种”相同),因此将结果合并。
最简单的解决方法是创建和比较嵌套模型。这将与
I(.)
语法匹配。
# you may also add the intercept-only model to completely match with I(.)
mod0<-lm(Petal.Length ~ Species,data=iris)
mod1<-lm(Petal.Length ~ Species + poly(Petal.Width,1,raw=T),data=iris)
mod2<-lm(Petal.Length ~ Species + poly(Petal.Width,2,raw=T),data=iris)
anova(mod0,mod1,mod2)
Analysis of Variance Table
Model 1: Petal.Length ~ Species
Model 2: Petal.Length ~ Species + poly(Petal.Width, 1, raw = T)
Model 3: Petal.Length ~ Species + poly(Petal.Width, 2, raw = T)
Res.Df RSS Df Sum of Sq F Pr(>F)
1 147 27.223
2 146 20.833 1 6.3892 45.1372 3.896e-10 ***
3 145 20.525 1 0.3087 2.1811 0.1419
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1