How to remove "Standard Error" column from xtable() output of an lm on R/RSweave/LaTeX

StackOverflow https://stackoverflow.com/questions/13784344

  •  06-12-2021
  •  | 
  •  

Question

I'm currently doing some data analysis on population data, so reporting the standard errors in the tables of parameter coefficients just doesn't really make statistical sense. I've done a fair bit of searching and can't find any way to customize the xtable output to remove it. Can anyone point me in the right direction?

Thanks a lot, I didn't post this lightly; if it's something obvious, I apologize for having wasted time!

Was it helpful?

Solution

so after my (other) whole long-winded answer... this works too:

xtable(summary(model1)$coefficients[,c(1,3,4)])

Or more generically:

sm <- summary(SomeModel)
SE.indx <- which(colnames(sm$coefficients) == "Std. Error")   # find which column is Std. Error (usually 2nd)
sm$coefficients <- sm$coefficients[, -SE.indx]  # Remove it
xtable(sm$coefficients)   # call xtable on just the coefficients table

Results:

% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Sun Dec  9 00:01:46 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrr}
  \hline
 & Estimate & t value & Pr($>$$|$t$|$) \\ 
  \hline
(Intercept) & 29.80 & 30.70 & 0.00 \\ 
  crim & -0.31 & -6.91 & 0.00 \\ 
  age & -0.09 & -6.50 & 0.00 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

OTHER TIPS

Using the first example in help(lm):

 xtable(as.matrix(coef(lm.D9)))

% latex table generated in R 2.15.2 by xtable 1.7-0 package
% Sat Dec  8 19:53:09 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rr}
  \hline
 & x \\ 
  \hline
(Intercept) & 5.03 \\ 
  groupTrt & -0.37 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

I agreed with not using std erros if this were descriptions of a population and not just a sample. By that reasoning, however, you would not want to leave in p-values or t-statistics. That was the reason I only included the coefficients. To remove the standard error column only from the summary coefficient matrix:

xtable( coef(summary(lm.D9))[,-2] )

% latex table generated in R 2.15.2 by xtable 1.7-0 package
% Sat Dec  8 21:02:17 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrr}
  \hline
 & Estimate & t value & Pr($>$$|$t$|$) \\ 
  \hline
(Intercept) & 5.03 & 22.85 & 0.00 \\ 
  groupTrt & -0.37 & -1.19 & 0.25 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

Looking at str(summary(Model1)) we see that $coefficients has the Std. Error value we want to remove.

lesserSummary <- function(x) {
## returns same as summary(x), but with "Std. Error" remove from coefficients. 
##    and class of object is "modifiedSummary"

  # grab the summary
  sm <- summary(x)

  # find which column is std error
  SE.indx <- which(colnames(sm$coefficients) == "Std. Error")

  # remove it 
  sm$coefficients <- sm$coefficients[, -SE.indx]

  # give it some class
  class(sm) <- "modifiedSummary"

  # return it
  sm
}


xtable.modifiedSummary <- 
function (x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, ...)  {
# x is a modifiedSummary object
# This function is a modification of xtable:::xtable.summary.lm
# Key Difference is simply the number of columns that x$coef is expected to have
#   (Here 3.  Originally 4)  

    x <- data.frame(x$coef, check.names = FALSE)
    class(x) <- c("xtable", "data.frame")
    caption(x) <- caption
    label(x) <- label
    align(x) <- switch(1 + is.null(align), align, c("r", "r", "r", "r"))
    digits(x) <- switch(1 + is.null(digits), digits, c(0, 4, 2, 4))
    display(x) <- switch(1 + is.null(display), display, c("s", "f", "f", "f"))
    return(x)
}


xtable_mod <- function(x) {
  # Wrapper function to xtable.modified summary, calling first lesserSummary on x
  xtable(lesserSummary(x))
}    

EXAMPLE:

xtable_mod(model1)

% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Sat Dec  8 23:44:54 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrr}
  \hline
 & Estimate & t value & Pr($>$$|$t$|$) \\ 
  \hline
(Intercept) & 29.8007 & 30.70 & 0.0000 \\ 
  crim & -0.3118 & -6.91 & 0.0000 \\ 
  age & -0.0896 & -6.50 & 0.0000 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}




Below are the steps taken to arrive at the above conclusion.

You can likely modify the call to xtable, but you first need to follow it down a bit: start by looking at the source for xtable:

xtable
# function (x, caption = NULL, label = NULL, align = NULL, digits = NULL, 
#     display = NULL, ...) 
# {
#     UseMethod("xtable")
# }
# <environment: namespace:xtable>

We see that it simply has a call to UseMethod(). So lets see which methods are available:

methods(xtable)
#  [1] xtable.anova*           xtable.aov*             xtable.aovlist*        
#  [4] xtable.coxph*           xtable.data.frame*      xtable.glm*            
#  [7] xtable.lm*              xtable.matrix*          xtable.prcomp*         
# [10] xtable.summary.aov*     xtable.summary.aovlist* xtable.summary.glm*    
# [13] xtable.summary.lm*      xtable.summary.prcomp*  xtable.table*          
# [16] xtable.ts*              xtable.zoo*      

There are several. Note that the ones with an asterisk * are non-visible.

The method called is determined by the class of the object we are calling xtable on.

Let's say our output is Model1 We take a look at its class: '

class(Model1)
# [1] "lm"

So the source we want to look at is xtable.lm.

xtable.lm
# Error: object 'xtable.lm' not found

Error? That's right, it is non-visible. So we use the package name with triple-colons. Note: please be sure to read the notice in the help file ?":::"

xtable:::xtable.lm
# function (x, caption = NULL, label = NULL, align = NULL, digits = NULL, 
# display = NULL, ...) 
# {
#     return(xtable.summary.lm(summary(x), caption = caption, label = label, 
#         align = align, digits = digits, display = display))
# }
# <environment: namespace:xtable>   

We notice that xtable.lm calls xtable.summary.lm and passes as its first argument a summary(x), where x is our model.

So that leads us to two place to investigate: summary and xtable.summary.lm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top