如何在 rmarkdown 文档中对 Latex 表输出中的回归系数进行小数对齐 [英] How to decimal-align regression coefficients in Latex table output in rmarkdown document

查看:18
本文介绍了如何在 rmarkdown 文档中对 Latex 表输出中的回归系数进行小数对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rmarkdown 文档中,我正在创建一个包含标准误差的回归系数的 Latex 表,以比较单个表中的多个回归模型.我想垂直对齐每个模型的系数,以便系数的小数点垂直排列在一列.

我正在使用 texreg 来创建表格.默认情况下,系数不是小数对齐的(相反,每个字符串都在其列中居中),我正在寻找一种方法来让系数小数对齐.我不喜欢 texreg,所以如果你有一个使用 xtablepanderstargazer 或任何方法的解决方案其他方法,我也会对此感兴趣.理想情况下,我想要一个可以在 rmarkdown 文档中以编程方式实现的解决方案,而不是在将文档呈现为 .tex<之后调整 latex 标记/code> 文件.

作为奖励,我还希望能够在表格标题中添加换行符.例如,在 texreg 中,您可以使用 custom.model.names 参数来设置每个回归模型的列名.在下面的示例中,我希望将 "Add Horsepower and AM" 分成两行,这样列就不需要那么宽了.我试过 "Add Horsepower ewline and AM" 但这只是将ewline"添加到最后的列标题,而 "被忽略了.

这是一个可重现的例子:

---标题:回归表"作者:eipi10"日期:2016 年 8 月 15 日"标头包括:- usepackage{dcolumn}输出:pdf_document---```{r,回声=假,消息=假,结果=asis"}图书馆(texreg)m1 = glm(mpg ~ wt + factor(cyl), data=mtcars)m2 = glm(mpg ~ wt + factor(cyl) + hp + factor(am), data=mtcars)texreg(列表(m1,m2),单行=真,custom.model.names=c("基础模型", "添加马力和 AM"),custom.coef.names=c("截距", "重量","圆柱: 6", "圆柱: 8", "马力","AM: 1"))```

输出表如下所示:

解决方案

这花了很多时间,但我认为它可以让你接近你想要的.我使用了 xtable.主要思想是为每个模型创建两列,一列右对齐(系数),另一列左对齐(标准误差).所以对于一个有两个模型的表,我们有五列.标题和汇总统计信息显示在跨两列的单元格中.

首先,我们有 header.tex,借鉴 p.27 xtable 小插图:

usepackage{array}usepackage{tabularx}
ewcolumntype{L}[1]{>{
aggedrightlet
ewline\arraybackslashhspace{0pt}}m{#1}}
ewcolumntype{C}[1]{>{centeringlet
ewline\arraybackslashhspace{0pt}}m{#1}}
ewcolumntype{R}[1]{>{
aggedleftlet
ewline\arraybackslashhspace{0pt}}m{#1}}
ewcolumntype{P}[1]{>{
aggedright	abularxbackslash}p{#1}}

.Rmd 文件.我从

In an rmarkdown document, I'm creating a Latex table of regression coefficients with standard errors to compare several regression models in a single table. I'd like to vertically align the coefficients for each model so that the decimal points of the coefficients line up vertically down a column.

I'm using texreg to create the table. The coefficients aren't decimal-aligned by default (instead, each string is centered within its column) and I'm looking for a way to get the coefficents decimal-aligned. I'm not wedded to texreg, so if you have a solution using xtable, pander, stargazer or any other method, I'd be interested in that as well. Ideally, I'd like a solution that can be implemented programmatically within the rmarkdown document, rather than tweaking the latex markup after rendering the document into a .tex file.

As a bonus, I'd also like to be able to put line breaks in table headings. For example, in texreg you can use the custom.model.names argument to set the column names for each regression model. In the example below, I'd like to have "Add Horsepower and AM" split into two lines so that the column doesn't need to be so wide. I tried "Add Horsepower ewline and AM" but that just adds "ewline" to the final column header and the " " is ignored.

Here's a reproducible example:

---
title: "Regression Table"
author: "eipi10"
date: "August 15, 2016"
header-includes:
    - usepackage{dcolumn}
output: pdf_document
---

```{r, echo=FALSE, message=FALSE, results="asis"}
library(texreg)

m1 = glm(mpg ~ wt + factor(cyl), data=mtcars)
m2 = glm(mpg ~ wt + factor(cyl) + hp + factor(am), data=mtcars)

