JakeRuss/cheatsheets

Formatting

Closed this issue · 7 comments

Is it possible to produce coefficient +/- SE format when publishing regression model output in stargazer? The parenthesis are non-standard in the journals I am familiar with.

I assume yes, but my guess is it will likely require a substantial customization of the stargazer code and has been a long while since I first wrote this cheat sheet.

If you'll point me to a visual example of exactly what you intend to create (a link or just post an image here), I'll give it a try and report back.

The image seems not to have gone though. Mind adding it on GitHub rather than via email?

Trying again:
image

Let me know if you are able to access that. I copied the link and was able to view it

Thanks for the example image.

It appears that stargazer is too limited for this to work. The package is narrowly focused on a handful of display templates and isn't robust enough to handle significant customization beyond the supported structures. My recommendation would be to explore one of the other alternative table builders, though, I am not sure which one is best practice these days.

For the sake of completeness, I will explain what I thought was possible, below.

If you look in my cheatsheet you'll see that I show a way to replace the standard errors with new ones. My first thought here was that maybe we could manually build a vector of strings in the desired form (coef +/- SE) and then supply this new vector to stargazer's coef argument and similarly replace the coefficients. This doesn't work because stargazer requires the coefs to be numeric. Look at my coef_plus_se object below for the visual and see the error message. This is as much as I have time to try, sorry not to have been more help.

library(dplyr)
library(nycflights13)
library(AER) # Applied Econometrics with R
library(stargazer)

daily <- flights %>%
  filter(origin == "EWR") %>%
  group_by(year, month, day) %>%
  summarise(delay = mean(dep_delay, na.rm = TRUE))

daily_weather <- weather %>%
  filter(origin == "EWR") %>%
  group_by(year, month, day) %>%
  summarise(temp   = mean(temp, na.rm = TRUE),
            wind   = mean(wind_speed, na.rm = TRUE),
            precip = sum(precip, na.rm = TRUE))

# Merge flights with weather data frames
both <- daily %>%
  inner_join(y  = daily_weather, 
             by = c("year", "month", "day")) %>% 
  data.frame()  # Temporary fix

# Create an indicator for quarter
both$quarter <- cut(both$month, breaks = c(0, 3, 6, 9, 12), 
                    labels = c("1", "2", "3", "4"))

# Create a vector of class logical
both$hot <- as.logical(both$temp > 85)

output  <- lm(delay ~ temp + wind + precip, data = both)
output2 <- lm(delay ~ temp + wind + precip + quarter, data = both)

summary(output)

coef_plus_se <- coef(output) %>%
  round(digits = 2) %>%
  paste0(., " +/- ", round(coef(summary(output))[, "Std. Error"], digits = 2))

stargazer(output, output2, type = "html", 
          coef = coef_plus_se, report = "p")