似乎predict
产生的标准误差太大。使用parsnip
模型时得到0.820,但是使用基本R模型时得到0.194。标准误差的0.194似乎更合理,因为在我的预测上下约2 * 0.195是置信区间的终点。我的问题/误解是什么?
library(parsnip)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
# 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"))
#> New names:
#> * lo -> lo...1
#> * hi -> hi...2
#> * lo -> lo...3
#> * hi -> hi...4
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-06-04创建
-EDIT-
正如@thelatemail和@Limey所说,对基本模型使用type="link"
将给出对数刻度的标准误差(0.820)。但是,我想要概率标度上的标准误差。我以为可以使用自然日志来获取我想要的东西。
parsnip_pred %>%
mutate(ln_se = log(.std_error))
#> # A tibble: 1 x 6
#> mpg .pred_1 .pred_lower_1 .pred_upper_1 .std_error ln_se
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 18 0.614 0.230 0.895 0.820 -0.199
但是我仍然缺少一些东西,因为我得到-0.199的标准误差,该误差不是0.194(我想我可以忽略负数)。我缺少parsnip
documentation中的选项吗?我想使用parsnip
。
@@ latemail是正确的。从在线文件中进行predict.glm:
类型所需的预测类型。默认值是线性预测变量的标度。替代的“响应”取决于响应变量的规模。因此,对于默认的二项式模型,默认的预测是对数奇数(对数刻度上的概率),而type =“ response”给出了预测的概率。
[默认是使用对数刻度进行报告,parsnip
以原始概率刻度请求结果。从'response'
parsnip::predict
并不明显,我发现如何选择返回结果的标度,但很明显,它使用的是原始概率标度。
所以这两种方法都返回正确的答案,它们只是使用不同的标度。
我不想从@thelatemail窃取可接受的解决方案,因此请他们发布与此类似的答案。