tidymodels / poissonreg

parsnip wrappers for Poisson regression
https://poissonreg.tidymodels.org
Other
22 stars 4 forks source link

handle quasi-likelihood glmnet models better #15

Open topepo opened 3 years ago

topepo commented 3 years ago

Similar to tidymodels/parsnip#483

library(tidymodels)
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip
library(poissonreg)

n <- 2e3
set.seed(123)
x1 <- MASS::rnegbin(n, mu = 28, theta = 0.6)
x2 <- rnorm(n, 5, 10)
x3 <- as.factor(rnorm(n) < 0)
y <- as.integer(x1 + x2 + as.numeric(x3))
y[y < 0] <- 0
data <- tibble(y, x1, x2, x3)

poisson_reg(penalty = 0.5) %>% 
  set_engine("glmnet") %>% 
  fit(y ~ x1 + x2, data = data) %>% 
  pluck("fit") %>% 
  class()
#> [1] "fishnet" "glmnet"

# 'fishnet' is their class of Poisson models

poisson_reg(penalty = 0.5) %>% 
  set_engine("glmnet", family = quasipoisson()) %>% 
  fit(y ~ x1 + x2, data = data) %>% 
  pluck("fit") %>% 
  class()
#> [1] "glmnetfit" "glmnet"

# Now the class is not Poisson specific, so our methods don't know what to do

Created on 2021-05-04 by the reprex package (v1.0.0.9000)

We should add class to the parsnip model or restrict this family for this engine.

hfrick commented 1 year ago

@topepo do you have a preference between the two options? I tend towards trying to avoid restricting the family but I guess that means ~adding~ running glmnet-specific code in parsnip::fit.model_spec() (in these lines) and parsnip::fit_xy.model_spec()

If we make the changes in parsnip, we might not need to touch the extension packages with glmnet engines