2012-06-11 9 views
3

它是在學術論文描述的估計的一些功能迴歸結果的表有一個行(S)常見的做法模型。例如,您可能有一個行名稱: 「模型包含單個固定效果」,然後每個關聯的單元格將根據情況設置是/否。使用memisc,xtable等添加自定義行(S)迴歸結果的乳膠輸出R表

我的問題是,是否有可能在許多使用R(比如,Tools for making latex tables in R)製作LaTeX表的任何工具中傳遞表格生成函數,例如這樣一行爲了使這更具體,我想象有一個參數,如:

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

我已經通過了memisc mtable和toLaTeX文檔閱讀,並沒有看到任何東西,似乎這個能力---不知道其他的包/方法,但是這似乎是這樣一個共同的使用我懷疑有這樣做的一些方法。

回答

5

您可能會嘗試將該新行直接添加到要傳遞到的表中,例如, 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. 更新您的自定義行rowname:

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

我結束了寫作一些hacky R代碼(注意它只適用於w/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的雙&列間距創建乳膠有效行:

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」,並不想獲得額外的線。