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

时间:2015-06-17 21:42:27

标签: r latex 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")) 创建的表格的底部添加一个相当长的注释;我希望这简单地换行,但似乎没有任何内置于函数中的功能。

采取,例如:

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

在编译时,它会提供如下内容:

simple texreg

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

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

更易消化的包装:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    var entityMethod = typeof(DbModelBuilder).GetMethod("Entity");

    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
    {
      var entityTypes = assembly
        .GetTypes()
        .Where(t =>
          t.GetCustomAttributes(typeof(PersistentAttribute), inherit: true)
          .Any());

      foreach (var type in entityTypes)
      {
        entityMethod.MakeGenericMethod(type)
          .Invoke(modelBuilder, new object[] { });
      }
    }
}

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

preferred output

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

3 个答案:

答案 0 :(得分:3)

在版本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) & $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}

哪个呈现为:

Screenshot of a single model

请注意,自定义注释必须以\\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}

哪个呈现为:

Screenshot of two models

答案 1 :(得分: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.wrap)选择一个最大字符串长度,然后将提供的音符拆分为最多以空格结尾的长度的字符串,最后将所有内容连接成一堆{{ 1}} s与每个拆分子字符串。

这不是最优的,因为multicolumn(有能力)在给定模型名称的长度等的情况下自动设置texreg会更好。但我的原始{{1}缺乏能力,所以我不确定如何做到这一点。

答案 2 :(得分:2)

我可能会指出一个整洁的alternative solution I received,您可能会感兴趣,最迟需要更新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