在执行逻辑回归后,我将根据excellent example中提供的关于如何计算响应的95%置信区间的数据拟合模型:
foo <- mtcars[,c("mpg","vs")]; names(foo) <- c("x","y")
mod <- glm(y ~ x, data = foo, family = binomial)
preddata <- with(foo, data.frame(x = seq(min(x), max(x), length = 100)))
preds <- predict(mod, newdata = preddata, type = "link", se.fit = TRUE)
critval <- 1.96 ## approx 95% CI
upr <- preds$fit + (critval * preds$se.fit)
lwr <- preds$fit - (critval * preds$se.fit)
fit <- preds$fit
fit2 <- mod$family$linkinv(fit)
upr2 <- mod$family$linkinv(upr)
lwr2 <- mod$family$linkinv(lwr)
现在,我的问题来自于你可以直接得到答案,只需要求
predict(..., type = 'response', se.fit = TRUE)
没有改变
predict(..., type = 'link', se.fit = TRUE)
但是,这会产生不同的标准误差 . What are these errors and can they be directly used to calculate the confidence interval on the fitted response values?
predict.glm
中相关的代码是这样的:
switch(type, response = {
se.fit <- se.fit * abs(family(object)$mu.eta(fit))
fit <- family(object)$linkinv(fit)
}, link = , terms = )
并比较输出:
preds_2 <- predict(mod, newdata = preddata, type = "response", se.fit = TRUE)
> head(preds_2$fit)
1 2 3 4 5 6
0.01265744 0.01399994 0.01548261 0.01711957 0.01892627 0.02091959
> head(preds_2$se.fit)
1 2 3 4 5 6
0.01944681 0.02098841 0.02263473 0.02439022 0.02625902 0.02824491
如何从上面看到:
> head(fit2)
1 2 3 4 5 6
0.01265744 0.01399994 0.01548261 0.01711957 0.01892627 0.02091959
> head(upr2)
1 2 3 4 5 6
0.2130169 0.2184891 0.2240952 0.2298393 0.2357256 0.2417589
> head(lwr2)
1 2 3 4 5 6
0.0006067975 0.0007205942 0.0008555502 0.0010155472 0.0012051633 0.0014297930
1 回答
如果你看
?family
,你会看到$mu.eta
是mu
的衍生物eta
(即反向链接函数的导数) . 因此,se.fit = TRUE
fortype = "response"
给出了真正标准误差的一阶近似 . 这被称为"delta method" .当然,由于反向链接函数通常是非线性的,因此置信区间在拟合值周围不对称 . 因此,在链接比例上获得置信区间,然后将其映射回响应比例是要走的路 .