6

我正在尝试在由texreg;创建的表格底部添加一个相当长的注释。我希望它只是简单地环绕,但似乎没有任何功能内置于这样做的函数中。

举个例子:

texreg(
  lm(speed ~ dist, data = cars),
  custom.note = paste(
    "%stars. This regression should be",
    "intepreted with strong caution as",
    "it is likely plagued by extensive", 
    "omitted variable bias"
  )
)

其中,编译时,会给出如下内容:

回归的编译 TeX 输出; 脚注都在一行上,使单列结果非常宽,并引入了许多不必要的空白

格式很糟糕;更好的是替换标准输出:

\multicolumn{2}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$. This regression should be intepreted with strong caution as it is likely plagued by extensive omitted variable bias}}

使用更易消化的包装:

\multicolumn{2}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$.}} \\
\multicolumn{2}{l}{\scriptsize{This regression should be intepreted with}} \\
\multicolumn{2}{l}{\scriptsize{strong caution as it is likely plagued by}} \\
\multicolumn{2}{l}{\scriptsize{extensive omitted variable bias}}

这使输出更接近我正在寻找的内容:

现在,脚注被换行了——脚注的宽度由表格的整体宽度决定,而不是反之亦然

有没有办法以编程方式做到这一点?

4

3 回答 3

3

我可能会指出我收到的一个简洁的替代解决方案,您可能会感兴趣,最迟在您需要更新 texreg 包时。

因此,自定义注释在\multicolumnLaTeX 代码中以 a 结尾,因此我们不能使用像paror之类的换行命令\\。但是我们可以实现自动换行\parbox。如果我们仍然想要自定义换行符,我们可以使用四个反斜杠\\\\。为了更好的格式化,我们可以\\vspace{2pt}在文本内容的开头使用:

texreg(lm(speed ~ dist, data = cars),
       custom.note = ("\\parbox{.4\\linewidth}{\\vspace{2pt}%stars. \\\\
       This regression should be intepreted with strong caution as it is 
       likely plagued by extensive omitted variable bias.}"))

在此处输入图像描述

于 2017-06-28T08:55:00.040 回答
3

在 1.37.1 版本(2020 年 5 月发布)中,texreg引入了使用为此目的设计threeparttable的 LaTeX 包的论点。threeparttable

示例 R 代码:

texreg(lm(speed ~ dist, data = cars),
       custom.note = paste("\\item %stars. This regression",
                           "should be interpreted with strong",
                           "caution as it is likely plagued by",
                           "extensive omitted variable bias."),
       single.row = TRUE,
       threeparttable = TRUE)

输出:

\begin{table}
\begin{center}
\begin{threeparttable}
\begin{tabular}{l c}
\hline
 & Model 1 \\
\hline
(Intercept) & $8.28 \; (0.87)^{***}$ \\
dist        & $0.17 \; (0.02)^{***}$ \\
\hline
R$^2$       & $0.65$                 \\
Adj. R$^2$  & $0.64$                 \\
Num. obs.   & $50$                   \\
\hline
\end{tabular}
\begin{tablenotes}[flushleft]
\scriptsize{\item $^{***}p<0.001$; $^{**}p<0.01$; $^{*}p<0.05$. This regression should be interpreted with strong caution as it is likely plagued by extensive omitted variable bias}
\end{tablenotes}
\end{threeparttable}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}

呈现为:

单个模型的屏幕截图

请注意,自定义注释必须以\\item. 也可以有多个项目和/或使用项目符号来格式化多个注释,例如在列表中:

texreg(lm(speed ~ dist, data = cars),
       custom.note = paste("\\item[$\\bullet$] %stars.",
                           "\\item[$\\bullet$] This regression",
                           "should be interpreted with strong",
                           "caution as it is likely plagued by",
                           "extensive omitted variable bias."),
       single.row = TRUE,
       threeparttable = TRUE)

格式不完美,因为您无法设置所需的表格宽度;注释只是调整到相应表格的宽度。但我认为在一次显示多个模型并且某些系数名称比示例中长的实际使用场景中,这应该不是问题。此解决方案还支持longtable环境,在这种情况下使用threeparttablex包来代替。

这是一个示例,说明如何使用两个模型使其看起来不错:


fit <- lm(speed ~ dist, data = cars)
texreg(list(fit, fit),
       custom.note = paste("\\item[\\hspace{-5mm}] %stars.",
                           "\\item[\\hspace{-5mm}] This regression",
                           "should be interpreted with strong",
                           "caution as it is likely plagued by",
                           "extensive omitted variable bias."),
       single.row = TRUE,
       threeparttable = TRUE)

这产生:

\begin{table}
\begin{center}
\begin{threeparttable}
\begin{tabular}{l c c}
\hline
 & Model 1 & Model 2 \\
\hline
(Intercept) & $8.28 \; (0.87)^{***}$ & $8.28 \; (0.87)^{***}$ \\
dist        & $0.17 \; (0.02)^{***}$ & $0.17 \; (0.02)^{***}$ \\
\hline
R$^2$       & $0.65$                 & $0.65$                 \\
Adj. R$^2$  & $0.64$                 & $0.64$                 \\
Num. obs.   & $50$                   & $50$                   \\
\hline
\end{tabular}
\begin{tablenotes}[flushleft]
\scriptsize{\item[\hspace{-5mm}] $^{***}p<0.001$; $^{**}p<0.01$; $^{*}p<0.05$. \item[\hspace{-5mm}] This regression should be interpreted with strong caution as it is likely plagued by extensive omitted variable bias.}
\end{tablenotes}
\end{threeparttable}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}

呈现为:

两个模型的屏幕截图

于 2020-06-17T22:33:54.203 回答
2

到目前为止,我texreg通过添加custom.note.wrap参数和更改来重写函数,从而提出了一种解决方法:

note <- paste0("\\multicolumn{", length(models) + 1, 
               "}{l}{\\", notesize, "{", custom.note, "}}")
note <- gsub("%stars", snote, note, perl = TRUE)

到:

if (custom.note.wrap){
  note<-paste(paste0("\\multicolumn{", length(models) + 1L,"}{l}{\\",notesize,"{",
                     strwrap(custom.note, width=custom.note.wrap), "}}"),
              collapse = " \\ \n")
  note <- gsub("%stars", snote, note, perl = TRUE)
}else{
  note <- paste0("\\multicolumn{", length(models) + 1L, 
                 "}{l}{\\", notesize, "{", custom.note, "}}")
  note <- gsub("%stars", snote, note, perl = TRUE)
}

这个想法是为每一行选择一个最大字符串长度custom.note.wrapmulticolumn

这不是最佳选择,因为texreg(能够)custom.note.wrap根据模型名称的长度等自动设置会更好。但是我LaTeX缺乏原始能力,所以我不确定我会如何做到这一点。

于 2015-06-17T22:37:07.713 回答