From d225cb041e1deb6fe193bb2e04e66e0ca39d9fc0 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Mon, 8 Jul 2024 11:37:19 -0700 Subject: [PATCH 1/3] add predict --- DESCRIPTION | 2 +- NAMESPACE | 2 +- NEWS.md | 2 ++ R/mutate.R | 34 ------------------ R/predict.R | 17 ++++----- README.Rmd | 6 ++-- README.md | 33 ++++++++++-------- man/orbital_predict.Rd | 65 ----------------------------------- man/predict.Rd | 41 ++++++++++++++++++++++ tests/testthat/test-predict.R | 2 +- 10 files changed, 76 insertions(+), 128 deletions(-) delete mode 100644 R/mutate.R delete mode 100644 man/orbital_predict.Rd create mode 100644 man/predict.Rd diff --git a/DESCRIPTION b/DESCRIPTION index b4af4b3..4ec0b6e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,4 +28,4 @@ Suggests: Config/testthat/edition: 3 Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1.9000 +RoxygenNote: 7.3.2 diff --git a/NAMESPACE b/NAMESPACE index 9941eb4..5ac1b08 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,6 +22,6 @@ export(orbital_dt) export(orbital_inline) export(orbital_json_read) export(orbital_json_write) -export(orbital_predict) export(orbital_sql) +export(predict) export(to_r_fun) diff --git a/NEWS.md b/NEWS.md index f824020..68ce62a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # orbital (development version) +* `orbital_predict()` has been removed and replaced with the more idiomatic `predict()` method. (#10) + # orbital 0.1.0 * Initial CRAN submission. diff --git a/R/mutate.R b/R/mutate.R deleted file mode 100644 index a5cf29c..0000000 --- a/R/mutate.R +++ /dev/null @@ -1,34 +0,0 @@ -#' Use orbital in a mutate way -#' -#' @param .data A data frame that can be used with mutate. -#' @param x A orbital object. -#' -#' @returns A modified data frame. -#' -#' @examplesIf rlang::is_installed(c("recipes", "tidypredict", "workflows")) -#' library(workflows) -#' library(recipes) -#' library(parsnip) -#' -#' rec_spec <- recipe(mpg ~ ., data = mtcars) %>% -#' step_normalize(all_numeric_predictors()) -#' -#' lm_spec <- linear_reg() -#' -#' wf_spec <- workflow(rec_spec, lm_spec) -#' -#' wf_fit <- fit(wf_spec, mtcars) -#' -#' orbital_obj <- orbital(wf_fit) -#' -#' mtcars %>% -#' orbital_predict(orbital_obj) -#' @export -orbital_predict <- function(.data, x) { - pred_name <- names(x)[length(x)] - - res <- dplyr::mutate(.data, !!!orbital_inline(x)) - res <- dplyr::select(res, dplyr::any_of(pred_name)) - - res -} diff --git a/R/predict.R b/R/predict.R index c184d27..7d10267 100644 --- a/R/predict.R +++ b/R/predict.R @@ -1,7 +1,8 @@ -#' Use orbital in a mutate way +#' Prediction using orbital objects #' -#' @param .data A data frame that can be used with mutate. -#' @param x A orbital object. +#' @param object A orbital object. +#' @param new_data A data frame to predict with. +#' @param ... Not currently used. #' #' @returns A modified data frame. #' @@ -21,13 +22,13 @@ #' #' orbital_obj <- orbital(wf_fit) #' -#' mtcars %>% -#' orbital_predict(orbital_obj) +#' predict(orbital_obj, mtcars) #' @export -orbital_predict <- function(.data, x) { - res <- dplyr::mutate(.data, !!!orbital_inline(x)) +predict <- function(object, new_data, ...) { + rlang::check_dots_empty() + res <- dplyr::mutate(new_data, !!!orbital_inline(object)) - pred_name <- names(x)[length(x)] + pred_name <- names(object)[length(object)] res <- dplyr::select(res, dplyr::any_of(pred_name)) res diff --git a/README.Rmd b/README.Rmd index 87aa71b..9ea8015 100644 --- a/README.Rmd +++ b/README.Rmd @@ -66,10 +66,8 @@ orbital_obj <- orbital(wf_fit) orbital_obj ``` -and then "predicting" with it using `orbital_predict()` to get the same results +and then "predicting" with it using `predict()` to get the same results ```{r} -mtcars %>% - as_tibble() %>% - orbital_predict(orbital_obj) +predict(orbital_obj, as_tibble(mtcars)) ``` \ No newline at end of file diff --git a/README.md b/README.md index 168c8e0..31cc281 100644 --- a/README.md +++ b/README.md @@ -32,20 +32,20 @@ Given a fitted workflow ``` r library(tidymodels) #> ── Attaching packages ────────────────────────────────────── tidymodels 1.2.0 ── -#> ✔ broom 1.0.6 ✔ recipes 1.0.10 -#> ✔ dials 1.2.1 ✔ rsample 1.2.1 -#> ✔ dplyr 1.1.4 ✔ tibble 3.2.1 -#> ✔ ggplot2 3.5.1 ✔ tidyr 1.3.1 -#> ✔ infer 1.0.7 ✔ tune 1.2.1 -#> ✔ modeldata 1.4.0 ✔ workflows 1.1.4 -#> ✔ parsnip 1.2.1 ✔ workflowsets 1.1.0 -#> ✔ purrr 1.0.2 ✔ yardstick 1.3.1 +#> ✔ broom 1.0.6 ✔ recipes 1.1.0.9000 +#> ✔ dials 1.2.1 ✔ rsample 1.2.1 +#> ✔ dplyr 1.1.4 ✔ tibble 3.2.1 +#> ✔ ggplot2 3.5.1 ✔ tidyr 1.3.1 +#> ✔ infer 1.0.7 ✔ tune 1.2.1 +#> ✔ modeldata 1.4.0 ✔ workflows 1.1.4 +#> ✔ parsnip 1.2.1 ✔ workflowsets 1.1.0 +#> ✔ purrr 1.0.2 ✔ yardstick 1.3.1 #> ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ── #> ✖ purrr::discard() masks scales::discard() #> ✖ dplyr::filter() masks stats::filter() #> ✖ dplyr::lag() masks stats::lag() #> ✖ recipes::step() masks stats::step() -#> • Dig deeper into tidy modeling with R at https://www.tmwr.org +#> • Use tidymodels_prefer() to resolve common conflicts. ``` ``` r @@ -84,6 +84,14 @@ We can get the same results by first creating an orbital object ``` r library(orbital) +#> +#> Attaching package: 'orbital' +#> The following object is masked from 'package:stats': +#> +#> predict +``` + +``` r orbital_obj <- orbital(wf_fit) orbital_obj #> @@ -103,13 +111,10 @@ orbital_obj #> 11 equations in total. ``` -and then “predicting” with it using `orbital_predict()` to get the same -results +and then “predicting” with it using `predict()` to get the same results ``` r -mtcars %>% - as_tibble() %>% - orbital_predict(orbital_obj) +predict(orbital_obj, as_tibble(mtcars)) #> # A tibble: 32 × 1 #> .pred #> diff --git a/man/orbital_predict.Rd b/man/orbital_predict.Rd deleted file mode 100644 index c8e764e..0000000 --- a/man/orbital_predict.Rd +++ /dev/null @@ -1,65 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/mutate.R, R/predict.R -\name{orbital_predict} -\alias{orbital_predict} -\title{Use orbital in a mutate way} -\usage{ -orbital_predict(.data, x) - -orbital_predict(.data, x) -} -\arguments{ -\item{.data}{A data frame that can be used with mutate.} - -\item{x}{A orbital object.} -} -\value{ -A modified data frame. - -A modified data frame. -} -\description{ -Use orbital in a mutate way - -Use orbital in a mutate way -} -\examples{ -\dontshow{if (rlang::is_installed(c("recipes", "tidypredict", "workflows"))) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -library(workflows) -library(recipes) -library(parsnip) - -rec_spec <- recipe(mpg ~ ., data = mtcars) \%>\% - step_normalize(all_numeric_predictors()) - -lm_spec <- linear_reg() - -wf_spec <- workflow(rec_spec, lm_spec) - -wf_fit <- fit(wf_spec, mtcars) - -orbital_obj <- orbital(wf_fit) - -mtcars \%>\% - orbital_predict(orbital_obj) -\dontshow{\}) # examplesIf} -\dontshow{if (rlang::is_installed(c("recipes", "tidypredict", "workflows"))) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -library(workflows) -library(recipes) -library(parsnip) - -rec_spec <- recipe(mpg ~ ., data = mtcars) \%>\% - step_normalize(all_numeric_predictors()) - -lm_spec <- linear_reg() - -wf_spec <- workflow(rec_spec, lm_spec) - -wf_fit <- fit(wf_spec, mtcars) - -orbital_obj <- orbital(wf_fit) - -mtcars \%>\% - orbital_predict(orbital_obj) -\dontshow{\}) # examplesIf} -} diff --git a/man/predict.Rd b/man/predict.Rd new file mode 100644 index 0000000..3b9f70c --- /dev/null +++ b/man/predict.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/predict.R +\name{predict} +\alias{predict} +\title{Prediction using orbital objects} +\usage{ +predict(object, new_data, ...) +} +\arguments{ +\item{object}{A orbital object.} + +\item{new_data}{A data frame to predict with.} + +\item{...}{Not currently used.} +} +\value{ +A modified data frame. +} +\description{ +Prediction using orbital objects +} +\examples{ +\dontshow{if (rlang::is_installed(c("recipes", "tidypredict", "workflows"))) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +library(workflows) +library(recipes) +library(parsnip) + +rec_spec <- recipe(mpg ~ ., data = mtcars) \%>\% + step_normalize(all_numeric_predictors()) + +lm_spec <- linear_reg() + +wf_spec <- workflow(rec_spec, lm_spec) + +wf_fit <- fit(wf_spec, mtcars) + +orbital_obj <- orbital(wf_fit) + +predict(orbital_obj, mtcars) +\dontshow{\}) # examplesIf} +} diff --git a/tests/testthat/test-predict.R b/tests/testthat/test-predict.R index 4081fb4..90cd962 100644 --- a/tests/testthat/test-predict.R +++ b/tests/testthat/test-predict.R @@ -16,7 +16,7 @@ test_that("orbital works with workflows - recipe", { obj <- orbital(wf_fit) expect_identical( - mtcars %>% orbital_predict(obj), + predict(obj, mtcars), mtcars %>% dplyr::mutate(!!!orbital_inline(obj)) %>% dplyr::select(.pred) ) }) From 12c064ae05b9299c54938e518d8c7bfe0fc65b63 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Mon, 8 Jul 2024 11:41:56 -0700 Subject: [PATCH 2/3] actually use s3 predict... --- NAMESPACE | 2 +- R/predict.R | 2 +- man/{predict.Rd => predict.orbital_class.Rd} | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) rename man/{predict.Rd => predict.orbital_class.Rd} (88%) diff --git a/NAMESPACE b/NAMESPACE index 5ac1b08..2ff0e79 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,6 +16,7 @@ S3method(orbital,step_scale) S3method(orbital,step_select) S3method(orbital,step_zv) S3method(orbital,workflow) +S3method(predict,orbital_class) S3method(print,orbital_class) export(orbital) export(orbital_dt) @@ -23,5 +24,4 @@ export(orbital_inline) export(orbital_json_read) export(orbital_json_write) export(orbital_sql) -export(predict) export(to_r_fun) diff --git a/R/predict.R b/R/predict.R index 7d10267..39210d6 100644 --- a/R/predict.R +++ b/R/predict.R @@ -24,7 +24,7 @@ #' #' predict(orbital_obj, mtcars) #' @export -predict <- function(object, new_data, ...) { +predict.orbital_class <- function(object, new_data, ...) { rlang::check_dots_empty() res <- dplyr::mutate(new_data, !!!orbital_inline(object)) diff --git a/man/predict.Rd b/man/predict.orbital_class.Rd similarity index 88% rename from man/predict.Rd rename to man/predict.orbital_class.Rd index 3b9f70c..e3d18f6 100644 --- a/man/predict.Rd +++ b/man/predict.orbital_class.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/predict.R -\name{predict} -\alias{predict} +\name{predict.orbital_class} +\alias{predict.orbital_class} \title{Prediction using orbital objects} \usage{ -predict(object, new_data, ...) +\method{predict}{orbital_class}(object, new_data, ...) } \arguments{ \item{object}{A orbital object.} From 330e7c22fba89e05fecf29fff3c26836dde41b18 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Mon, 8 Jul 2024 11:45:27 -0700 Subject: [PATCH 3/3] reknit readme --- README.Rmd | 1 + README.md | 26 -------------------------- 2 files changed, 1 insertion(+), 26 deletions(-) diff --git a/README.Rmd b/README.Rmd index 9ea8015..b0d7252 100644 --- a/README.Rmd +++ b/README.Rmd @@ -40,6 +40,7 @@ devtools::install_github("tidymodels/orbital") Given a fitted workflow ```{r} +#| message: false library(tidymodels) rec_spec <- recipe(mpg ~ ., data = mtcars) %>% diff --git a/README.md b/README.md index 31cc281..d1efd9f 100644 --- a/README.md +++ b/README.md @@ -31,24 +31,6 @@ Given a fitted workflow ``` r library(tidymodels) -#> ── Attaching packages ────────────────────────────────────── tidymodels 1.2.0 ── -#> ✔ broom 1.0.6 ✔ recipes 1.1.0.9000 -#> ✔ dials 1.2.1 ✔ rsample 1.2.1 -#> ✔ dplyr 1.1.4 ✔ tibble 3.2.1 -#> ✔ ggplot2 3.5.1 ✔ tidyr 1.3.1 -#> ✔ infer 1.0.7 ✔ tune 1.2.1 -#> ✔ modeldata 1.4.0 ✔ workflows 1.1.4 -#> ✔ parsnip 1.2.1 ✔ workflowsets 1.1.0 -#> ✔ purrr 1.0.2 ✔ yardstick 1.3.1 -#> ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ── -#> ✖ purrr::discard() masks scales::discard() -#> ✖ dplyr::filter() masks stats::filter() -#> ✖ dplyr::lag() masks stats::lag() -#> ✖ recipes::step() masks stats::step() -#> • Use tidymodels_prefer() to resolve common conflicts. -``` - -``` r rec_spec <- recipe(mpg ~ ., data = mtcars) %>% step_normalize(all_numeric_predictors()) @@ -84,14 +66,6 @@ We can get the same results by first creating an orbital object ``` r library(orbital) -#> -#> Attaching package: 'orbital' -#> The following object is masked from 'package:stats': -#> -#> predict -``` - -``` r orbital_obj <- orbital(wf_fit) orbital_obj #>