在 texreg 输出中包装自定义注释 [英] Wrapping custom notes in texreg output

查看:22
本文介绍了在 texreg 输出中包装自定义注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to add a fairly long note to the bottom of a table created by texreg; I want this to simply wrap around, but there doesn't seem to be any functionality built into the function for doing so.

取,例如:

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

编译时会给出如下内容:

Which, when compiled, gives something like:

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

The formatting is atrocious; much better would be something like replacing the standard output:

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

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

Which gives output much closer to what I'm looking for:

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

Is there a way to do this programatically?

推荐答案

在 1.37.1 版本(2020 年 5 月发布)中,texreg 引入了 threeparttable 参数,它使用 threeparttable LaTeX 包,它是为此目的而设计的.

With version 1.37.1 (released in May 2020), texreg introduces the threeparttable argument, which uses the threeparttable LaTeX package, which was designed for this purpose.

示例 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)

输出:

egin{table}
egin{center}
egin{threeparttable}
egin{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}
egin{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 开头.也可以有多个项目和/或使用项目符号来格式化多个注释,如列表:

Note that the custom note must start with \item. It is also possible to have multiple items and/or use bullet points to format multiple notes like in a list:

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 包.

The formatting is not perfect as you cannot set the desired width of the table; the note just adjusts to the width of the respective table. But I think it should be less of a problem in a realistic usage scenario where more than one model is displayed at a time and some of the coefficient names are longer than in the example. This solution also supports longtable environments, in which case the threeparttablex package is used instead.

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

Here is an example of how you could make it look nice with two models:


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)

这会产生:

egin{table}
egin{center}
egin{threeparttable}
egin{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}
egin{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}

呈现为:

这篇关于在 texreg 输出中包装自定义注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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