-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #344 from tidymodels/tidy-glmnet
better tidy glmnet methods
- Loading branch information
Showing
10 changed files
with
174 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#' tidy methods for glmnet models | ||
#' | ||
#' `tidy()` methods for the various `glmnet` models that return the coefficients | ||
#' for the specific penalty value used by the `parsnip` model fit. | ||
#' @param x A fitted `parsnip` model that used the `glmnet` engine. | ||
#' @param penalty A _single_ numeric value. If none is given, the value specified | ||
#' in the model specification is used. | ||
#' @param ... Not used | ||
#' @return A tibble with columns `term`, `estimate`, and `penalty`. When a | ||
#' multinomial mode is used, an additional `class` column is included. | ||
#' @importFrom stats coef | ||
#' @export | ||
tidy._elnet <- function(x, penalty = NULL, ...) { | ||
tidy_glmnet(x, penalty) | ||
} | ||
|
||
#' @export | ||
#' @rdname tidy._elnet | ||
tidy._lognet <- function(x, penalty = NULL, ...) { | ||
tidy_glmnet(x, penalty) | ||
} | ||
|
||
#' @export | ||
#' @rdname tidy._elnet | ||
tidy._multnet <- function(x, penalty = NULL, ...) { | ||
tidy_glmnet(x, penalty) | ||
} | ||
|
||
#' @export | ||
#' @rdname tidy._elnet | ||
tidy._fishnet <- function(x, penalty = NULL, ...) { | ||
tidy_glmnet(x, penalty) | ||
} | ||
|
||
## ----------------------------------------------------------------------------- | ||
|
||
get_glmn_coefs <- function(x, penalty = 0.01) { | ||
res <- coef(x, s = penalty) | ||
res <- as.matrix(res) | ||
colnames(res) <- "estimate" | ||
rn <- rownames(res) | ||
res <- tibble::as_tibble(res) %>% mutate(term = rn, penalty = penalty) | ||
res <- dplyr::select(res, term, estimate, penalty) | ||
if (is.list(res$estimate)) { | ||
res$estimate <- purrr::map(res$estimate, ~ as_tibble(as.matrix(.x), rownames = "term")) | ||
res <- tidyr::unnest(res, cols = c(estimate), names_repair = "minimal") | ||
names(res) <- c("class", "term", "estimate", "penalty") | ||
} | ||
res | ||
} | ||
|
||
tidy_glmnet <- function(x, penalty = NULL, ...) { | ||
check_installs(x$spec) | ||
load_libs(x$spec, quiet = TRUE, attach = TRUE) | ||
if (is.null(penalty)) { | ||
if (isTRUE(is.numeric(x$spec$args$penalty))){ | ||
penalty <- x$spec$args$penalty | ||
} else { | ||
rlang::abort("Please pick a single value of `penalty`.") | ||
} | ||
} | ||
get_glmn_coefs(x$fit, penalty = penalty) | ||
} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
context("tidy glmnet models") | ||
|
||
test_that('linear regression', { | ||
skip_if_not_installed("glmnet") | ||
|
||
ps_mod <- | ||
linear_reg(penalty = .1) %>% | ||
set_engine("glmnet") %>% | ||
fit(mpg ~ ., data = mtcars) | ||
|
||
ps_coefs <- tidy(ps_mod) | ||
gn_coefs <- as.matrix(coef(ps_mod$fit, s = .1)) | ||
for(i in ps_coefs$term) { | ||
expect_equal(ps_coefs$estimate[ps_coefs$term == i], gn_coefs[i,1]) | ||
} | ||
}) | ||
|
||
test_that('logistic regression', { | ||
skip_if_not_installed("glmnet") | ||
|
||
data(two_class_dat, package = "modeldata") | ||
|
||
ps_mod <- | ||
logistic_reg(penalty = .1) %>% | ||
set_engine("glmnet") %>% | ||
fit(Class ~ ., data = two_class_dat) | ||
|
||
ps_coefs <- tidy(ps_mod) | ||
gn_coefs <- as.matrix(coef(ps_mod$fit, s = .1)) | ||
for(i in ps_coefs$term) { | ||
expect_equal(ps_coefs$estimate[ps_coefs$term == i], gn_coefs[i,1]) | ||
} | ||
}) | ||
|
||
test_that('multinomial regression', { | ||
skip_if_not_installed("glmnet") | ||
|
||
data(penguins, package = "modeldata") | ||
|
||
ps_mod <- | ||
multinom_reg(penalty = .01) %>% | ||
set_engine("glmnet") %>% | ||
fit(species ~ ., data = penguins) | ||
|
||
ps_coefs <- tidy(ps_mod) | ||
gn_coefs <- coef(ps_mod$fit, s = .01) | ||
gn_coefs <- purrr::map(gn_coefs, as.matrix) | ||
for(i in unique(ps_coefs$term)) { | ||
for(j in unique(ps_coefs$class)) { | ||
expect_equal( | ||
ps_coefs$estimate[ps_coefs$term == i & ps_coefs$class == j], | ||
gn_coefs[[j]][i,1] | ||
) | ||
} | ||
} | ||
}) | ||
|
||
|