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

Mic*_*ico 5 latex r texreg

我正在尝试在由...创建的表的底部添加一个相当长的音符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"))
Run Code Online (Sandbox Code Playgroud)

在编译时,它给出了类似的东西:

简单的texreg

格式是残酷的; 更好的是取代标准输出:

\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}}
Run Code Online (Sandbox Code Playgroud)

更易消化的包装:

\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}}
Run Code Online (Sandbox Code Playgroud)

这使得输出更接近我正在寻找的东西:

首选输出

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

Phi*_*eld 4

在版本 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)
Run Code Online (Sandbox Code Playgroud)

输出:

\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}
Run Code Online (Sandbox Code Playgroud)

其呈现为:

单个模型截图

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

\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}
Run Code Online (Sandbox Code Playgroud)

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

以下是如何使用两个模型使其看起来更漂亮的示例:

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)
Run Code Online (Sandbox Code Playgroud)

这产生:

\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}
Run Code Online (Sandbox Code Playgroud)

其呈现为:

两个模型的截图