JakeRuss / cheatsheets

Programming cheatsheets in R markdown
jakeruss.github.io/cheatsheets/
36 stars 23 forks source link

Formatting #7

Closed tcastrosantos closed 7 years ago

tcastrosantos commented 7 years ago

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.

JakeRuss commented 7 years ago

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.

tcastrosantos commented 7 years ago

Cool! Thanks for the quick response. Here's an example:

[image: Inline image 1]

To my eye the coefficients and standard errors are a little squashed together, but you get the gist. value + SE with the p-value underneath.


Theodore Castro-Santos, PhD Research Ecologist USGS-Leetown Science Center S.O. Conte Anadromous Fish Research Center One Migratory Way Turners Falls, MA 01376 413-863-3838 tcastrosantos@usgs.gov

On Tue, Sep 26, 2017 at 11:04 AM, Jake Russ notifications@github.com wrote:

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.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/JakeRuss/cheatsheets/issues/7#issuecomment-332228221, or mute the thread https://github.com/notifications/unsubscribe-auth/AZhv4Z_oVNpVgdo9Ufzn4xQn4LIH4oFKks5smRI4gaJpZM4PkUc0 .

JakeRuss commented 7 years ago

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

tcastrosantos commented 7 years ago

roger!


Theodore Castro-Santos, PhD Research Ecologist USGS-Leetown Science Center S.O. Conte Anadromous Fish Research Center One Migratory Way Turners Falls, MA 01376 413-863-3838 tcastrosantos@usgs.gov

On Tue, Sep 26, 2017 at 4:58 PM, Jake Russ notifications@github.com wrote:

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

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/JakeRuss/cheatsheets/issues/7#issuecomment-332333941, or mute the thread https://github.com/notifications/unsubscribe-auth/AZhv4XoFuHpYHUpuykQMRmJwKhrWSiyEks5smWVvgaJpZM4PkUc0 .

tcastrosantos commented 7 years ago

Trying again: image

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

JakeRuss commented 7 years ago

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")
tcastrosantos commented 7 years ago

Thanks! We'll give it a shot.


Theodore Castro-Santos, PhD Research Ecologist USGS-Leetown Science Center S.O. Conte Anadromous Fish Research Center One Migratory Way Turners Falls, MA 01376 413-863-3838 tcastrosantos@usgs.gov

On Wed, Sep 27, 2017 at 8:26 AM, Jake Russ notifications@github.com wrote:

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_plu_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")

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/JakeRuss/cheatsheets/issues/7#issuecomment-332504270, or mute the thread https://github.com/notifications/unsubscribe-auth/AZhv4ecElSdiS526zwDF9QV1ZPmxijbfks5smj8QgaJpZM4PkUc0 .