这样的"求x给定y"问题是一个寻根问题.
你的问题有点特别.GLM response ~ time
表明,在这种情况下,逻辑曲线是time
的单调函数.因此,对于任何目标概率,只有一个根.这使得应用任何数值方法来寻找该根变得微不足道.我们只使用uniroot
,它非常详细.
## we want to find the root of this function
## i.e., where it crosses 0
f <- function (tm, model, prob.target) {
predict(model, newdata = data.frame(time = tm), type = "response") - prob.target
}
## try different lower bound 'lwr' and upper bound 'upr'
## until you see that the curve crosses the horizontal line at 0
lwr <- 0
upr <- 10
curve(f(x, mod, 0.5), lwr, upr)
abline(h = 0, lty = 2)
## use this 'lwr' and 'upr' for uniroot()
uniroot(f, c(lwr, upr), model = mod, prob.target = 0.5)$root
#[1] 5.160737
我懂了.我问这个问题是因为@FP0计算出的时间为4.68/0.94=5.16/this answer,所以我想可能有一个简单的关系,我遗漏了.
因为该GLM的分析表达式是已知的:
log(p / (1 - p)) = intercept + slope * time
当p=0.5时,左侧为0.因此根简单地为:
time = -(intercept / slope)
R代码为:
unname(-(mod$coef[1] / mod$coef[2]))
#[1] 5.160737
所以,我展示了如何从分析和数值两方面来实现这一点.我优先考虑数字一,因为堆栈溢出是一个编码网站:D