Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

r - How do I add confidence intervals to odds ratios in stargazer table?

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...