if ais an anova table object, then it attr(a,"heading")contains the information you are looking for, but I could not find a good way to retrieve it. So I looked at the code anova.glmthat directed me to the code anova.lmlistto find out how they put this information in the header. This caused the following solution:
x <- 1:10
y <- x+ rnorm(10)
m1 <- glm(y~x)
m2 <- glm(y~x+I(x^2))
a <- anova(m1, m2)
flas <- sapply(list(m1,m2), function(x)paste(deparse(x$formula)))
rownames(a) <- flas # add formulas as rownames
# convert to latex
xtable(a)
Edit to use long formulas:
If you have long formulas, two changes are necessary: first we need to make sure that it deparsedoes not break it into lines, and then we need to make latex to wrap the formula in the table. The first can be achieved using the cutoff.widthdeparse argument , and the second using the column type p{width}in latex. For example:
m2$formula <- freq ~ sex + attend + birth + politics + sex:attend + sex:birth +
sex:politics + attend:birth + attend:politics + birth:politics +
sex:attend:birth + sex:attend:politics + sex:birth:politics +
attend:birth:politics
a <- anova(m1, m2)
flas <- sapply(list(m1,m2),
function(x)paste(deparse(x$formula, cutoff.width=500)))
rownames(a) <- flas # add formulas as rownames
# convert to latex with first column wrapped in a 5cm wide parbox
xtable(a, align="p{5cm}rrrr")
, . (sex + attend + birth + politics)^3 - .