title: How to extract GLMNET coefficients produced by Tidymodels
body: <p>I estimated a <strong>glmnet</strong> logistic regression using <strong>tidymodels</strong>.  But I couldn't figure out 2 things, which are closely related, in <strong>tidymodels</strong>:</p>
  <ul>
  <li>a) how to extract the estimated coefficients</li>
  <li>b) save the estimated model for future production use.</li>
  </ul>
  <p>Below are the codes for a pseudo model.  I tried <code>tidy()</code>, <code>coef()</code>, and <code>predict()</code> but they all failed.  Any help will be much appreciated.  Thanks.</p>
  <pre class="lang-r prettyprint-override"><code>library(tidymodels)
  #&gt; -- Attaching packages --------------------------------------------------------------------------------------------------------------------------- tidymodels 0.1.0 --
  #&gt; v broom     0.7.0      v recipes   0.1.13
  #&gt; v dials     0.0.8      v rsample   0.0.7 
  #&gt; v dplyr     1.0.0      v tibble    3.0.3 
  #&gt; v ggplot2   3.3.2      v tune      0.1.1 
  #&gt; v infer     0.5.2      v workflows 0.1.2 
  #&gt; v parsnip   0.1.2      v yardstick 0.0.7 
  #&gt; v purrr     0.3.4
  #&gt; -- Conflicts ------------------------------------------------------------------------------------------------------------------------------ tidymodels_conflicts() --
  #&gt; x purrr::discard() masks scales::discard()
  #&gt; x dplyr::filter()  masks stats::filter()
  #&gt; x dplyr::lag()     masks stats::lag()
  #&gt; x recipes::step()  masks stats::step()
  
  set.seed(1234)
  
  train &lt;- tibble(y = factor(sample(c(0,1), 1000, replace = TRUE)),
                  x1 = rnorm(1000),
                  x2 = rnorm(1000)
                  )
  
  test &lt;- tibble(y = factor(sample(c(0,1), 300, replace = TRUE)),
                 x1 = rnorm(300),
                 x2 = rnorm(300)
                 )
  
  lr_cv &lt;- vfold_cv(train)
  
  lr_mod &lt;- logistic_reg(penalty = tune(), mixture = 1) %&gt;%
    set_mode(&quot;classification&quot;) %&gt;%
    set_engine(&quot;glmnet&quot;)
  
  lr_wf &lt;- workflow() %&gt;%
    add_model(lr_mod) %&gt;%
    add_formula(y ~ .)
  
  lr_grid &lt;- tibble(penalty = 10^seq(-4,1, length.out = 5))
  
  lr_tune &lt;- tune_grid(lr_wf, resamples = lr_cv, grid = lr_grid, metrics = metric_set(roc_auc))
  
  lr_best &lt;- select_best(lr_tune)
  lr_best
  #&gt; # A tibble: 1 x 2
  #&gt;   penalty .config
  #&gt;     &lt;dbl&gt; &lt;chr&gt;  
  #&gt; 1  0.0001 Model1
  
  lr_wf_final &lt;- lr_wf %&gt;% finalize_workflow(lr_best)
  
  final_mod &lt;- lr_wf_final %&gt;% fit(train)
  
  # the followings were tried but didn't work
  lambda &lt;- lr_best[1]
  lambda
  #&gt; # A tibble: 1 x 1
  #&gt;   penalty
  #&gt;     &lt;dbl&gt;
  #&gt; 1  0.0001
  
  predict(final.mod , s = lambda, type =&quot;coefficients&quot;)[1:3 ,]
  #&gt; Error in predict(final.mod, s = lambda, type = &quot;coefficients&quot;): object 'final.mod' not found
  
  tidy(final_mod)
  #&gt; # A tibble: 35 x 5
  #&gt;    term         step estimate  lambda dev.ratio
  #&gt;    &lt;chr&gt;       &lt;dbl&gt;    &lt;dbl&gt;   &lt;dbl&gt;     &lt;dbl&gt;
  #&gt;  1 (Intercept)     1  -0.0560 0.0183   1.71e-14
  #&gt;  2 (Intercept)     2  -0.0561 0.0167   1.65e- 4
  #&gt;  3 (Intercept)     3  -0.0562 0.0152   3.02e- 4
  #&gt;  4 (Intercept)     4  -0.0562 0.0139   4.16e- 4
  #&gt;  5 (Intercept)     5  -0.0563 0.0126   5.10e- 4
  #&gt;  6 (Intercept)     6  -0.0564 0.0115   5.89e- 4
  #&gt;  7 (Intercept)     7  -0.0564 0.0105   6.54e- 4
  #&gt;  8 (Intercept)     8  -0.0565 0.00956  7.08e- 4
  #&gt;  9 (Intercept)     9  -0.0565 0.00871  7.53e- 4
  #&gt; 10 (Intercept)    10  -0.0566 0.00794  7.90e- 4
  #&gt; # ... with 25 more rows
  
  coef(final_mod)
  #&gt; NULL
  </code></pre>
  <p><sup>Created on 2020-07-25 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v0.3.0)</sup></p>