When you use R at the command-line, the textual output is limited by the medium: one monospaced font, with no typesetting of any kind. That's great when you're doing exploratory analysis, but what about when you want to include R output in a report or publication? In other words, what if you want to convert this Analysis of Variance table:
Df Sum Sq Mean Sq F value Pr(>F) sex 1 75.4 75.37 0.3793 0.539478 ethnicty 3 2572.1 857.38 4.3147 0.006781 ** sex:ethnicty 2 298.4 149.22 0.7509 0.474767 Residuals 93 18480.0 198.71
to this:
or this:
Df | Sum Sq | Mean Sq | F value | Pr(> F) | |
---|---|---|---|---|---|
sex | 1 | 75.37 | 75.37 | 0.38 | 0.5395 |
ethnicty | 3 | 2572.15 | 857.38 | 4.31 | 0.0068 |
sex:ethnicty | 2 | 298.43 | 149.22 | 0.75 | 0.4748 |
Residuals | 93 | 18480.04 | 198.71 |
With the xtable package, you can. It's a handy tool for converting the output of many of R's statistical functions into a presentation-ready table in LaTeX or HTML format. For example, to create the table above, I simply did the following:
> fm2 <- lm(tlimth ~ sex * ethnicty, data = tli) > print(xtable(anova(fm2)), type="html")
and then pasted the HTML it generated straight into this blog post. I also tweaked the border="1" table directive to border="0" -- if you have a decent Web editor you could pretty the table up further to your heart's desire.
You can find more examples of xtable in action in the package vignette.
xtable package: vignette
The problem with xtable is that it cannot automatically combine and present results from multiple models, which makes it practically impossible to use it in real research. I have been using the package "memisc" for this, which has been working reasonably well.
Posted by: Shige | February 12, 2010 at 13:15
@Shige- my work around for this is to extract the information a I want from a given set of models (usually from the summary object) and arrange them in a data frame. Xtable is very friendly with data frames.
Posted by: Frank | February 12, 2010 at 13:41
You are right, and thanks for the information.
Posted by: Shige | February 12, 2010 at 16:00
Frank,
Can you give a simple example, say two OLS regressions presented side by side in the same table using xtable? I don't remember ever seeing anything like that. Thanks.
Shige
Posted by: Shige | February 14, 2010 at 04:51
It's rather sad that we have to generate Latex or HTML code to make an attractive table. With all of our emphasis on visualization, you'd think we'd have the ability to do attractive and more importantly readable tables within the tool instead of needing a browser or Latex interpreter.
Posted by: Michael Wexler | February 23, 2010 at 09:43
Is there some way to keep the significance level i.e. ** in the ethnicity row? Also, is it possible to keep the significance level key that would also show up at the bottom of the output i.e. Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 ?
Posted by: Julie | June 01, 2010 at 11:32
nice blog!
Posted by: banklawyersblog | November 06, 2012 at 00:08