'stargazer2 - automatically return odds ratio

I am doing a logistic regression and was recommended the stargazer2 function instead of stargazer function since it would automatically convert log odds to odds ratio.

My R doesn't find this function though. My question now is, does this even exist or is there just a simpler but still tidy way to exponentiate the values other than doing it manually?



Solution 1:[1]

Here's a more concrete example:

You will need to first load the stargazer package

library(stargazer)

As mentioned by @John Garland, stargazer2 is an utility function developed by cimentadaj. You can load it from here: https://github.com/cimentadaj/cimentadaj/blob/master/R/stargazer2.R

Here's the function copy-pasted from the github repo above:

stargazer2 <- function(model, odd.ratio = FALSE, ...) {
  if(!("list" %in% class(model))) model <- list(model)

  if (odd.ratio) {
    coefOR2 <- lapply(model, function(x) exp(stats::coefficients(x)))
    seOR2 <- lapply(model, function(x) exp(stats::coefficients(x)) * summary(x)$coef[, 2])
    p2 <- lapply(model, function(x) summary(x)$coefficients[, 4])
    stargazer::stargazer(model, coef = coefOR2, se = seOR2, p = p2, ...)

  } else {
    stargazer::stargazer(model, ...)
  }
}

Now, let's fit a logit regression model to see how it works. I am building on an example from here: https://stats.oarc.ucla.edu/r/dae/logit-regression/

# load some data 
mydata <- read.csv("https://stats.idre.ucla.edu/stat/data/binary.csv")
    
# fit model
mylogit <- glm(admit ~ gre + gpa + factor(rank), data = mydata, family = "binomial")

Stargazer output with odds ratios looks like this (note that the parameter is odd.ratio!)

stargazer2(mylogit, odd.ratio=TRUE, type='text')

=============================================
                      Dependent variable:
                  ---------------------------
                             admit
---------------------------------------------
gre                         1.002**
                            (0.001)

gpa                         2.235**
                            (0.741)

factor(rank)2               0.509**
                            (0.161)

factor(rank)3              0.262***
                            (0.090)

factor(rank)4              0.212***
                            (0.089)

Constant                   0.019***
                            (0.021)

---------------------------------------------
Observations                  400
Log Likelihood             -229.259
Akaike Inf. Crit.           470.517
=============================================
Note:             *p<0.1; **p<0.05; ***p<0.01

I would also like to take the chance to plug the excellent modelsummary package (https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html), which you can install by install.packages(modelsummary).

You could accomplish the same as above by running

modelsummary(mylogit, exponentiate=TRUE)

Solution 2:[2]

stargazer2() appears to be among a number of functions developed at https://rdrr.io/github/cimentadaj/cimentadaj/man/stargazer2.html with source code available there.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Otto Kässi
Solution 2