[R] Using xtable with summaries of lm objects

Martin Maechler maechler at stat.math.ethz.ch
Tue Jun 22 14:49:52 CEST 2004


>>>>> "Peter" == Peter Dunn <dunn at usq.edu.au>
>>>>>     on Tue, 22 Jun 2004 10:02:16 +1000 writes:

    Peter> Hi all
    Peter> Suppose I do the following:

    Peter> set.seed(1000)
    Peter> library(xtable)
    Peter> x <- runif( 10 )
    Peter> y <- 1 + 2*x + rnorm( length(x) )
    Peter> test.lm <- lm( y ~ x )
    Peter> summary( test.lm )
    Peter> xtable ( summary( test.lm ) )

    Peter> The final  xtable  output follows:

    Peter> % latex table generated in R 1.8.1 by xtable 1.2-2 package
    Peter> % Tue Jun 22 09:56:36 2004
    Peter> \begin{table}[ht]
    Peter> \begin{center}
    Peter> \begin{tabular}{rrrrr}
    Peter> \hline
    Peter> & Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\
    Peter> \hline
    Peter>  (Intercept) & 0.5731 & 0.4396 & 1.30 & 0.2286 \\
    Peter>   x & 1.9680 & 0.8894 & 2.21 & 0.0578 \\
    Peter> \hline
    Peter> \end{tabular}
    Peter> \end{center}
    Peter> \end{table}


    Peter> Notice this line:
    Peter> & Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\

    Peter> This will not LaTeX correctly, as teh string `$$' occurs;
    Peter> presumably it should read something like:
    Peter> & Estimate & Std. Error & t value & Pr({$>$}{$|$}t$|$) \\
    Peter> or (better IMHO):
    Peter> & Estimate & Std. Error & $t$ value & Pr($>|t|$) \\

    Peter> I searched the archives, and couldn't find any reference to this
    Peter> bug;
yes, it's a bug.

    Peter>  perhaps its just me!  Is there a known workaround (as I want
    Peter> to auto-generate these table using  Sweave; I know I could
    Peter> cut-and-paste the table, and correct as appropriate).

R and xtable are open source ... ;-)
and the posting guide tells you to ask package authors even
before posting to R-help..
(I've now at least CC'ed xtable's author).

When looking at the xtable source,
you'll realize quickly that  xtable/R/xtable.R
has the ingredients:

 xtable.summary.lm()  is the function that could be improved
 and
 xtable.anova  will probably serve as an example on how to
 treat the "Pr(>|t|)" as special case.

Theoretically better would be to deal with such "P(..)" strings
more ``package globally'', probably in xtable.data.frame() or
xtable.matrix()
{and obviate the need for both xtable.anova() and xtable.summary.lm()'s
 special treatment -- automatically dealing correctly with
 similar classes}.

--
Martin Maechler




More information about the R-help mailing list