-
Notifications
You must be signed in to change notification settings - Fork 92
Support glmnet models with base-R families #890
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
65b73ab
add methods for `glmnetfit` objects
hfrick e6fb0c7
use `type = "response"` for all log reg prediction
hfrick b221494
add NEWS bullet
hfrick cae067b
Merge branch 'main' into glmnet-base-family
hfrick 9e3905a
merge main
hfrick c1e795d
bump version for tests in extratests
hfrick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: parsnip | ||
Title: A Common API to Modeling and Analysis Functions | ||
Version: 1.0.4.9001 | ||
Version: 1.0.4.9002 | ||
Authors@R: c( | ||
person("Max", "Kuhn", , "[email protected]", role = c("aut", "cre")), | ||
person("Davis", "Vaughan", , "[email protected]", role = "aut"), | ||
|
@@ -34,7 +34,7 @@ Imports: | |
rlang (>= 0.3.1), | ||
stats, | ||
tibble (>= 2.1.1), | ||
tidyr (>= 1.0.0), | ||
tidyr (>= 1.3.0), | ||
utils, | ||
vctrs (>= 0.4.1), | ||
withr | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,34 +184,41 @@ predict._lognet <- predict_glmnet | |
multi_predict._lognet <- multi_predict_glmnet | ||
|
||
format_glmnet_multi_logistic_reg <- function(pred, penalty, type, lvl) { | ||
param_key <- tibble(group = colnames(pred), penalty = penalty) | ||
|
||
type <- rlang::arg_match(type, c("class", "prob")) | ||
|
||
penalty_key <- tibble(s = colnames(pred), penalty = penalty) | ||
|
||
pred <- as_tibble(pred) | ||
pred$.row <- 1:nrow(pred) | ||
pred <- gather(pred, group, .pred_class, -.row) | ||
pred$.row <- seq_len(nrow(pred)) | ||
pred <- tidyr::pivot_longer(pred, -.row, names_to = "s", values_to = ".pred") | ||
|
||
if (type == "class") { | ||
pred[[".pred_class"]] <- factor(pred[[".pred_class"]], levels = lvl) | ||
pred <- pred %>% | ||
dplyr::mutate(.pred_class = dplyr::if_else(.pred >= 0.5, lvl[2], lvl[1]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. apart from this line adding the translation from probabilities to classes, the changes to this function are only to make use of dplyr and tidyr |
||
.pred_class = factor(.pred_class, levels = lvl), | ||
.keep = "unused") | ||
} else { | ||
if (type == "response") { | ||
pred[[".pred2"]] <- 1 - pred[[".pred_class"]] | ||
names(pred) <- c(".row", "group", paste0(".pred_", rev(lvl))) | ||
pred <- pred[, c(".row", "group", paste0(".pred_", lvl))] | ||
} | ||
pred <- pred %>% | ||
dplyr::mutate(.pred_class_2 = 1 - .pred) %>% | ||
rlang::set_names(c(".row", "s", paste0(".pred_", rev(lvl)))) %>% | ||
dplyr::select(c(".row", "s", paste0(".pred_", lvl))) | ||
} | ||
|
||
if (utils::packageVersion("dplyr") >= "1.0.99.9000") { | ||
pred <- full_join(param_key, pred, by = "group", multiple = "all") | ||
pred <- dplyr::full_join(penalty_key, pred, by = "s", multiple = "all") | ||
} else { | ||
pred <- full_join(param_key, pred, by = "group") | ||
pred <- dplyr::full_join(penalty_key, pred, by = "s") | ||
} | ||
pred$group <- NULL | ||
pred <- arrange(pred, .row, penalty) | ||
.row <- pred$.row | ||
pred$.row <- NULL | ||
pred <- split(pred, .row) | ||
names(pred) <- NULL | ||
tibble(.pred = pred) | ||
} | ||
|
||
pred <- pred %>% | ||
dplyr::select(-s) %>% | ||
dplyr::arrange(penalty) %>% | ||
tidyr::nest(.by = .row, .key = ".pred") %>% | ||
dplyr::select(-.row) | ||
|
||
pred | ||
} | ||
|
||
#' @export | ||
predict_class._lognet <- predict_class_glmnet | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A note to help speed up @topepo's review:
format_glmnet_multi_logistic_reg()
used to take whichevertype
was passed along to glmnet, and its internals read:That helper is now supplied the "parsnip" type instead, in this case one of
"class"
or"prob"
, so that the above conditional has only one possible value for theelse
. Theformat_glmnet_multi_multinom_reg()
helper does this already.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@simonpcouch Thanks for adding this! 🙌