Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi_predict() for glmnet to return mean count #89

Merged
merged 4 commits into from
Nov 13, 2024

Conversation

hfrick
Copy link
Member

@hfrick hfrick commented Nov 11, 2024

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
Copy link
Member Author

hfrick commented Nov 11, 2024

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

Copy link
Contributor

@simonpcouch simonpcouch left a comment

Choose a reason for hiding this comment

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

🏄

R/poisson_reg-glmnet.R Outdated Show resolved Hide resolved
NEWS.md Outdated Show resolved Hide resolved
@hfrick hfrick merged commit f17b777 into main Nov 13, 2024
12 of 13 checks passed
@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