将自定义注释包装在texreg输出中

7

我试图在由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}}

以下是更接近我所需输出的内容:

现在,脚注已经换行 - 脚注的宽度由表格的总宽度决定,而不是相反

有没有一种程序化的方法来实现这个?

3个回答

4
我可以指出一个不错的备选方案,可能会对你有兴趣,尤其是当你需要更新texreg包时。
因此,在LaTeX代码中,自定义注释以\multicolumn结束,因此我们不能使用像par或\\这样的换行命令。但是我们可以使用\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.}"))

enter image description here


谢谢!我一直想更新我的答案;事实上,parbox 是我现在自己使用的! - MichaelChirico
为了给GOFs(这里是R²)更多的空间,我们可以在{r ...块之前添加\renewcommand\arraystretch{1.3} - jay.sf

4
自2020年5月发布版本1.37.1起,texreg 引入了 threeparttable 参数,该参数使用了为此目的而设计的threeparttable LaTeX包。
示例 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}

这会被渲染成:

两个模型的截图


3

目前我想到了一个解决方法,通过重写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.wrap),然后将提供的注释分成不超过该长度的字符串,以空格结尾,最后将每个拆分的子字符串连接成一堆带有multicolumn的单元格。
这并不是最优的,因为更好的做法是让texreg自动设置custom.note.wrap,给定模型名称的长度等。但我的LaTeX水平有限,所以我不确定如何实现这一点。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接