什么时候我应该使用aov()和anova()?

时间:2016-11-25 23:46:56

标签: r anova

我已经提到了很多在线文献,但它增加了我的困惑。大部分讨论过于技术化,术语设计不平衡,I,II或III因子ANOVA和一切。

我只知道aov()在内部使用lm(),对于有要素的数据非常有用。而anova()可用于同一数据集上的不同模型。 我的理解是否正确?

1 个答案:

答案 0 :(得分:12)

anovaaov大不相同。为什么不阅读R&C的文档?aov?anova?简而言之:

  • aov适合模型(正如您已经知道的那样,内部调用lm),因此它会生成回归系数,拟合值,残差等;它产生了一个初级阶段的对象" aov"而且还是一个中学课程" lm"。所以,它是一个" lm"对象。
  • anova是一个通用函数。在您的方案中,您指的是anova.lmanova.lmlist(有关详细信息,请参阅?anova.lm)。前者分析拟合模型(由lmaov生成),而后者分析几个嵌套(越来越大)拟合模型(lmaov)。它们都旨在产生I型(顺序)ANOVA表。

实际上,您首先使用lm / aov来拟合模型,然后使用anova来分析结果。没有什么比尝试一个小例子更好:

fit <- aov(sr ~ ., data = LifeCycleSavings)  ## can also use `lm`
z <- anova(fit)

现在,看看他们的结构。 aov返回一个大对象:

str(fit)

#List of 12
# $ coefficients : Named num [1:5] 28.566087 -0.461193 -1.691498 -0.000337 0.409695
#  ..- attr(*, "names")= chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
# $ residuals    : Named num [1:50] 0.864 0.616 2.219 -0.698 3.553 ...
#  ..- attr(*, "names")= chr [1:50] "Australia" "Austria" "Belgium" "Bolivia" ...
# $ effects      : Named num [1:50] -68.38 -14.29 7.3 -3.52 -7.94 ...
#  ..- attr(*, "names")= chr [1:50] "(Intercept)" "pop15" "pop75" "dpi" ...
# $ rank         : int 5
# $ fitted.values: Named num [1:50] 10.57 11.45 10.95 6.45 9.33 ...
#  ..- attr(*, "names")= chr [1:50] "Australia" "Austria" "Belgium" "Bolivia" ...
# $ assign       : int [1:5] 0 1 2 3 4
# $ qr           :List of 5
#  ..$ qr   : num [1:50, 1:5] -7.071 0.141 0.141 0.141 0.141 ...
#  .. ..- attr(*, "dimnames")=List of 2
#  .. .. ..$ : chr [1:50] "Australia" "Austria" "Belgium" "Bolivia" ...
#  .. .. ..$ : chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
#  .. ..- attr(*, "assign")= int [1:5] 0 1 2 3 4
#  ..$ qraux: num [1:5] 1.14 1.17 1.16 1.15 1.05
#  ..$ pivot: int [1:5] 1 2 3 4 5
#  ..$ tol  : num 1e-07
#  ..$ rank : int 5
#  ..- attr(*, "class")= chr "qr"
# $ df.residual  : int 45
# $ xlevels      : Named list()
# $ call         : language aov(formula = sr ~ ., data = LifeCycleSavings)
# $ terms        :Classes 'terms', 'formula'  language sr ~ pop15 + pop75 + dpi + ddpi
#  .. ..- attr(*, "variables")= language list(sr, pop15, pop75, dpi, ddpi)
#  .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
#  .. .. ..- attr(*, "dimnames")=List of 2
#  .. .. .. ..$ : chr [1:5] "sr" "pop15" "pop75" "dpi" ...
#  .. .. .. ..$ : chr [1:4] "pop15" "pop75" "dpi" "ddpi"
#  .. ..- attr(*, "term.labels")= chr [1:4] "pop15" "pop75" "dpi" "ddpi"
#  .. ..- attr(*, "order")= int [1:4] 1 1 1 1
#  .. ..- attr(*, "intercept")= int 1
#  .. ..- attr(*, "response")= int 1
#  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#  .. ..- attr(*, "predvars")= language list(sr, pop15, pop75, dpi, ddpi)
#  .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "numeric" "numeric" "numeric" ...
#  .. .. ..- attr(*, "names")= chr [1:5] "sr" "pop15" "pop75" "dpi" ...
# $ model        :'data.frame':	50 obs. of  5 variables:
#  ..$ sr   : num [1:50] 11.43 12.07 13.17 5.75 12.88 ...
#  ..$ pop15: num [1:50] 29.4 23.3 23.8 41.9 42.2 ...
#  ..$ pop75: num [1:50] 2.87 4.41 4.43 1.67 0.83 2.85 1.34 0.67 1.06 1.14 ...
#  ..$ dpi  : num [1:50] 2330 1508 2108 189 728 ...
#  ..$ ddpi : num [1:50] 2.87 3.93 3.82 0.22 4.56 2.43 2.67 6.51 3.08 2.8 ...
#  ..- attr(*, "terms")=Classes 'terms', 'formula'  language sr ~ pop15 + pop75 + dpi + ddpi
#  .. .. ..- attr(*, "variables")= language list(sr, pop15, pop75, dpi, ddpi)
#  .. .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
#  .. .. .. ..- attr(*, "dimnames")=List of 2
#  .. .. .. .. ..$ : chr [1:5] "sr" "pop15" "pop75" "dpi" ...
#  .. .. .. .. ..$ : chr [1:4] "pop15" "pop75" "dpi" "ddpi"
#  .. .. ..- attr(*, "term.labels")= chr [1:4] "pop15" "pop75" "dpi" "ddpi"
#  .. .. ..- attr(*, "order")= int [1:4] 1 1 1 1
#  .. .. ..- attr(*, "intercept")= int 1
#  .. .. ..- attr(*, "response")= int 1
#  .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#  .. .. ..- attr(*, "predvars")= language list(sr, pop15, pop75, dpi, ddpi)
#  .. .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "numeric" "numeric" "numeric" ...
#  .. .. .. ..- attr(*, "names")= chr [1:5] "sr" "pop15" "pop75" "dpi" ...
# - attr(*, "class")= chr [1:2] "aov" "lm"

anova返回时:

str(z)

#Classes ‘anova’ and 'data.frame':  5 obs. of  5 variables:
# $ Df     : int  1 1 1 1 45
# $ Sum Sq : num  204.1 53.3 12.4 63.1 650.7
# $ Mean Sq: num  204.1 53.3 12.4 63.1 14.5
# $ F value: num  14.116 3.689 0.858 4.36 NA
# $ Pr(>F) : num  0.000492 0.061125 0.359355 0.042471 NA
# - attr(*, "heading")= chr  "Analysis of Variance Table\n" "Response: sr"