Specification + recipe + resample + grid + metric + control: I didn't set a workflow object, yet it's telling me that it's not compatible with a workflow

I followed these two guides here:

Below is the reprex with the dataframe generation alongside the libraries. The code is very similar to the guides above, but the errors don't seem to be present in the guides above. I tried two different functions and they are showing the same error.

# For reprex
library(plyr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:plyr':
#> 
#>     arrange, count, desc, failwith, id, mutate, rename, summarise,
#>     summarize
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

library(tidymodels)
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip
library(glmnet)
#> Loading required package: Matrix
#> 
#> Attaching package: 'Matrix'
#> The following objects are masked from 'package:tidyr':
#> 
#>     expand, pack, unpack
#> Loaded glmnet 4.1-1
library(hardhat)
#> Warning: package 'hardhat' was built under R version 4.1.1
#> 
#> Attaching package: 'hardhat'
#> The following object is masked from 'package:tune':
#> 
#>     extract_recipe

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(reprex)

df = data.frame(yearr = sample(2015:2021, 2000, replace = TRUE),
                monthh = sample(1:12, 2000, replace = TRUE),
                dayy = sample(1:29, 2000, replace = TRUE)) |>
  mutate(datee = ymd(paste(yearr, monthh, dayy)),
         weekk = week(datee),
         quarterr = quarter(datee),
         semesterr = semester(datee),
         doyy = yday(datee),
         y = sample(0:100, 2000, replace = TRUE) + (130 * yearr) + (2 * monthh) + (2 * weekk),
         dummyy = round(sample(0:1, 2000, replace = TRUE))) |>
  filter(!is.na(datee)) |>
  arrange(-desc(datee)) |>
  mutate(ii = row_number())
#> Warning: 4 failed to parse.

folds = df |>
  sliding_period(lookback = Inf, # if Inf, then it's chain
                 period = 'week',
                 index = datee,
                 #assess_stop = 1L, # include how many 'periods' in the future
                 every = 8L, # group how many 'periods' together
                 step = 1L, # how many 'periods' * 'every' to move the window (?)
                 skip = 8L)

time_only = df |>
  select(datee)

time_only = do.call('c', time_only)

rec_generic = df |>
  recipe(y ~ .) |>
  step_rm(datee) |>
  prep()

df_baked = rec_generic |>
  bake(NULL)

rec_iteration = df_baked |> # create new recipe without date variable since step_rm/IDing doesn't work
  recipe(y ~ .) |>
  step_zv(doyy)

grid_pen_mix = expand_grid(penalty = seq(0, 100, by = 25),
                           mixture = seq(0, 1, by = 0.25))

glmnet_vars = function(x) {
  # `x` will be a workflow object
  mod <- extract_fit_engine(x) #library(hardhat) # https://tune.tidymodels.org/reference/extract-tune.html
  # `df` is the number of model terms for each penalty value
  tibble(penalty = mod$lambda, num_vars = mod$df)
}

spec_enet = linear_reg(mode = 'regression', penalty = tune(), mixture = tune()) |>
  set_engine('glmnet')

metric = metric_set(rmse)

ctrl <- control_grid(extract = glmnet_vars, verbose = TRUE)

five_star_glmnet = tune_grid(spec_enet,
                             rec_iteration,
                             resamples = folds,
                             grid = grid_pen_mix,
                             metrics = metric,
                             control = ctrl) # Where is the workflow?
#> i Slice01: preprocessor 1/1
#> v Slice01: preprocessor 1/1
#> i Slice01: preprocessor 1/1, model 1/5
#> v Slice01: preprocessor 1/1, model 1/5
#> i Slice01: preprocessor 1/1, model 1/5 (predictions)
#> i Slice01: preprocessor 1/1, model 2/5
#> v Slice01: preprocessor 1/1, model 2/5
#> i Slice01: preprocessor 1/1, model 2/5 (predictions)
#> i Slice01: preprocessor 1/1, model 3/5
#> v Slice01: preprocessor 1/1, model 3/5
#> i Slice01: preprocessor 1/1, model 3/5 (predictions)
#> i Slice01: preprocessor 1/1, model 4/5
#> v Slice01: preprocessor 1/1, model 4/5
#> i Slice01: preprocessor 1/1, model 4/5 (predictions)
#> i Slice01: preprocessor 1/1, model 5/5
#> v Slice01: preprocessor 1/1, model 5/5
#> i Slice01: preprocessor 1/1, model 5/5 (predictions)
#> i Slice02: preprocessor 1/1
#> v Slice02: preprocessor 1/1
#> i Slice02: preprocessor 1/1, model 1/5
#> v Slice02: preprocessor 1/1, model 1/5
#> i Slice02: preprocessor 1/1, model 1/5 (predictions)
#> i Slice02: preprocessor 1/1, model 2/5
#> v Slice02: preprocessor 1/1, model 2/5
#> i Slice02: preprocessor 1/1, model 2/5 (predictions)
#> i Slice02: preprocessor 1/1, model 3/5
#> v Slice02: preprocessor 1/1, model 3/5
#> i Slice02: preprocessor 1/1, model 3/5 (predictions)
#> i Slice02: preprocessor 1/1, model 4/5
#> v Slice02: preprocessor 1/1, model 4/5
#> i Slice02: preprocessor 1/1, model 4/5 (predictions)
#> i Slice02: preprocessor 1/1, model 5/5
#> v Slice02: preprocessor 1/1, model 5/5
#> i Slice02: preprocessor 1/1, model 5/5 (predictions)
... [truncated] ...
#> i Slice38: preprocessor 1/1, model 5/5
#> v Slice38: preprocessor 1/1, model 5/5
#> i Slice38: preprocessor 1/1, model 5/5 (predictions)

# ERROR MESSAGE 1 HERE
head(five_star_glmnet[[5]][[1]][[3]], 1)
#> [[1]]
#> [1] "Error in UseMethod(\"extract_fit_engine\") : \n  no applicable method for 'extract_fit_engine' applied to an object of class \"workflow\"\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in UseMethod("extract_fit_engine"): no applicable method for 'extract_fit_engine' applied to an object of class "workflow">

# 

spec_enet = linear_reg(mode = 'regression', penalty = 0.1, mixture = 0.5) |>
  set_engine('glmnet')

preds_enet = spec_enet |>
  fit_resamples(y ~ .,
                resamples = folds,
                control = control_resamples(save_pred = TRUE,
                                            extract = glmnet_vars)) #can't do glmnet_vars to workflow...? Where is the workflow?

# ERROR MESSAGE 2 HERE
preds_enet[[5]][[1]][[1]]
#> [[1]]
#> [1] "Error in UseMethod(\"extract_fit_engine\") : \n  no applicable method for 'extract_fit_engine' applied to an object of class \"workflow\"\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in UseMethod("extract_fit_engine"): no applicable method for 'extract_fit_engine' applied to an object of class "workflow">

Created on 2021-09-02 by the reprex package (v2.0.0)

This topic was automatically closed 21 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.