MYa*_*208 2 r glm logistic-regression
我想找到Lethal Dose(LD50),其置信区间为R.其他软件系列Minitab,SPSS,SAS提供了三种不同版本的置信区间.我在任何包中找不到这样的间隔R(我也使用findFn了sos包中的函数).如果有人指导如何找到这样的间隔,我将非常感激.我基于Delta方法编写了一种类型的间隔(不确定它是否正确)但是想要使用R包中的任何已建立的函数.谢谢
MWE
dose <- c(10.2, 7.7, 5.1, 3.8, 2.6, 0)
total <- c(50, 49, 46, 48, 50, 49)
affected <- c(44, 42, 24, 16, 6, 0)
finney71 <- data.frame(dose, total, affected)
fm1 <- glm(cbind(affected, total-affected) ~ log(dose),
family=binomial(link = logit), data=finney71[finney71$dose != 0, ])
summary(fm1)$coef
Estimate Std. Error z value Pr(>|z|)
(Intercept) -4.886912 0.6429272 -7.601035 2.937717e-14
log(dose) 3.103545 0.3877178 8.004650 1.198070e-15
library(MASS)
xp <- dose.p(fm1, p=c(0.50, 0.90, 0.95)) # from MASS
xp.ci <- xp + attr(xp, "SE") %*% matrix(qnorm(1 - 0.05/2)*c(-1,1), nrow=1)
zp.est <- exp(cbind(xp, attr(xp, "SE"), xp.ci[,1], xp.ci[,2]))
dimnames(zp.est)[[2]] <- c("LD", "SE", "LCL","UCL")
zp.est
LD SE LCL UCL
p = 0.50: 4.828918 1.053044 4.363708 5.343724
p = 0.90: 9.802082 1.104050 8.073495 11.900771
p = 0.95: 12.470382 1.133880 9.748334 15.952512
Run Code Online (Sandbox Code Playgroud)
从软件包drc中,您可以获得ED50(相同的计算)以及置信区间.
library(drc) # Directly borrowed from the drc manual
mod <- drm(affected/total ~ dose, weights = total,
data = finney71[finney71$dose != 0, ], fct = LL2.2(), type = "binomial")
#intervals on log scale
ED(mod, c(50, 90, 95), interval = "fls", reference = "control")
Estimated effective doses
(Back-transformed from log scale-based confidence interval(s))
Estimate Lower Upper
1:50 4.8289 4.3637 5.3437
1:90 9.8021 8.0735 11.9008
1:95 12.4704 9.7483 15.9525
Run Code Online (Sandbox Code Playgroud)
哪个与手动输出相匹配.
"finney71"数据包含在此包中,您的置信区间计算与人们给出的示例完全匹配drc,直到"#MASS"注释.你应该赞扬他们,而不是声称你写了代码.
还有其他一些方法可以解决这个问题.一种是使用参数化引导程序,可通过boot程序包方便地使用.
首先,我们将改装模型.
library(boot)
finney71 <- finney71[finney71$dose != 0,] # pre-clean data
fm1 <- glm(cbind(affected, total-affected) ~ log(dose),
family=binomial(link = logit),
data=finney71)
Run Code Online (Sandbox Code Playgroud)
为了说明,我们可以计算出LD50和LD75.
statfun <- function(dat, ind) {
mod <- update(fm1, data = dat[ind,])
coefs <- coef(mod)
c(exp(-coefs[1]/coefs[2]),
exp((log(0.75/0.25) - coefs[2])/coefs[1]))
}
boot_out <- boot(data = finney71, statistic = statfun, R = 1000)
Run Code Online (Sandbox Code Playgroud)
该boot.ci函数可以使用此对象为我们计算出各种置信区间.
boot.ci(boot_out, index = 1, type = c('basic', 'perc', 'norm'))
##BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
##Based on 999 bootstrap replicates
##
##CALL :
##boot.ci(boot.out = boot_out, type = c("basic", "perc", "norm"),
## index = 1)
##Intervals :
##Level Normal Basic Percentile
##95% ( 3.976, 5.764 ) ( 4.593, 5.051 ) ( 4.607, 5.065 )
Run Code Online (Sandbox Code Playgroud)
使用正态近似的置信区间被相当多的极值抛出,基本和基于百分位数的区间更加稳健.
有一点值得注意的是:如果斜率的符号足够清晰,我们可以得到一些相当极端的值(模拟在这个答案中,并在Andrew Gelman的博客文章中进行更深入的讨论).
set.seed(1)
x <- rnorm(100)
z = 0.05 + 0.1*x*rnorm(100, 0, 0.05) # small slope and more noise
pr = 1/(1+exp(-z))
y = rbinom(1000, 1, pr)
sim_dat <- data.frame(x, y)
sim_mod <- glm(y ~ x, data = sim_dat, family = 'binomial')
statfun <- function(dat, ind) {
mod <- update(sim_mod, data = dat[ind,])
-coef(mod)[1]/coef(mod)[2]
}
sim_boot <- boot(data = sim_dat, statistic = statfun, R = 1000)
hist(sim_boot$t[,1], breaks = 100,
main = "Bootstrap of simulated model")
Run Code Online (Sandbox Code Playgroud)
上面的delta方法给出了我们的平均值= 6.448,更低的ci = -36.22,以及更高的ci = 49.12,并且所有的bootstrap CI都给出了类似的极端估计.
##Level Normal Basic Percentile
##95% (-232.19, 247.76 ) ( -20.17, 45.13 ) ( -32.23, 33.06 )
Run Code Online (Sandbox Code Playgroud)