这是我的代码,可以按预期工作(最初来自这里(:
library(stargazer)
mydata <- read.csv("https://stats.idre.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")
它给出了正确的输出:
Call:
glm(formula = admit ~ gre + gpa + rank, family = "binomial",
data = mydata)
Deviance Residuals:
Min 1Q Median 3Q Max
-1.6268 -0.8662 -0.6388 1.1490 2.0790
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -3.989979 1.139951 -3.500 0.000465 ***
gre 0.002264 0.001094 2.070 0.038465 *
gpa 0.804038 0.331819 2.423 0.015388 *
rank2 -0.675443 0.316490 -2.134 0.032829 *
rank3 -1.340204 0.345306 -3.881 0.000104 ***
rank4 -1.551464 0.417832 -3.713 0.000205 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 499.98 on 399 degrees of freedom
Residual deviance: 458.52 on 394 degrees of freedom
AIC: 470.52
Number of Fisher Scoring iterations: 4
>
> # Table with coefficients
> stargazer(mylogit, ci = T, single.row = T, type = "text")
=============================================
Dependent variable:
---------------------------
admit
---------------------------------------------
gre 0.002** (0.0001, 0.004)
gpa 0.804** (0.154, 1.454)
rank2 -0.675** (-1.296, -0.055)
rank3 -1.340*** (-2.017, -0.663)
rank4 -1.551*** (-2.370, -0.733)
Constant -3.990*** (-6.224, -1.756)
---------------------------------------------
Observations 400
Log Likelihood -229.259
Akaike Inf. Crit. 470.517
=============================================
Note: *p<0.1; **p<0.05; ***p<0.01
>
> OR.vector <- exp(mylogit$coef)
> CI.vector <- exp(confint(mylogit))
Waiting for profiling to be done...
> 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")
=============================================
Dependent variable:
---------------------------
admit
---------------------------------------------
gre 1.002** (1.000, 1.004)
gpa 2.235** (1.174, 4.324)
rank2 0.509** (0.272, 0.945)
rank3 0.262*** (0.132, 0.512)
rank4 0.212*** (0.091, 0.471)
Constant 0.019*** (0.002, 0.167)
---------------------------------------------
Observations 400
Log Likelihood -229.259
Akaike Inf. Crit. 470.517
=============================================
Note: *p<0.1; **p<0.05; ***p<0.01
现在我不想单独对估计值和 CI 进行指数化,而是想像下面的代码一样使用观星者:
stargazer(mylogit, apply.coef = exp, apply.ci = exp, ci = TRUE, ci.level = 0.95, single.row = T, type = "text")
但它给了我不正确的 p 值星星的输出:
=============================================
Dependent variable:
---------------------------
admit
---------------------------------------------
gre 1.002*** (2.719, 2.730)
gpa 2.235*** (4.875, 17.902)
rank2 0.509 (0.895, 3.093)
rank3 0.262 (0.660, 2.556)
rank4 0.212 (0.545, 2.804)
Constant 0.019 (0.109, 9.514)
---------------------------------------------
Observations 400
Log Likelihood -229.259
Akaike Inf. Crit. 470.517
=============================================
Note:
*p<0.1; **p<0.05; ***p<0.01
问题:为什么最后一行代码给我错误,有没有办法修复它?
这是一个非常简单的技巧。我不确定系数和置信区间之间的模式有多少变化,但这可以改进或推广。
library('stargazer')
mydata <- read.csv("https://stats.idre.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
x <- stargazer(mylogit, ci = T, single.row = T, type = "text")
y <- stargazer(mylogit, apply.coef = exp, apply.ci = exp, ci = TRUE, ci.level = 0.95, single.row = T, type = "text")
f <- function(x, y) {
## make sure both objects are similar
stopifnot(
identical(
gsub('^(\S+)|.', '\1', x),
gsub('^(\S+)|.', '\1', y)
)
)
## where and what to get from x
mx <- gregexpr('(?<=\d)[*]+ (?=\()', x, perl = TRUE)
## where and what to subtract from y
my <- gregexpr('(?<=\d)[*]* (?=\()', y, perl = TRUE)
regmatches(y, my) <- regmatches(x, mx)
cat(y, sep = 'n')
invisible(y)
}
f(x, y)
# =============================================
# Dependent variable:
# ---------------------------
# admit
# ---------------------------------------------
# gre 1.002** (2.719, 2.730)
# gpa 2.235** (4.875, 17.902)
# rank2 0.509** (0.895, 3.093)
# rank3 0.262*** (0.660, 2.556)
# rank4 0.212*** (0.545, 2.804)
# Constant 0.019*** (0.109, 9.514)
# ---------------------------------------------
# Observations 400
# Log Likelihood -229.259
# Akaike Inf. Crit. 470.517
# =============================================
# Note: *p<0.1; **p<0.05; ***p<0.01