4

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

enter image description here

user456789
  • 331
  • 1
  • 3
  • 9
  • 1
    You don't have to simulate a regression with fake data. You can use one of the built-in datasets like `mtcars`. This post might help too: [Minimal Reproducible Example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Vincent Jan 24 '21 at 13:14

1 Answers1

1

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.

enter image description here

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()
Carlos Luis Rivera
  • 3,108
  • 18
  • 45