我已经引用了许多在线文献,但是这使我感到困惑。对于术语不平衡设计以及I,II或III因子ANOVA以及其他所有内容,很多讨论都过于技术性。

我只知道aov()在内部使用lm(),并且对于具有因子的数据很有用。而anova()可用于同一数据集上的不同模型。
我的理解正确吗?

最佳答案

anovaaov实质上不同。为什么不阅读R的文档?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"
    

    关于r - 我应该何时使用aov()和何时anova()?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40823310/

    10-12 22:31