[R] *** significance with xtable

Marc Schwartz marc_schwartz at comcast.net
Thu Jul 3 20:57:25 CEST 2008


on 07/03/2008 07:41 AM Bunny, lautloscrew.com wrote:
> Hello everybody,
> 
> i used xtable to get some latex output, which worked pretty well with my 
> latex document. But somewhere i missed the part where they explain how 
> to get these nice  significance indicating *** into my latex table. it 
> just stops right after Pr(> |t|)
> 
> thanks in advance
> 
> matthias

I don't believe that either xtable() or Hmisc:::latex() directly support 
such output and in general, you won't see the significance stars 
included in formal model output, though there may be community specific 
exceptions to that.

If you really need to include it, short of writing your own function, 
you would probably need to do something like the following:


Using 'lm.D9' from example(lm):


 > summary(lm.D9)

Call:
lm(formula = weight ~ group)

Residuals:
     Min      1Q  Median      3Q     Max
-1.0710 -0.4938  0.0685  0.2462  1.3690

Coefficients:
             Estimate Std. Error t value Pr(>|t|)
(Intercept)   5.0320     0.2202  22.850 9.55e-15 ***
groupTrt     -0.3710     0.3114  -1.191    0.249
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.6964 on 18 degrees of freedom
Multiple R-squared: 0.07308,	Adjusted R-squared: 0.02158
F-statistic: 1.419 on 1 and 18 DF,  p-value: 0.249



# Get the key model output using capture.output(), since the
# significance stars are generated in printCoefmat() which is called
# from print.summary.lm(). They are not otherwise available in an
# R object.

DF <- read.table(textConnection(capture.output(summary(lm.D9))[11:12]),
                  fill = TRUE)


That gives you:

 > DF
            V1     V2     V3     V4       V5  V6
1 (Intercept)  5.032 0.2202 22.850 9.55e-15 ***
2    groupTrt -0.371 0.3114 -1.191 2.49e-01


Now set the correct column names:

names(DF) <- c(" ", colnames(coef(summary(lm.D9))), " ")


That gives you:

 > DF
               Estimate Std. Error t value Pr(>|t|)
1 (Intercept)    5.032     0.2202  22.850 9.55e-15 ***
2    groupTrt   -0.371     0.3114  -1.191 2.49e-01


Now you can output the dataframe using xtable() or latex():


 > print(xtable(DF), include.rownames = FALSE)
% latex table generated in R 2.7.1 by xtable 1.5-2 package
% Thu Jul  3 13:52:52 2008
\begin{table}[ht]
\begin{center}
\begin{tabular}{lrrrrl}
   \hline
   & Estimate & Std. Error & t value & Pr($>$$|$t$|$) &   \\
   \hline
(Intercept) & 5.03 & 0.22 & 22.85 & 0.00 & *** \\
   groupTrt & $-$0.37 & 0.31 & $-$1.19 & 0.25 &  \\
    \hline
\end{tabular}
\end{center}
\end{table}




 > latex(DF, file = "", rowname = NULL)
% latex.default(DF, file = "", rowname = NULL)
%
\begin{table}[!tbp]
  \begin{center}
  \begin{tabular}{lrrrrl}\hline\hline
\multicolumn{1}{c}{ }&
\multicolumn{1}{c}{Estimate}&
\multicolumn{1}{c}{Std. Error}&
\multicolumn{1}{c}{t value}&
\multicolumn{1}{c}{Pr(>|t|)}&
\multicolumn{1}{c}{ }
\\ \hline
(Intercept)&$ 5.032$&$0.2202$&$22.850$&$9.55e-15$&***\\
groupTrt&$-0.371$&$0.3114$&$-1.191$&$2.49e-01$&\\
\hline
\end{tabular}

\end{center}

\end{table}



See ?capture.output, ?textConnection and ?latex (the latter in the Hmisc 
CRAN package).


HTH,

Marc Schwartz



More information about the R-help mailing list