when creating a table with stargazer, I would like to add a new line befor the degrees of freedom (s. below: before the opening bracket). Could someone help me with the correct call, I couldn't find it in the package documentation. (Apologies for not creating reproducible code, I don't know how to simulate a regression with fake data. I hope someone can still help me!)

As far as I know, there is no built-in functionality to show F-statistics and dfs in distinct lines. You have to hack the output of stargazer() to make a table that you want. A user-defined function in this answer (show_F_in_two_lines()) will produce a table as shown below.

library(stringr)
show_F_in_two_lines <- function(stargazer) {
  # `Stringr` works better than base's regex 
  require(stringr)
  # If you remove `capture.output()`, not only the modified LaTeX code 
  # but also the original code would show up
  stargazer <- stargazer |>
    capture.output()
  # Reuse the index in which F-statistics are displayed
  position_F <- str_which(stargazer, "F Statistic")
  # Extract only F-statistics
  Fs <- stargazer[position_F] |>
    str_replace_all("\\(.*?\\)", "")
  # Extract only df values and make a new line for them
  dfs <- stargazer[position_F] |>
    str_extract_all("\\(.*?\\)") |>
    unlist() |>
    (
      \(dfs)
      paste0(" & ", dfs, collapse = "")
    )() |>
    paste0(" \\\\")
  # Reuse table elements that are specified
  # after the index of F-statistics
  after_Fs <- stargazer[-seq_len(position_F)]
  c(
    stargazer[seq_len(position_F - 1)],
    Fs,
    dfs,
    after_Fs
  ) |>
    cat(sep = "\n")
}
stargazer(
  header = FALSE,
  lm.out.1,
  lm.out.2,
  lm.out.3,
  lm.out.4,
  lm.out.5
) |>
  show_F_in_two_lines()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With