Search code examples
ranovagtmass

How to access the "heading" in the list of an Anova output


I want to find a way to take the summary output from anova(model) and put it into a table. I haven't been able to find any ways to do this so I am just doing it manually using gt() and data.frames. My function thus far is this:

anovaPrint<-function(x){
  
  g<-data.table()
  suppressWarnings(g[,':='("Response: depression"=row.names(x),Df=x$Df,'Sum Sq'=x$`Sum Sq`,'Mean Sq'=x$`Mean Sq`,'F value'=x$`F value`,'Pr(>F)'=x$`Pr(>F)`," "=symnum(x$`Pr(>F)`, corr = FALSE, na = FALSE, cutpoints = c(0, 
    0.001, 0.01, 0.05, 0.1, 1), symbols = c("***", "**", "*", ".", " ")))])
  gt(g)%>%
    tab_footnote("Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1")%>%
}

I am attempting to replicate this output and this is what I have so far.

(Target Output)

enter image description here

(Current Output)

enter image description here

R shows that when I save the anova model into a variable there is a heading data field.

> t <- anova(model)
> dput(t)
structure(list(Df = c(1L, 1L, 1L, 1L, 3130L), `Sum Sq` = c(799.229823148598, 
208.833026298425, 327.319201369881, 44.9767447590518, 29756.4490194161
), `Mean Sq` = c(799.229823148598, 208.833026298425, 327.319201369881, 
44.9767447590518, 9.50685272185817), `F value` = c(84.0688129427952, 
21.9665784680008, 34.4298172009448, 4.73098154299174, NA), `Pr(>F)` = c(8.43085789787755e-20, 
2.89282673612949e-06, 4.88082683354118e-09, 0.0296985950527817, 
NA)), row.names = c("diversity", "size", "total", "density", 
"Residuals"), class = c("anova", "data.frame"), heading = c("Analysis of Variance Table\n", 
"Response: depression"))

How would I access the heading field? I am using the gt library and the anova() from the MASS package. I have tried dollar sign notation


Solution

  • attr(t, "heading")

    (see unclass(t) or str(t))