r语言 - 如何将置信区间添加到观星者表中的优势比

  • 本文关键字:添加 r语言 区间 r stargazer
  • 更新时间 :
  • 英文 :


我正在尝试使用 stargazer 创建一个多变量逻辑回归模型表。我想包括优势比及其置信区间,而不是模型系数。

由于这个链接,我想出了如何用优势比替换系数,但对 CI 做同样的事情会产生问题。如果我给stargazer一个像se = *a list of the standard errors or exp(standard errors)*这样的参数,它会使用 OR +/- 1.96 倍于该列表来计算 CI,这是不正确的。

下面是一些示例代码,来自加州大学洛杉矶分校 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)

感谢 Marek 对这个问题的帮助。这是在这个例子中对我有用的代码:

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")
您可以使用

ci.custom 参数stargazer具有自定义置信区间(第一列是下限,第二列是上限)的列表提供信息。在您的示例中,您所要做的就是调用:

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

或者,您可以定义自己的函数来对值进行指数运算,并简单地将此函数应用于系数和/或置信区间(使用参数apply.coefapply.ci):

exponentiate <- function(x) exp(x)
stargazer(mylogit, ci=T, apply.coef=exponentiate, apply.ci=exponentiate, 
single.row = T, type="text")

最新更新