Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping custom notes in texreg output

Tags:

r

latex

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.

Take, e.g.:

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 complied TeX output of the regression; the footnote is all on one line, making the single-column result very wide and introducing lots of unnecessary white space

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

With more digestible wrapping:

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

Now, the footnote is wrapped -- the width of the footnote is dictated by the overall width of the table, rather than vice versa

Is there a way to do this programatically?

like image 254
MichaelChirico Avatar asked Jun 17 '15 21:06

MichaelChirico


1 Answers

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.

Example R code:

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)

Output:

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

Which is rendered as:

Screenshot of a single model

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)

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)

This yields:

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

Which renders as:

Screenshot of two models

like image 88
Philip Leifeld Avatar answered Sep 28 '22 03:09

Philip Leifeld