Targets workflow management error when repeats pulling coefficients of tidyverse framework

{targets} is a package to manage the workflow. (https://wlandau.github.io/targets-manual/)
I tried to apply the {targets} for the tidymodels framework.
When I re-run target to get the coefficients of the final model, It returns null

_targets.R

library(targets)

options(tidyverse.quiet = TRUE)
tar_option_set(packages = c("tidyverse", "tidymodels"))

tar_pipeline(
  tar_target(glmn_rec,
    recipe(mpg ~ ., data = mtcars) %>% 
    step_normalize(all_predictors())),
  
  tar_target(mod,
    linear_reg(penalty = 0.1, mixture = 1) %>% 
    set_engine("glmnet")),
  
  tar_target(glmn_wflow,
    workflow() %>% 
    add_model(mod) %>% 
    add_recipe(glmn_rec)),
  
  tar_target(glmn_fit,
    glmn_wflow %>% 
    fit(data = mtcars)),
  
  tar_target(coeff,
    glmn_fit %>% 
    pull_workflow_fit() %>% 
    pluck("fit") %>% 
    coef(s = 0.1)) 
)
library(targets)
tar_make()
tar_read(coeff)
# edit the coef(s=0.1) -> coef(s=0.2) in the original _targets.R file

  tar_target(coeff,
    glmn_fit %>% 
    pull_workflow_fit() %>% 
    pluck("fit") %>% 
    coef(s = 0.2)) 

# re-run coeff target return NULL
tar_make()
tar_read(coeff)

Hi @jkang,

I was able to use your code and did not run into any issues which produced a NULL value for the target coeff. Can you try running the code below and see if you are able to reproduce the same results?

library(targets)

tar_script({
  options(tidyverse.quiet = TRUE)
  tar_option_set(packages = c("tidyverse", "tidymodels"))
  
  tar_pipeline(
    tar_target(
      glmn_rec,
      recipe(mpg ~ ., data = mtcars) %>%
        step_normalize(all_predictors())
      ),
    tar_target(
      mod,
      linear_reg(penalty = 0.1, mixture = 1) %>% 
        set_engine("glmnet")
      ),
    tar_target(
      glmn_wflow,
      workflow() %>% 
        add_model(mod) %>% 
        add_recipe(glmn_rec)
      ),
    tar_target(
      glmn_fit,
      glmn_wflow %>% 
        fit(data = mtcars)
      ),
    tar_target(
      coeff,
      glmn_fit %>%
        pull_workflow_fit() %>% 
        pluck("fit") %>% 
        coef(s = 0.1)
      )
    )
  })
tar_make()
#> e[34m●e[39m run target glmn_rec
#> e[34m●e[39m run target mod
#> e[34m●e[39m run target glmn_wflow
#> e[34m●e[39m run target glmn_fit
#> e[34m●e[39m run target coeff
tar_read(coeff)
#> Loading required package: Matrix
#> 11 x 1 sparse Matrix of class "dgCMatrix"
#>                       1
#> (Intercept) 20.09062500
#> cyl         -0.39267064
#> disp         .         
#> hp          -0.89172557
#> drat         0.41257152
#> wt          -2.58105019
#> qsec         0.82333186
#> vs           0.05920734
#> am           1.05461719
#> gear         0.22456562
#> carb        -0.75029547
tar_make()
#> e[32m✓e[39m skip target glmn_rec
#> e[32m✓e[39m skip target mod
#> e[32m✓e[39m skip target glmn_wflow
#> e[32m✓e[39m skip target glmn_fit
#> e[32m✓e[39m skip target coeff
#> e[32m✓e[39m Already up to date.
tar_read(coeff)
#> 11 x 1 sparse Matrix of class "dgCMatrix"
#>                       1
#> (Intercept) 20.09062500
#> cyl         -0.39267064
#> disp         .         
#> hp          -0.89172557
#> drat         0.41257152
#> wt          -2.58105019
#> qsec         0.82333186
#> vs           0.05920734
#> am           1.05461719
#> gear         0.22456562
#> carb        -0.75029547

Created on 2020-10-06 by the reprex package (v0.3.0)

1 Like

Hi @mattwarkentin,

I`m glad to your interest on my issue.

There was my mistake in the reproducible example.

Please edit the coeff target, before re-tar_make().

I edit the original issue.

Many thanks

@mattwarkentin, thanks for helping @jkang.

I think there's a lot we still to figure out when it comes to combining targets with tidymodels. They have different ways of managing data and artifacts (e.g. tidymodels workflows and recipes carry the original data along for the ride) and it is sometimes hard to follow the advice at https://wlandau.github.io/targets-manual/practice.html#targets. The pattern in https://github.com/wlandau/targets-keras and https://github.com/wlandau/targets-tutorial is useful to start, but it does not use any cross validation.

Hi @mattwarkentin

I'll check the tutorial.

Many thanks.

Hi @jkang,

Thanks for updating the example. I was able to reproduce your issue. I don't know why it's happening but after some playing around I was able to avoid the issue by making a namespaced call to glmnet:::coef.glmnet().

Can you try the code below and see if it works on your end when you update s = 0.1 before calling tar_make() a second time?

library(targets)

tar_script({
  options(tidyverse.quiet = TRUE)
  tar_option_set(packages = c("tidyverse", "tidymodels"),
                 format = "qs")
  
  tar_pipeline(
    tar_target(
      glmn_rec,
      recipe(mpg ~ ., data = mtcars) %>%
        step_normalize(all_predictors())
    ),
    tar_target(
      mod,
      linear_reg(penalty = 0.1, mixture = 1) %>% 
        set_engine("glmnet")
    ),
    tar_target(
      glmn_wflow,
      workflow() %>% 
        add_model(mod) %>% 
        add_recipe(glmn_rec)
    ),
    tar_target(
      glmn_fit,
      glmn_wflow %>% 
        fit(data = mtcars)
    ),
    tar_target(
      coeff,
      glmn_fit %>%
        pull_workflow_fit() %>% 
        pluck("fit") %>% 
        glmnet:::coef.glmnet(s = 0.1)
    )
  )
})
tar_make()
1 Like

Hi @mattwarkentin

Your solution works!!

Thank you for your help.

No problem. While that did seem to solve the issue at hand, it's generally not recommended to namspace internal functions (i.e use the triple-colon operator :::), especially internal S3 methods. A more robust long-term solution is needed, but I'm glad it was helpful in the short-term.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.