5

Suppose I compared two models of nested random effects using anova(), and the result is below:

new.model: new
current.model: new 
              Df    AIC    BIC  logLik  Chisq Chi Df Pr(>Chisq)    
new.model      8 299196 299259 -149590                             
current.model  9 299083 299154 -149533 115.19      1  < 2.2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

I would like to use only the table part (see below):

              Df    AIC    BIC  logLik  Chisq Chi Df Pr(>Chisq)    
new.model      8 299196 299259 -149590                             
current.model  9 299083 299154 -149533 115.19      1  < 2.2e-16 ***

I know I am able to get rid of the heading part (see blow) by setting the heading to null using attributes(anova.object)$heading = NULL, but I don't know how to get rid of the bottom part: Signif. codes: .....

new.model: new
current.model: new

I crucially do not want to use data.frame (see below) as it changes the blank cells to NAs

data.frame(anova(new.model, current.model))
              Df      AIC      BIC    logLik    Chisq Chi.Df   Pr..Chisq.
new.model      8 299196.4 299258.9 -149590.2       NA     NA           NA
current.model  9 299083.2 299153.6 -149532.6 115.1851      1 7.168247e-27

I wonder if you guys know a way to deal with this situation.

[UPDATE]: I ended up writing a wrapper using print.anova:

anova.print = function(object, signif.stars = TRUE, heading = TRUE){
    if(!heading)
         attributes(object)$heading = NULL
    print.anova(object, signif.stars = signif.stars)
}

Example:

dv = c(rnorm(20), rnorm(20, mean=2), rnorm(20))
iv = factor(rep(letters[1:3], each=20))
anova.object = anova(lm(dv~iv))

Analysis of Variance Table
Response: dv 
          Df Sum Sq Mean Sq F value    Pr(>F)    
iv         2 46.360 23.1798  29.534 1.578e-09 ***
Residuals 57 44.737  0.7849                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 


anova.print(anova.object, F, F)
          Df Sum Sq Mean Sq F value    Pr(>F)
iv         2 46.360 23.1798  29.534 1.578e-09
Residuals 57 44.737  0.7849             
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Alex
  • 4,030
  • 8
  • 40
  • 62

2 Answers2

11

EDIT: anova has a print method with signif.stars as a parameter

anova(new.model, current.model, signif.stars=FALSE)

> x <- anova(lm(hp~mpg+am, data=mtcars))
> print(x, signif.stars=F)
Analysis of Variance Table

Response: hp
          Df Sum Sq Mean Sq F value    Pr(>F)
mpg        1  87791   87791 54.5403 3.888e-08
am         1  11255   11255  6.9924   0.01307
Residuals 29  46680    1610       
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 1
    You can just remove the legend too: `print(x, signif.legend=FALSE)` – Aaron left Stack Overflow May 17 '12 at 16:30
  • Didn't know that. That's better than my response for sure. +1 – Tyler Rinker May 17 '12 at 16:35
  • @Aaron these are really straightforward. I've been trying to simplify a script that I am planning to use to process my data. So these tips are going to be really handy. – Alex May 17 '12 at 16:37
  • 2
    @X.He: You may also find reading the code of `print.anova` and `printCoefmat` helpful; it may be that you'll just want to pull out from those functions just the functionality you need. – Aaron left Stack Overflow May 17 '12 at 16:46
  • It looks to me that the help page for printCoefmat should be recommended as well. – IRTFM May 17 '12 at 16:59
  • 1
    it's too bad that `print.anova` doesn't have a `print.heading` option -- otherwise you could do this all in one command. You could copy the `print.anova` function and add that functionality easily ... I wish there were a `setAttributes()` function like `setNames()`, so you could then do `print(setAttributes(x,heading=NULL),signif.stars=FALSE)` – Ben Bolker May 17 '12 at 17:12
  • @BenBolker Yeah, I ended up writing a wrapper using print.anova, which I put in the main body of my post. – Alex May 18 '12 at 03:43
  • By the way, what's the difference between wrapping `aov()` into `anova()` and into `summary()`? I can tell `anova()` doesn't suppress warnings, but are there other differences? – Waldir Leoncio Apr 24 '14 at 19:05
3

We had a similar post the other day about not showing NAs. You could do:

x <- as.matrix(anova(new.model, current.model))
print(x, na.print="", quote=FALSE)

A more reproducible example using the mtcars data set:

x <- as.matrix(anova(lm(hp~mpg+am, data=mtcars)))
print(x, na.print="", quote=FALSE)
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • Thanks! I actually posted the other question! I apologize for this duplicate post as I thought using the actual ANOVA output would require a different approach. Should've tried the solutions in the other post before posting this one. Thank you again! – Alex May 17 '12 at 16:33
  • X.He look at DWin's solution. It is much better; built in and less work. though I guess this method takes care of the heading and sig stars in one fell swoop. – Tyler Rinker May 17 '12 at 16:36