3

I use the function Anova() in package car to perform a test with type III. But I have no idea how to extract the residuals or how to get the information of parameter estimates.

Is there some way to do these like residuals(model) and summary.lm(model)?

kath
  • 7,624
  • 17
  • 32
A. Caikov
  • 51
  • 5

2 Answers2

4

The output of Anova is of class anova and data.frame.

So, if we use the extraction with row/column names, it should work. Using a reproducible example from the ?Anova documentation

library(car)
mod <- lm(conformity ~ fcategory*partner.status, data=Moore,
     contrasts=list(fcategory=contr.sum, partner.status=contr.sum))
out <- Anova(mod, type = 3)
str(out)
#Classes ‘anova’ and 'data.frame':  5 obs. of  4 variables:
# $ Sum Sq : num  5753 36 240 175 818
# $ Df     : num  1 2 1 2 39
# $ F value: num  274.359 0.859 11.425 4.185 NA
# $ Pr(>F) : num  3.05e-19 4.31e-01 1.66e-03 2.26e-02 NA
# - attr(*, "heading")= chr  "Anova Table (Type III tests)\n" "Response: conformity"

The print method changes the way how it is printing the output. But, if we just strip off the anova class. The "Residuals" are also in the row names

row.names(out)
#[1] "(Intercept)"              "fcategory"              "partner.status"           
#[4] "fcategory:partner.status" "Residuals"     

So, using the row/column names for extraction

out["Residuals","Sum Sq"]
#[1] 817.764
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    The OP says it's a type III test, so it should be `out <- Anova(mod, type = 3)`. (The row/column extraction is the same.) – Rui Barradas Mar 23 '19 at 12:30
  • @RuiBarradas Thank you for the comment. I updated – akrun Mar 23 '19 at 18:34
  • Sorry. I would like to have a normality test on the residuals, but this just give a single number. Thank you all the same. – A. Caikov Mar 24 '19 at 01:14
  • @A.Caikov Your question is `have no idea how to extract the residuals or how to get the information of parameter estimates.` and it is answering that – akrun Mar 24 '19 at 01:54
  • Yes. But it seems to me that the way gives only one number. The Sum Sq of Residuals. – A. Caikov Mar 26 '19 at 02:25
  • @A.Caikov You can check `out` and extract any of the parameters based on the row/column values – akrun Mar 26 '19 at 05:25
  • I believe the OP's question was about extracting case-wise residuals and parameter estimates for all paramters in the ANOVA models (not the F-values for model tests). These are not in the output and I am still searching for options to uncover them. – jank Jun 25 '21 at 20:51
  • @jank you may need to check the source code `getAnywhere("Anova.III.lm")`. Can get the residual with `df.residual(mod)# [1] 39 > deviance(mod)# [1] 817.764` – akrun Jun 25 '21 at 21:54
0

In my understanding of the question, you are looking for

mod$residuals

Check also

names(mod)

resulting in

[1] "coefficients"  "residuals"     "effects"       "rank"          "fitted.values" "assign"        "qr"  
[8] "df.residual"   "contrasts"     "xlevels"       "call"          "terms"         "model"

You can obtain the list of residuals, the list of coefficients (mod$coefficients) etc. In akrun's example:

 mod$residuals
         1          2          3          4          5          6          7          8          9         10         11 
-0.9000000 -8.6250000 -4.6250000 -1.9000000  1.1000000 -2.9000000  4.7500000 -3.2500000  4.1000000  3.1000000 -3.2500000 
        12         13         14         15         16         17         18         19         20         21         22 
 0.3750000 -1.9000000  1.7500000 -3.6250000 11.3750000 -2.9000000 -5.6250000 10.3750000  0.3750000 -0.9000000  3.1000000 
        23         24         25         26         27         28         29         30         31         32         33 
 7.1428571 -2.2727273  3.6000000 -2.8571429  5.6000000 -4.8571429  2.7272727 -0.2727273 -0.8571429  1.7272727 -2.4000000 
        34         35         36         37         38         39         40         41         42         43         44 
 5.7272727 -6.2727273 -5.4000000  2.1428571 -0.2727273  2.7272727 -7.2727273  2.7272727  1.1428571 -1.4000000 -1.8571429 
        45 
 0.7272727 

and

mod$coefficients
               (Intercept)                 fcategory1                 fcategory2            partner.status1 
                12.0508117                  0.1902597                  1.0991883                  2.4591450 
fcategory1:partner.status1 fcategory2:partner.status1 
                -2.8430736                  1.7908550 
jank
  • 665
  • 6
  • 14