texreg(list(m1,m2),
       single.row=TRUE, 
       custom.model.names=c("Base Model", "Add Horsepower and AM"),
       custom.coef.names=c("Intercept", "Weight","Cyl: 6", "Cyl: 8", "Horsepower","AM: 1"))
```

And here's what the output table looks like:

解决方案

This took quite a bit of wrangling, but I think it gets you close to what you want. I used xtable. The main idea is to create two columns for each model, one aligned right (coefficients) and the other aligned left (standard errors). So for a table with two models, we have five columns. Headers and the summary statistics are displayed in cells that span two columns.

First, we have header.tex, drawing on p. 27 of the xtable vignette:

usepackage{array}
usepackage{tabularx}

ewcolumntype{L}[1]{>{
aggedrightlet
ewline\
arraybackslashhspace{0pt}}m{#1}}

ewcolumntype{C}[1]{>{centeringlet
ewline\
arraybackslashhspace{0pt}}m{#1}}

ewcolumntype{R}[1]{>{
aggedleftlet
ewline\
arraybackslashhspace{0pt}}m{#1}}

ewcolumntype{P}[1]{>{
aggedright	abularxbackslash}p{#1}}

The .Rmd file. I learnt about add.to.row from this answer.

---
title: "Regression Table"
author: "eipi10"
date: "August 15, 2016"
header-includes:
    - usepackage{dcolumn}
output: 
  pdf_document:
    includes:
      in_header: header.tex
---

```{r, echo=FALSE, message=FALSE, results="asis"}
library(xtable)
library(broom)   

m1 = glm(mpg ~ wt + factor(cyl), data=mtcars)
m2 = glm(mpg ~ wt + factor(cyl) + hp + factor(am), data=mtcars)

p_val <- c(0, 0.001, 0.01, 0.05, 1)
stars <- sapply(3:0, function(x) paste0(rep("*", x), collapse=""))

make_tbl <- function(model) {
  coefs <- summary(model)$coefficients
  coef_col <- round(coefs[,1], 2)
  se_col <- round(coefs[,2], 2)
  star_col <- stars[findInterval(coefs[,4], p_val)]
  tbl <- data.frame(coef=coef_col)
  tbl$se <- sprintf("(%0.2f)%s", se_col, star_col)
  tbl
}

make_addtorow <- function(row.name, terms) {
  # xtable allows the addition of custom rows. This function
  # makes a row with a one column (which is used for the row
  # names for the model statistics), 
  # followed by two columns that each span two columns.
  paste0(row.name, 
  paste0('& \multicolumn{2}{C{3cm}}{', 
         terms, 
         '}', 
        collapse=''), 
  '\\')
}

tbl1 <- make_tbl(m1)
tbl2 <- make_tbl(m2)
combo <- merge(tbl1, tbl2, by = "row.names", all = TRUE)[,-1]
rownames(combo) <- c("Intercept", "AM: 1", "Cyl: 6", "Cyl: 8", "Horsepower", "Weight")
sum_stats <- round(rbind(glance(m1), glance(m2)), 2)

addtorow <- list()
addtorow$pos <- list(0, 6, 6, 6, 6, 6)
addtorow$command <- c(
  make_addtorow("", c("Base model", "Add Horsepower and AM")),
  make_addtorow("\hline AIC", sum_stats$AIC), # Draw a line after coefficients
  make_addtorow("BIC", sum_stats$BIC),
  make_addtorow("Log Likelihood", sum_stats$logLik),
  make_addtorow("Deviance", sum_stats$deviance),
  make_addtorow("Num. obs.", sum_stats$df.null + 1)
  )

xtbl <- xtable(combo, add.to.row = addtorow, include.colnames = FALSE,  
               comment = FALSE)
# Specify column alignment for tabularx environment
# We're using the custom column types we created in header.tex
# hskip specifies the width between columns
align(xtbl) <- c("L{2.5cm}", "R{1.5cm}@{\hskip 0.1cm}", "L{1.5cm}", 
                           "R{1.5cm}@{\hskip 0.1cm}","L{1.5cm}")

print(xtbl, 
      tabular.environment = "tabularx", # tabularx takes two arguments
      width = ".60\textwidth",         # width, and alignment (specified above)
      add.to.row = addtorow, 
      include.colnames = FALSE,
      comment = FALSE)
```

这篇关于如何在 rmarkdown 文档中对 Latex 表输出中的回归系数进行小数对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