Reputation: 1
I have been trying to fit a relaxed regularized poisson regression using tidymodels
and poissonreg
. Whenever I specify the family (either family = poisson()
or family = "poisson"
) as arguments to pass to the engine (glmnet
), I get this error:
Error in glmnet.path(x, y, weights, lambda, nlambda, lambda.min.ratio, : Invalid family argument; must be either character, function or family object
Without the relax = TRUE
argument, there is no error. Am I missing something?
Ultimately, I'd like to estimate a family = MASS::negative.binomial(theta)
regression using the tidymodels
framework.
library(tidymodels)
library(poissonreg)
# Data generation ----
set.seed(123)
df <- tibble(
y = rpois(1000, lambda = 3),
x_1 = 2 * y + rnorm(1000),
x_2 = 0.1 * y + rnorm(1000)
)
# this works
poisson_reg(penalty = 0.5) %>%
set_engine("glmnet",
relax = TRUE
) |>
fit(y~., data = df)
#> parsnip model object
#>
#>
#> Call: glmnet::glmnet(x = maybe_matrix(x), y = y, family = "poisson", relax = ~TRUE)
#> Relaxed
#>
#> Df %Dev %Dev R Lambda
#> 1 0 0.00 0.00 1.63700
#> 2 1 14.05 77.14 1.49200
#> 3 1 25.46 77.14 1.35900
#> 4 1 34.75 77.14 1.23800
#> 5 1 42.33 77.14 1.12800
#> 6 1 48.51 77.14 1.02800
#> 7 1 53.58 77.14 0.93690
#> 8 1 57.73 77.14 0.85360
#> 9 1 61.14 77.14 0.77780
#> 10 1 63.94 77.14 0.70870
#> 11 1 66.25 77.14 0.64570
#> 12 1 68.14 77.14 0.58840
#> 13 1 69.70 77.14 0.53610
#> 14 1 70.99 77.14 0.48850
#> 15 1 72.06 77.14 0.44510
#> 16 1 72.93 77.14 0.40550
#> 17 1 73.66 77.14 0.36950
#> 18 1 74.26 77.14 0.33670
#> 19 1 74.76 77.14 0.30680
#> 20 1 75.17 77.14 0.27950
#> 21 1 75.50 77.14 0.25470
#> 22 1 75.79 77.14 0.23210
#> 23 1 76.02 77.14 0.21150
#> 24 1 76.21 77.14 0.19270
#> 25 1 76.37 77.14 0.17560
#> 26 1 76.50 77.14 0.16000
#> 27 1 76.61 77.14 0.14570
#> 28 1 76.70 77.14 0.13280
#> 29 1 76.78 77.14 0.12100
#> 30 1 76.84 77.14 0.11030
#> 31 1 76.89 77.14 0.10050
#> 32 1 76.94 77.14 0.09153
#> 33 1 76.97 77.14 0.08340
#> 34 1 77.00 77.14 0.07599
#> 35 1 77.02 77.14 0.06924
#> 36 1 77.04 77.14 0.06309
#> 37 1 77.06 77.14 0.05749
#> 38 1 77.08 77.14 0.05238
#> 39 1 77.09 77.14 0.04773
#> 40 1 77.10 77.14 0.04349
#> 41 1 77.10 77.14 0.03962
#> 42 1 77.11 77.14 0.03610
#> 43 1 77.12 77.14 0.03290
#> 44 1 77.12 77.14 0.02997
#> 45 1 77.13 77.14 0.02731
#> 46 1 77.13 77.14 0.02488
#> 47 1 77.13 77.14 0.02267
#> 48 1 77.13 77.14 0.02066
#> 49 1 77.14 77.14 0.01882
#> 50 1 77.14 77.14 0.01715
#> 51 1 77.14 77.14 0.01563
# this fails
poisson_reg(penalty = 0.5) %>%
set_engine("glmnet",
family = "poisson",
relax = TRUE
) |>
fit(y~., data = df)
#> Error in glmnet.path(x, y, weights, lambda, nlambda, lambda.min.ratio, : Invalid family argument; must be either character, function or family object
Created on 2024-02-02 with reprex v2.0.2
Upvotes: 0
Views: 81