-
Notifications
You must be signed in to change notification settings - Fork 29
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 #75 from tidymodels/add-glm
Add vetiver support for `glm()`
- Loading branch information
Showing
12 changed files
with
148 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#' @rdname vetiver_create_description | ||
#' @export | ||
vetiver_create_description.glm <- function(model) { | ||
glue("A generalized linear model ({model$family$family} family, {model$family$link} link)") | ||
} | ||
|
||
#' @rdname vetiver_create_description | ||
#' @export | ||
vetiver_prepare_model.glm <- function(model) { | ||
butcher::butcher(model) | ||
} | ||
|
||
#' @rdname vetiver_create_ptype | ||
#' @export | ||
vetiver_ptype.glm <- function(model, ...) { | ||
vetiver_ptype.lm(model, ...) | ||
} | ||
|
||
#' @rdname handler_startup | ||
#' @export | ||
handler_predict.glm <- function(vetiver_model, ...) { | ||
handler_predict.lm(vetiver_model, ...) | ||
} |
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
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.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# can print glm model | ||
|
||
Code | ||
v | ||
Output | ||
-- cars_glm - <butchered_glm> model for deployment | ||
A generalized linear model (gaussian family, identity link) using 10 features | ||
|
||
# create plumber.R for xgboost | ||
|
||
Code | ||
cat(readr::read_lines(tmp), sep = "\n") | ||
Output | ||
# Generated by the vetiver package; edit with care | ||
library(pins) | ||
library(plumber) | ||
library(rapidoc) | ||
library(vetiver) | ||
b <- board_folder(path = "/tmp/test") | ||
v <- vetiver_pin_read(b, "cars_glm") | ||
#* @plumber | ||
function(pr) { | ||
pr %>% vetiver_pr_predict(v) | ||
} | ||
|
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,64 @@ | ||
library(pins) | ||
library(plumber) | ||
|
||
mtcars_glm <- glm(mpg ~ ., data = mtcars) | ||
v <- vetiver_model(mtcars_glm, "cars_glm") | ||
|
||
test_that("can print glm model", { | ||
expect_snapshot(v) | ||
}) | ||
|
||
test_that("can predict glm model", { | ||
preds <- predict(v, mtcars) | ||
expect_type(preds, "double") | ||
expect_equal(mean(preds), 20.1, tolerance = 0.1) | ||
}) | ||
|
||
test_that("can pin a glm model", { | ||
b <- board_temp() | ||
vetiver_pin_write(b, v) | ||
pinned <- pin_read(b, "cars_glm") | ||
expect_equal( | ||
pinned, | ||
list( | ||
model = butcher::butcher(mtcars_glm), | ||
ptype = vctrs::vec_slice(tibble::as_tibble(mtcars[,2:11]), 0), | ||
required_pkgs = NULL | ||
), | ||
ignore_function_env = TRUE, | ||
ignore_formula_env = TRUE | ||
) | ||
}) | ||
|
||
test_that("default endpoint for glm", { | ||
p <- pr() %>% vetiver_pr_predict(v) | ||
expect_equal(names(p$routes), c("ping", "predict")) | ||
expect_equal(map_chr(p$routes, "verbs"), | ||
c(ping = "GET", predict = "POST")) | ||
}) | ||
|
||
test_that("default OpenAPI spec", { | ||
v$metadata <- list(url = "potatoes") | ||
p <- pr() %>% vetiver_pr_predict(v) | ||
car_spec <- p$getApiSpec() | ||
expect_equal(car_spec$info$description, | ||
"A generalized linear model (gaussian family, identity link)") | ||
post_spec <- car_spec$paths$`/predict`$post | ||
expect_equal(names(post_spec), c("summary", "requestBody", "responses")) | ||
expect_equal(as.character(post_spec$summary), | ||
"Return predictions from model using 10 features") | ||
get_spec <- car_spec$paths$`/pin-url`$get | ||
expect_equal(as.character(get_spec$summary), | ||
"Get URL of pinned vetiver model") | ||
|
||
}) | ||
|
||
test_that("create plumber.R for xgboost", { | ||
skip_on_cran() | ||
b <- board_folder(path = "/tmp/test") | ||
vetiver_pin_write(b, v) | ||
tmp <- tempfile() | ||
vetiver_write_plumber(b, "cars_glm", file = tmp) | ||
expect_snapshot(cat(readr::read_lines(tmp), sep = "\n")) | ||
}) | ||
|
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