Skip to content

Commit ecb2a8e

Browse files
authored
Merge pull request #474 from tidymodels/liblinear-tidy
Add `tidy` method for LiblineaR parsnip models
2 parents d48be86 + a3ef9bc commit ecb2a8e

File tree

6 files changed

+54
-1
lines changed

6 files changed

+54
-1
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ S3method(req_pkgs,model_fit)
6666
S3method(req_pkgs,model_spec)
6767
S3method(required_pkgs,model_fit)
6868
S3method(required_pkgs,model_spec)
69+
S3method(tidy,"_LiblineaR")
6970
S3method(tidy,"_elnet")
7071
S3method(tidy,"_fishnet")
7172
S3method(tidy,"_lognet")

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
* The `liquidSVM` engine for `svm_rbf()` was deprecated due to that package's removal from CRAN. (#425)
66

7-
* A new linear SVM model `svm_linear()` is now available with the `LiblineaR` engine (#424) and the `kernlab` engine (#438), and the `LiblineaR` engine is available for `logistic_reg()` as well (#429). These models can use sparse matrices via `fit_xy()` (#447).
7+
* A new linear SVM model `svm_linear()` is now available with the `LiblineaR` engine (#424) and the `kernlab` engine (#438), and the `LiblineaR` engine is available for `logistic_reg()` as well (#429). These models can use sparse matrices via `fit_xy()` (#447) and have a `tidy` method (#474).
88

99
* New model specification `survival_reg()` for the new mode `"censored regression"` (#444). `surv_reg()` is now soft-deprecated (#448).
1010

R/tidy_liblinear.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#' tidy methods for LiblineaR models
2+
#'
3+
#' `tidy()` methods for the various `LiblineaR` models that return the
4+
#' coefficients from the `parsnip` model fit.
5+
#' @param x A fitted `parsnip` model that used the `LiblineaR` engine.
6+
#' @param ... Not used
7+
#' @return A tibble with columns `term` and `estimate`.
8+
#' @keywords internal
9+
#' @export
10+
11+
tidy._LiblineaR <- function(x, ...) {
12+
check_installs(x$spec)
13+
ret <- tibble(colnames(x$fit$W), x$fit$W[1,])
14+
colnames(ret) <- c("term", "estimate")
15+
16+
ret
17+
}

man/tidy._LiblineaR.Rd

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_logistic_reg.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,13 @@ test_that('liblinear execution', {
448448
)
449449
)
450450

451+
expect_error(
452+
tidy_res <- tidy(res),
453+
NA
454+
)
455+
expect_s3_class(tidy_res, "tbl_df")
456+
expect_equal(colnames(tidy_res), c("term", "estimate"))
457+
451458
# wrong outcome type
452459
expect_error(
453460
glm_form_catch <- fit(

tests/testthat/test_svm_linear.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ test_that('linear svm regression: LiblineaR', {
143143
expect_equal(multi_predict_args(res), NA_character_)
144144
expect_output(print(res), "parsnip model object")
145145

146+
expect_error(
147+
tidy_res <- tidy(res),
148+
NA
149+
)
150+
expect_s3_class(tidy_res, "tbl_df")
151+
expect_equal(colnames(tidy_res), c("term", "estimate"))
152+
146153
expect_error(
147154
fit(
148155
reg_mod,

0 commit comments

Comments
 (0)