【发布时间】:2020-09-24 06:00:07
【问题描述】:
似乎predict 产生了一个太大的标准错误。 parsnip 模型得到 0.820,而基本 R 模型得到 0.194。标准误差 0.194 似乎更合理,因为在我的预测上下大约 2*0.195 是置信区间的末端。我的问题/误解是什么?
library(parsnip)
library(dplyr)
# example data
mod_dat <- mtcars %>%
as_tibble() %>%
mutate(cyl_8 = as.numeric(cyl == 8)) %>%
select(mpg, cyl_8)
parsnip_mod <- logistic_reg() %>%
set_engine("glm") %>%
fit(as.factor(cyl_8) ~ mpg, data = mod_dat)
base_mod <- glm(as.factor(cyl_8) ~ mpg, data = mod_dat, family = "binomial")
parsnip_pred <- tibble(mpg = 18) %>%
bind_cols(predict(parsnip_mod, new_data = ., type = 'prob'),
predict(parsnip_mod, new_data = ., type = 'conf_int', std_error = T)) %>%
select(!ends_with("_0"))
base_pred <- predict(base_mod, tibble(mpg = 18), se.fit = T, type = "response") %>%
unlist()
# these give the same prediction but different SE
parsnip_pred
#> # A tibble: 1 x 5
#> mpg .pred_1 .pred_lower_1 .pred_upper_1 .std_error
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 18 0.614 0.230 0.895 0.820
base_pred
#> fit.1 se.fit.1 residual.scale
#> 0.6140551 0.1942435 1.0000000
由reprex package (v0.3.0) 于 2020 年 6 月 4 日创建
--编辑--
正如@thelatemail 和@Limey 所说,使用type="link" 作为基本模型将给出logit 标度上的标准误差(0.820)。但是,我想要概率尺度上的标准误差。
parsnip documentation 中是否有我缺少的选项?我想使用parsnip。
【问题讨论】:
-
我认为保留 so 帖子对于这种特定情况也很好,他清楚地知道他在问什么,有一个代表,而且这个问题很有趣,交叉验证的人也将如何验证回答包装特定的问题?你可以尝试在parnsnip的github repo上提问
-
您明确要求
type = "response",这不是默认值。我可以使用predict(base_mod, data.frame(mpg=18), se.fit=TRUE, type="link")获得0.82结果 - 查看?predict.glm以查看不同的types返回。 -
@thelatemail 我想要响应/概率标度上的标准误差,这就是我使用
type = "response"的原因。如何从parsnip模型中获得结果以给出相同比例的标准误差?查看修改 -
@LeviBaguley - 您可以通过
predict(parsnip_mod, new_data =tibble(mpg=18), type="raw", opts=list(se.fit=TRUE, type="response"))使用底层predict.glm 函数来强制输出您想要的输出,但与直接使用predict.glm相比,这似乎过于复杂。
标签: r predict confidence-interval tidymodels