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

Wrapping custom notes in texreg output

我想在 texreg 创建的 table 的底部添加一个相当长的注释;我希望它简单地环绕,但函数中似乎没有内置任何功能来这样做。

取例如:

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

编译时,给出如下内容:

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

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

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

有没有办法以编程方式执行此操作?

到目前为止,我已经通过添加 custom.note.wrap 参数重写 texreg 函数并更改:

来想出一个解决方法
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),然后将提供的注释拆分成最多该长度的字符串,并以 space 结尾,最后连接所有内容每个拆分子字符串分成一堆 multicolumn

这不是最优的,因为 texreg(有能力)根据模型名称的长度自动设置 custom.note.wrap 会更好。但是我的原始 LaTeX能力不足,所以我不确定我会怎么做。

最迟在您需要更新 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.}"))

在 1.37.1 版(2020 年 5 月发布)中,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) & .28 \; (0.87)^{***}$ \
dist        & [=11=].17 \; (0.02)^{***}$ \
\hline
R$^2$       & [=11=].65$                 \
Adj. R$^2$  & [=11=].64$                 \
Num. obs.   & $                   \
\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开头。也可以有多个项目 and/or 使用项目符号来格式化多个注释,就像在列表中一样:

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)

格式不完美,因为您无法设置所需的 table 宽度;音符只是调整到各自 table 的宽度。但我认为在一次显示多个模型并且某些系数名称比示例中更长的实际使用场景中,这应该不是什么问题。此解决方案还支持 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) & .28 \; (0.87)^{***}$ & .28 \; (0.87)^{***}$ \
dist        & [=14=].17 \; (0.02)^{***}$ & [=14=].17 \; (0.02)^{***}$ \
\hline
R$^2$       & [=14=].65$                 & [=14=].65$                 \
Adj. R$^2$  & [=14=].64$                 & [=14=].64$                 \
Num. obs.   & $                   & $                   \
\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}

呈现为: