Search code examples
rlatexstargazer

Formatting notes in R's stargazer tables


I am using stargazer package to produce (regression output) tables. Everything working miracles until I start editing the notes. First: line breaks are hard, but Bryan suggests a manual solution that is not elegant, but works. Second I need to make it start from the very left of the table.

Here is an example of the notes I am trying to produce from within R. enter image description here

Produced by changing the original LaTeX code from:

\textit{Note:}  & \multicolumn{4}{l}{Logistic regression. Dependent variable: an indicator varible ... AND Some very long and interesting comment.} \\ 

To

   \multicolumn{5}{l} {\parbox[t]{11cm}{ \textit{Notes:} Logistic regression. Dependent variable: an indicator varible ... AND Some very long and interesting comment.}} \\

enter image description here

Editing by hand is time-consuming and error-prone. So I am looking for a way to solve this from R, where I am currently using the following:

stargazer([...],
          style = "qje", notes.append = FALSE, notes.align = "l",
          notes = "\\parbox[t]{7cm}{Logistic regression. Dependent variable: an indicator 
                   varible ... AND Some very long and interesting comment.}")

Solution

  • stargazer returns its output invisibly as a character vector, specifically so you can post-process it. Depending what you want to change, this may involve some regex. Or, as in this case, if you know what your notes line should look like, you can simply replace the wrong line with what you want. Here's a minimal reproducible example:

    df <- data.frame(x = 1:10 + rnorm(100),
                     y = 1:10 + rnorm(100))
    reg <- lm(y ~ x, data = df)
    
    star <- stargazer(reg,
              style = "qje", notes.append = FALSE, notes.align = "l",
              notes = "This will be replaced")
    
    
    note.latex <- "\\multicolumn{5}{l} {\\parbox[t]{11cm}{ \\textit{Notes:} Logistic regression. Dependent variable: an indicator varible ... AND Some very long and interesting comment.}} \\\\"
    star[grepl("Note",star)] <- note.latex
    cat (star, sep = "\n")