3

It's common practice for tables of regression outcomes in academic papers to have a row(s) that describe some feature of the estimated model. For example, you might have a row name: "Model included individual fixed effects" and then each associated cell will have a Yes/No as appropriate.

My question is whether it is possible in any of the many tools for making LaTeX tables with R (c.f., Tools for making latex tables in R) to pass the table-generating functions such a row To make this more concrete, I'm imagining having a parameter like:

model.info.row <- list(name = "Fixed effects", values = c("Y", "N", "Y"))

I've read through the memisc mtable and toLaTeX documentation and did not see anything that seems capable of this---not sure about other packages / approaches, but this seems like such a common use case that I suspect there is some way of doing this.

Community
  • 1
  • 1
John Horton
  • 4,122
  • 6
  • 31
  • 45

2 Answers2

5

You might try to add that new line(s) directly to the table you want to pass to e.g. xtable. Really lame example:

  1. Let us have some model:

    m <- lm(mtcars$hp ~ mtcars$wt)
    
  2. Grab the table which is returned in xtable and other helpers:

    df <- as.data.frame(summary(m)$coefficient)
    
  3. Add a new line with some values:

    df[3, ] <- c(sample(c('foo', 'bar'), 4, replace = TRUE))
    
  4. Update the rowname of your custom line:

    rownames(df)[3] <- 'FOOBAR'
    
  5. Check out results:

    > df
                         Estimate       Std. Error             t value            Pr(>|t|)
    (Intercept) -1.82092177119464 32.3246158121787 -0.0563323561763288     0.95545056134944
    mtcars$wt    46.1600502824445 9.62530003926982    4.79569988406785 4.14582744107531e-05
    FOOBAR                    bar              foo                 bar                  bar
    
  6. Or just call xtable:

    > xtable(df)
    % latex table generated in R 2.15.0 by xtable 1.7-0 package
    % Tue Jun 12 01:39:46 2012
    \begin{table}[ht]
    \begin{center}
    \begin{tabular}{rllll}
      \hline
     & Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\ 
      \hline
    (Intercept) & -1.82092177119464 & 32.3246158121787 & -0.0563323561763288 & 0.95545056134944 \\ 
      mtcars\$wt & 46.1600502824445 & 9.62530003926982 & 4.79569988406785 & 4.14582744107531e-05 \\ 
      FOOBAR & bar & foo & bar & bar \\ 
       \hline
    \end{tabular}
    \end{center}
    \end{table}
    
N.N.
  • 8,336
  • 12
  • 54
  • 94
daroczig
  • 28,004
  • 7
  • 90
  • 124
0

I ended up writing some hacky R code (note that it only works on a system w/ sed, wc and awk available) that's more flexible and works well the memisc 'mtable' function, which is my preferred way of generating LaTeX tables. Basically you write your table to a text file, then call this function with (1) the line number in the file where you want to make an insertion (2) the line you want to insert and (3) the name of the file with you want to make the insertions into (note that this function will overwrite your existing file). The code is:

insert.note <-function(linenumber, line, file){
  num.lines <- as.numeric(system(paste("wc", file, "| awk '{print $1}'"), intern=TRUE))
  tmp <- tempfile()
  system(paste("head -n ", linenumber, file, "> ", tmp))
  sink(tmp, append=TRUE)
   cat(line)
   sink()
  system(paste("tail -n", num.lines - linenumber, file, ">>", tmp))
  system(paste("mv", tmp, file))
}

As a helper function, this code creates a valid line of LaTeX using mtable's double & column spacing:

 create.note <- function(l, include.row.end = TRUE){
  n <- length(l)
  s <- ""
  i <- 1
  for(note in l){
    if(i < n){
      cap <- "&&"
    } else {
      if(include.row.end){
        cap <- "\\\\ \n "
      } else {
          cap <- " \n"
      }
    }
    s <- paste(s, note, cap)
    i <- i + 1
  }
  s
}

The include.row.end parameter is in case you want to pass it something like "\midrule" and don't want to get an extra line.

John Horton
  • 4,122
  • 6
  • 31
  • 45