Skip to content

multi_predict() for glmnet to return mean count - #89

Merged
hfrick merged 4 commits into
mainfrom
glmnet-multi-predict-count
Nov 13, 2024
Merged

multi_predict() for glmnet to return mean count#89
hfrick merged 4 commits into
mainfrom
glmnet-multi-predict-count

Conversation

@hfrick

@hfrick hfrick commented Nov 11, 2024

Copy link
Copy Markdown
Member

closes #63

library(poissonreg)
#> Loading required package: parsnip

data(seniors, package = "poissonreg")

spec <- poisson_reg(penalty = 0.1, mixture = 0.3) %>%
  set_engine("glmnet", nlambda = 15)
f_fit <- fit(spec, count ~ ., data = seniors)

# default: type = NULL -> "numeric"
our_pred <- predict(f_fit, seniors, penalty = 0.1)
our_pred
#> # A tibble: 8 × 1
#>   .pred
#>   <dbl>
#> 1 540. 
#> 2 740. 
#> 3 282. 
#> 4 387. 
#> 5  90.7
#> 6 124. 
#> 7  47.4
#> 8  65.0
identical(our_pred, predict(f_fit, seniors, penalty = 0.1, type = "numeric"))
#> [1] TRUE

# default: type = NULL
our_multi_pred <- multi_predict(f_fit, seniors, penalty = 0.1) %>% 
  tidyr::unnest(cols = .pred)
our_multi_pred
#> # A tibble: 8 × 2
#>   penalty .pred
#>     <dbl> <dbl>
#> 1     0.1 540. 
#> 2     0.1 740. 
#> 3     0.1 282. 
#> 4     0.1 387. 
#> 5     0.1  90.7
#> 6     0.1 124. 
#> 7     0.1  47.4
#> 8     0.1  65.0


seniors_x <- model.matrix(~ ., data = seniors[, -4])[, -1]
seniors_y <- seniors$count
glmnet_fit <- glmnet::glmnet(x = seniors_x, y = seniors_y, family = "poisson",
                          alpha = 0.3, nlambda = 15)
waldo::compare(
  our_pred$.pred,
  predict(glmnet_fit, seniors_x, s = 0.1, type = "response") %>% as.vector()
)
#> ✔ No differences

# This is now also the response aka mean count (instead of the linear predictor)
waldo::compare(
  our_multi_pred$.pred,
  predict(glmnet_fit, seniors_x, s = 0.1, type = "response") %>% as.vector()
)
#> ✔ No differences

Created on 2024-11-11 with reprex v2.1.0

@hfrick

hfrick commented Nov 11, 2024

Copy link
Copy Markdown
Member Author

CI failure on oldrel-4 (R 4.0.5) is due to a undeclared dependency of distributional on R 4.1 through a use of |>. That's been fixed in the dev version of distributional: mitchelloharawild/distributional@2a2db76

CI failure on devel is due to C errors when building MatrixStats but I am not sure how poissonreg depends on that or what to do about it. Hoping it will resolve itself. If not, it should be a separate PR.

> pak::pkg_deps_explain("poissonreg", "matrixStats", dependencies = TRUE)
                                                                          
x matrixStats

@hfrick
hfrick requested a review from simonpcouch November 11, 2024 10:31

@simonpcouch simonpcouch left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏄

Comment thread R/poisson_reg-glmnet.R
Comment thread NEWS.md Outdated
Co-authored-by: Simon P. Couch <simonpatrickcouch@gmail.com>
@hfrick
hfrick merged commit f17b777 into main Nov 13, 2024
@hfrick
hfrick deleted the glmnet-multi-predict-count branch November 13, 2024 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

glmnet: Default to mean count for multi_predict()

2 participants