如何提取 Tidymodels 生成的 GLMNET 系数

nyk*_*nyk 4 r glmnet tidymodels

我使用tidymodels估计了glmnet逻辑回归。但我无法弄清楚tidymodels中密切相关的两件事:

  • a) 如何提取估计系数
  • b) 保存估计模型以供将来生产使用。

以下是伪模型的代码。我尝试过tidy()coef()predict()都失败了。任何帮助都感激不尽。谢谢。

library(tidymodels)
#> -- Attaching packages --------------------------------------------------------------------------------------------------------------------------- tidymodels 0.1.0 --
#> v broom     0.7.0      v recipes   0.1.13
#> v dials     0.0.8      v rsample   0.0.7 
#> v dplyr     1.0.0      v tibble    3.0.3 
#> v ggplot2   3.3.2      v tune      0.1.1 
#> v infer     0.5.2      v workflows 0.1.2 
#> v parsnip   0.1.2      v yardstick 0.0.7 
#> v purrr     0.3.4
#> -- Conflicts ------------------------------------------------------------------------------------------------------------------------------ tidymodels_conflicts() --
#> x purrr::discard() masks scales::discard()
#> x dplyr::filter()  masks stats::filter()
#> x dplyr::lag()     masks stats::lag()
#> x recipes::step()  masks stats::step()

set.seed(1234)

train <- tibble(y = factor(sample(c(0,1), 1000, replace = TRUE)),
                x1 = rnorm(1000),
                x2 = rnorm(1000)
                )

test <- tibble(y = factor(sample(c(0,1), 300, replace = TRUE)),
               x1 = rnorm(300),
               x2 = rnorm(300)
               )

lr_cv <- vfold_cv(train)

lr_mod <- logistic_reg(penalty = tune(), mixture = 1) %>%
  set_mode("classification") %>%
  set_engine("glmnet")

lr_wf <- workflow() %>%
  add_model(lr_mod) %>%
  add_formula(y ~ .)

lr_grid <- tibble(penalty = 10^seq(-4,1, length.out = 5))

lr_tune <- tune_grid(lr_wf, resamples = lr_cv, grid = lr_grid, metrics = metric_set(roc_auc))

lr_best <- select_best(lr_tune)
lr_best
#> # A tibble: 1 x 2
#>   penalty .config
#>     <dbl> <chr>  
#> 1  0.0001 Model1

lr_wf_final <- lr_wf %>% finalize_workflow(lr_best)

final_mod <- lr_wf_final %>% fit(train)

# the followings were tried but didn't work
lambda <- lr_best[1]
lambda
#> # A tibble: 1 x 1
#>   penalty
#>     <dbl>
#> 1  0.0001

predict(final.mod , s = lambda, type ="coefficients")[1:3 ,]
#> Error in predict(final.mod, s = lambda, type = "coefficients"): object 'final.mod' not found

tidy(final_mod)
#> # A tibble: 35 x 5
#>    term         step estimate  lambda dev.ratio
#>    <chr>       <dbl>    <dbl>   <dbl>     <dbl>
#>  1 (Intercept)     1  -0.0560 0.0183   1.71e-14
#>  2 (Intercept)     2  -0.0561 0.0167   1.65e- 4
#>  3 (Intercept)     3  -0.0562 0.0152   3.02e- 4
#>  4 (Intercept)     4  -0.0562 0.0139   4.16e- 4
#>  5 (Intercept)     5  -0.0563 0.0126   5.10e- 4
#>  6 (Intercept)     6  -0.0564 0.0115   5.89e- 4
#>  7 (Intercept)     7  -0.0564 0.0105   6.54e- 4
#>  8 (Intercept)     8  -0.0565 0.00956  7.08e- 4
#>  9 (Intercept)     9  -0.0565 0.00871  7.53e- 4
#> 10 (Intercept)    10  -0.0566 0.00794  7.90e- 4
#> # ... with 25 more rows

coef(final_mod)
#> NULL
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020-07-25 创建

Jul*_*lge 7

我们最近添加了一些更新的支持来获取估计系数。

\n
library(tidymodels)\n#> \xe2\x94\x80\xe2\x94\x80 Attaching packages \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80 tidymodels 0.1.1 \xe2\x94\x80\xe2\x94\x80\n#> \xe2\x9c\x93 broom     0.7.0          \xe2\x9c\x93 recipes   0.1.13    \n#> \xe2\x9c\x93 dials     0.0.8          \xe2\x9c\x93 rsample   0.0.7     \n#> \xe2\x9c\x93 dplyr     1.0.0          \xe2\x9c\x93 tibble    3.0.3     \n#> \xe2\x9c\x93 ggplot2   3.3.2          \xe2\x9c\x93 tidyr     1.1.0     \n#> \xe2\x9c\x93 infer     0.5.3          \xe2\x9c\x93 tune      0.1.1.9000\n#> \xe2\x9c\x93 modeldata 0.0.2          \xe2\x9c\x93 workflows 0.1.2     \n#> \xe2\x9c\x93 parsnip   0.1.2.9000     \xe2\x9c\x93 yardstick 0.0.7     \n#> \xe2\x9c\x93 purrr     0.3.4\n#> \xe2\x94\x80\xe2\x94\x80 Conflicts \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80 tidymodels_conflicts() \xe2\x94\x80\xe2\x94\x80\n#> x purrr::discard() masks scales::discard()\n#> x dplyr::filter()  masks stats::filter()\n#> x dplyr::lag()     masks stats::lag()\n#> x recipes::step()  masks stats::step()\n\nset.seed(1234)\ntrain <- tibble(y = factor(sample(c(0,1), 1000, replace = TRUE)),\n                x1 = as.numeric(y) + rnorm(1000),\n                x2 = rnorm(1000),\n                x3 = rnorm(1000)\n)\n\nlr_cv <- vfold_cv(train)\nlr_grid <- tibble(penalty = 10^seq(-4,1, length.out = 5))\nlr_mod <- logistic_reg(penalty = tune(), mixture = 1) %>%\n  set_mode("classification") %>%\n  set_engine("glmnet")\n\nlr_wf <- workflow() %>%\n  add_model(lr_mod) %>%\n  add_formula(y ~ .) \n\nlr_best <- lr_wf %>%\n  tune_grid(\n    resamples = lr_cv,\n    grid = lr_grid\n  ) %>%\n  select_best("roc_auc")\n\nlr_wf %>% \n  finalize_workflow(lr_best) %>%\n  fit(train) %>%\n  pull_workflow_fit() %>%\n  tidy()\n#> Loading required package: Matrix\n#> \n#> Attaching package: \'Matrix\'\n#> The following objects are masked from \'package:tidyr\':\n#> \n#>     expand, pack, unpack\n#> Loaded glmnet 4.0-2\n#> # A tibble: 4 x 3\n#>   term        estimate penalty\n#>   <chr>          <dbl>   <dbl>\n#> 1 (Intercept)   -1.30   0.0316\n#> 2 x1             0.834  0.0316\n#> 3 x2             0      0.0316\n#> 4 x3             0      0.0316\n
Run Code Online (Sandbox Code Playgroud)\n

reprex 包于 2020-07-30 创建(v0.3.0.9001)

\n

提取系数的步骤是拉出拟合对象,然后对其进行整理

\n

如果您想保存此对象以供将来使用,您可以只保存这些系数(毕竟它是一个线性模型),也可以将最终确定的拟合工作流程保存为二进制对象,可以从中进行预测。

\n