Including model specifications in xxtable (anova (...))

I have a bunch of logarithmic models that for our purposes will be glm()objects called mx, my, mz. I want to get a well formatted xtabledeviation analysis, so naturally, I would like to perform xtable(anova(mx, my, mz, test = "Chisq")).

Vanilla Conclusion xtable, however, does not include model specifications. I would like to enable them for all the ANOVA tests that I run, so if there is no parameter, I cannot do this, I probably just have to crack my own solution. But looking through the help page, there seems to be no easy way to include model specifications.

Any thoughts? Alternatives?

If this helps, this was done in 2.9.1 using xxtable 1.5-5.

+5
source share
2 answers

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:

# fake data
x <- 1:10
y <- x+ rnorm(10)

# two models
m1 <- glm(y~x)
m2 <- glm(y~x+I(x^2))
a <- anova(m1, m2)  # anova object to be printed

# get model formulas
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:

# add long formula
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) 

# use a large width
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 - .

+4

, LaTeX, HTML .

# if we presuppose that <b>a</b> is object from @Aniko reply
> class(a)
[1] "anova"      "data.frame"
# after doing a bit of that sapply magic you get
> a
Analysis of Deviance Table

Model 1: y ~ x
Model 2: y ~ x + I(x^2)
               Resid. Df Resid. Dev Df Deviance
y ~ x                  8     15.503            
y ~ x + I(x^2)         7     12.060  1   3.4428

- :

# load xtable library
library(xtable)
# sink output to html file
sink("~/anova_specs.html")  # suppose you're running R on Linux "~/"
print(xtable(a), type = "html")
sink()

, LaTeX, ...

+1

All Articles