我正在尝试使用stargazer
创建一个多变量逻辑回归模型表。我想包括优势比和他们的置信区间而不是模型系数。
我想出了如何用比值比替换系数,这要归功于link,但对CI做同样会产生问题。如果我给stargazer
se = *a list of the standard errors or exp(standard errors)*
之类的参数,它会使用该列表的OR +/- 1.96倍来计算CI,这是不正确的。
以下是一些示例代码,第一部分来自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)
答案 0 :(得分:7)
感谢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")
答案 1 :(得分:6)
您可以使用ci.custom
参数为stargazer
提供自定义置信区间的列表(第一列是下限,第二列是上限)。在您的示例中,您所要做的就是调用:
stargazer(mylogit, coef = list(OR.vector), ci = T,
ci.custom = list(CI.vector), single.row = T, type = "text")
或者,您可以定义自己的函数来取幂值,只需将此函数应用于系数和/或置信区间(使用参数apply.coef
和apply.ci
):
exponentiate <- function(x) exp(x)
stargazer(mylogit, ci=T, apply.coef=exponentiate, apply.ci=exponentiate,
single.row = T, type="text")