6

I am trying to create a table of a multivariable logistic regression model using stargazer. I would like to include odds ratios and their confidence intervals instead of the model coefficients.

I figured out how to replace the coefficients with the odds ratios, thanks to this link but doing the same with the CI creates problems. If I give stargazer an argument like se = *a list of the standard errors or exp(standard errors)* it calculates the CI using the OR +/- 1.96 times that list, which is incorrect.

Here's some sample code, first part from UCLA DAE:

library(stargazer)
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
mydata$rank <- factor(mydata$rank)
mylogit <- glm(admit ~ gre + gpa + rank, data = mydata, family = "binomial")
summary(mylogit) 

# Table with coefficients
stargazer(mylogit, ci = T, single.row = T, type = "text")

# Table with Odds Ratios, but the CI is not right
OR.vector <- exp(mylogit$coef)
stargazer(mylogit, coef = list(OR.vector), ci = T, single.row = T, type = "text")

# Correct CIs
CI.vector <- exp(confint(mylogit))
cbind(OR = OR.vector, CI.vector)
Community
  • 1
  • 1
MC808
  • 178
  • 1
  • 6

2 Answers2

9

Thank you to Marek for the assistance with this question. Here's the code that worked for me in this example:

library(stargazer)
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
mydata$rank <- factor(mydata$rank)
mylogit <- glm(admit ~ gre + gpa + rank, data = mydata, family = "binomial")
summary(mylogit) 

# Table with coefficients
stargazer(mylogit, ci = T, single.row = T, type = "text")

OR.vector <- exp(mylogit$coef)
CI.vector <- exp(confint(mylogit))
p.values <- summary(mylogit)$coefficients[, 4]

# Table with ORs and CIs
stargazer(mylogit, coef = list(OR.vector), ci = T, 
          ci.custom = list(CI.vector), p = list(p.values), 
          single.row = T, type = "text")
MC808
  • 178
  • 1
  • 6
  • This looks to be a solution for one model, but I am working with a table comparing 8 models. Can I manipulate stargazer to produce a table with many models, containing odds ratios and 95% CI of the odds ratios? My first guess, apply.ci=exp produces results where the odds ratio does not fall in the range of the CI output. @Marek – Bzap Apr 19 '21 at 15:47
7

You can use the ci.custom argument to feed stargazer a list with custom confidence intervals (first column is the lower bound, second column is the upper bound). In your example, all you have to do is call:

stargazer(mylogit, coef = list(OR.vector), ci = T, 
ci.custom = list(CI.vector), single.row = T, type = "text")

Alternatively, you can define your own function to exponentiate values and simply apply this function to your coefficients and/or confidence intervals (using arguments apply.coef and apply.ci):

exponentiate <- function(x) exp(x)

stargazer(mylogit, ci=T, apply.coef=exponentiate, apply.ci=exponentiate, 
single.row = T, type="text")
  • 1
    Thanks! I was using an old version of `stargazer` so I missed the `ci.custom` argument. If I might ask one more formatting question--in the output using `ci.custom`, the "stars" are not the same as for the standard output. That is, some "significant" results are not being starred. I looked at the star arguments but they don't seem to address this issue. Is there something I'm missing? – MC808 Oct 26 '13 at 17:31
  • You can use the `p` argument to specify custom p-values. Decisions about statistical significance stars are made based on these values. (In their absence, the significance stars will be calculated from the available - implicitly or explicitly - coefficients and standard errors.) Please note that I have edited my answer to include an alternative way of achieving the same result using `apply.coef` and `apply.ci`. –  Oct 27 '13 at 19:32
  • 1
    That's superb! I added the example code to your answer. I really like calling the `exponentiate` helper but your code does not produce the same CIs for some reason. It produces the same CIs as `stargazer(mylogit, coef = list(OR.vector), ci = T, single.row = T, type = "text")` – MC808 Oct 29 '13 at 04:33
  • 1
    I wonder of @MC808 has solved this problem, but the `apply.ci=exponentiate` argument behaves very weirdly. I can't replicate their result by manually calculate `exp(confint(mylogit))`. – Heisenberg Apr 06 '15 at 20:24
  • @Heisenberg and @Marek, `apply.ci` seems to behave strangely for me as well (whereas `apply.coef` seems to work). Any ideas as to why this might be? – user1231088 Sep 22 '15 at 14:47
  • here is data file: stats.idre.ucla.edu/stat/data/binary.csv – vasili111 Sep 10 '19 at 02:10