tidymodel tuning fails for lasso regression using family = Gamma

I'm trying to fit a lasso regression to some simulated data. The outcome variable is called 'loss' and is drawn from a gamma distribution. I can successfully fit a model when I hard code the penalty parameter but when I try to tune it as per the reproducible example below I get the following error:

"Error in class(): 0 arguments passed to 'class' which requires 1"

library(tidymodels)
library(dplyr)

# make up data
df <- tibble(loss = rgamma(10000, 1, scale = 14),
             predictor1 = runif(10000),
             predictor2 = runif(10000))

# create bootstraps
df_boots <- bootstraps(df)

# set up workflow
wf <- workflow() %>% 
  add_formula(loss ~ .) %>% 
  add_model(linear_reg(penalty = tune(), mixture = 1) %>% 
              set_engine("glmnet", family = Gamma))

# tune
tune_model <- tune_grid(
  wf,
  resamples = df_boots,
  grid = grid_regular(penalty(), levels = 50)
)
#> Warning: package 'rlang' was built under R version 3.6.3
#> 
#> Attaching package: 'rlang'
#> The following objects are masked from 'package:purrr':
#> 
#>     %@%, as_function, flatten, flatten_chr, flatten_dbl, flatten_int,
#>     flatten_lgl, flatten_raw, invoke, list_along, modify, prepend,
#>     splice
#> Warning: package 'vctrs' was built under R version 3.6.3
#> 
#> Attaching package: 'vctrs'
#> The following object is masked from 'package:tibble':
#> 
#>     data_frame
#> The following object is masked from 'package:dplyr':
#> 
#>     data_frame
#> Warning: package 'glmnet' was built under R version 3.6.3
#> Loading required package: Matrix
#> 
#> Attaching package: 'Matrix'
#> The following objects are masked from 'package:tidyr':
#> 
#>     expand, pack, unpack
#> Loaded glmnet 4.0-2
#> x Bootstrap01: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap02: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap03: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap04: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap05: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap06: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap07: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap08: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap09: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap10: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap11: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap12: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap13: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap14: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap15: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap16: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap17: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap18: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap19: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap20: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap21: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap22: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap23: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap24: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> x Bootstrap25: preprocessor 1/1, model 1/1 (predictions): Error in class(): 0 arguments...
#> Warning: All models failed. See the `.notes` column.

Created on 2021-02-15 by the reprex package (v1.0.0)

1 Like

Can you please provide a minimal reprex (reproducible example)? The goal of a reprex is to make it as easy as possible for me to recreate your problem so that I can fix it: please help me help you!

If you've never heard of a reprex before, start by reading "What is a reprex", and follow the advice further down that page.

Hi Max,
Sorry - I tried to follow the reprex guidelines when creating my original post. I've amended it now to try and make the reproducible example more minimal!
Thanks

That looks great.

It seems that glmnet doesn't offer all of the exponential families that glm does. See their docs for the list of acceptable values of family :frowning_face:

Thanks Max,

The documentation that you link to above suggests that glmnet should support all exponential families from glm . See the following section:

From version 4.0 onwards, glmnet supports both the original built-in families, as well as any family object as used by stats:glm() . This opens the door to a wide variety of additional models. For example family=binomial(link=cloglog) or family=negative.binomial(theta=1.5) (from the MASS library). Note that the code runs faster for the built-in families.

The reproducible example below shows that it is possible to fit a model with a Gamma family and then make predictions so it seems that the issue is with the tuning.

library(tidymodels)
library(dplyr)

# make up data
df <- tibble(loss = rgamma(10000, 1, scale = 14),
             predictor1 = runif(10000),
             predictor2 = runif(10000))


# fit model
fitted_model <- workflow() %>% 
  add_formula(loss ~ .) %>% 
  add_model(linear_reg(penalty = 0.1, mixture = 1) %>% 
              set_engine("glmnet", family = Gamma)) %>% 
  fit(df)


# make predictions
predict(fitted_model, df)
#> # A tibble: 10,000 x 1
#>    .pred
#>    <dbl>
#>  1  14.1
#>  2  13.8
#>  3  14.0
#>  4  14.0
#>  5  14.0
#>  6  14.0
#>  7  14.0
#>  8  13.8
#>  9  13.8
#> 10  14.0
#> # ... with 9,990 more rows

Created on 2021-02-15 by the reprex package (v1.0.0)

Hi Max,
Since it does appear that glmnet supports all built in families from glm I just wanted to check whether support for this will be built in to future versions of tune?
Thanks,

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.