How do I add confidence intervals to odds ratios in stargazer table?
Asked Answered
A

2

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)
Admissible answered 24/10, 2013 at 20:48 Comment(1)
here is data file: stats.idre.ucla.edu/stat/data/binary.csvBun
B
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")
Biegel answered 25/10, 2013 at 1:43 Comment(6)
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?Admissible
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.Biegel
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")Admissible
I wonder of @Admissible 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)).Luminesce
@Luminesce 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?Ageratum
here is data file: stats.idre.ucla.edu/stat/data/binary.csvBun
A
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")
Admissible answered 31/10, 2013 at 8:29 Comment(1)
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. @MarekMcgann
B
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")
Biegel answered 25/10, 2013 at 1:43 Comment(6)
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?Admissible
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.Biegel
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")Admissible
I wonder of @Admissible 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)).Luminesce
@Luminesce 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?Ageratum
here is data file: stats.idre.ucla.edu/stat/data/binary.csvBun

© 2022 - 2024 — McMap. All rights reserved.