r - Predict.glm(..., type = "response", se.fit = TRUE) 返回什么标准错误?

标签 r statistics regression glm confidence-interval

我将根据此 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)

但是,这会产生不同的标准错误。 这些误差是什么?它们可以直接用于计算拟合响应值的置信区间吗?

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 

最佳答案

如果您查看 ?family,您会发现 $mu.etamueta< 的导数 (即逆链接函数的导数)。因此,type = "response"se.fit = TRUE 给出了真实标准误差的一阶近似值。这称为“增量方法”。

当然,由于逆链接函数通常是非线性的,因此置信区间围绕拟合值不对称。因此,获取链接尺度上的置信区间,然后将其映射回响应尺度是正确的方法。

关于r - Predict.glm(..., type = "response", se.fit = TRUE) 返回什么标准错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44534864/

相关文章:

linux - R stats - 分配大矩阵/Linux 时的内存问题

r - 面板数据 R 中的多重共线性检验

R中虚拟变量的回归

r - 向ggplot中的每个矩形添加相同的渐变

r - 从数据框中选择偶数天

R:如何将两个 data.frame 合并为一个,匹配的 ID 重复自身,有时会丢失

statistics - 1 对 1 投票 : calculate ratings (Flickchart. com)

regression - Julia 混合效应模型中随机斜率的相互作用

r - 通过 R Shiny 中的 Leaflet 仅在 slider 上显示选定日期的数据点

r - tidyr::nest 在不同系统和包/R 版本上的不同行为