【问题标题】:Adding custom row(s) to an LaTeX-outputted R table of regression results using memisc, xtable etc.使用 memisc、xtable 等将自定义行添加到 LaTeX 输出的 R 回归结果表中。
【发布时间】:2012-06-11 18:34:26
【问题描述】:

学术论文中的回归结果表通常会有一行描述估计模型的某些特征。例如,您可能有一个行名: “模型包括单独的固定效应”,然后每个关联的单元格将酌情有一个是/否。

我的问题是,是否有可能在使用 R 制作 LaTeX 表格的许多工具中(c.f.,Tools for making latex tables in R)将表格生成函数传递给这样一行为了使这更具体,我想象有像这样的参数:

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

我已经阅读了 memisc mtable 和 toLaTeX 文档,但没有看到任何似乎能够做到这一点的东西——不确定其他包/方法,但这似乎是一个常见的用例,我怀疑有一些这样做的方式。

【问题讨论】:

    标签: r latex


    【解决方案1】:

    您可以尝试将该新行直接添加到您要传递给的表中,例如xtable。真蹩脚的例子:

    1. 让我们有一些模型:

      m <- lm(mtcars$hp ~ mtcars$wt)
      
    2. 获取xtable 和其他助手中返回的表格:

      df <- as.data.frame(summary(m)$coefficient)
      
    3. 添加带有一些值的新行:

      df[3, ] <- c(sample(c('foo', 'bar'), 4, replace = TRUE))
      
    4. 更新自定义行的行名:

      rownames(df)[3] <- 'FOOBAR'
      
    5. 查看结果:

      > 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. 或者直接拨打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}
      

    【讨论】:

      【解决方案2】:

      我最终编写了一些 hacky R 代码(请注意,它仅适用于具有 sed、wc 和 awk 可用的系统),它更灵活,并且在 memisc 'mtable' 函数中运行良好,这是我生成 LaTeX 的首选方式表。基本上,您将表写入文本文件,然后使用 (1) 文件中要插入的行号 (2) 要插入的行和 (3) 文件名调用此函数您想要插入(请注意,此功能将覆盖您现有的文件)。代码是:

      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))
      }
      

      作为一个辅助函数,这段代码使用 mtable 的双列间距创建了一个有效的 LaTeX 行:

       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
      }
      

      include.row.end 参数是为了防止你想传递类似 "\midrule" 的东西并且不想得到额外的行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-20
        • 2013-07-29
        相关资源
        最近更新 更多