Skip to content

Commit

Permalink
Merge pull request #10 from tidymodels/add-predict
Browse files Browse the repository at this point in the history
Add predict
  • Loading branch information
EmilHvitfeldt authored Jul 8, 2024
2 parents f1e2f0b + 330e7c2 commit e3c1466
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 137 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ 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)
export(orbital_inline)
export(orbital_json_read)
export(orbital_json_write)
export(orbital_predict)
export(orbital_sql)
export(to_r_fun)
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
34 changes: 0 additions & 34 deletions R/mutate.R

This file was deleted.

17 changes: 9 additions & 8 deletions R/predict.R
Original file line number Diff line number Diff line change
@@ -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.
#'
Expand All @@ -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.orbital_class <- 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
Expand Down
7 changes: 3 additions & 4 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ devtools::install_github("tidymodels/orbital")
Given a fitted workflow

```{r}
#| message: false
library(tidymodels)
rec_spec <- recipe(mpg ~ ., data = mtcars) %>%
Expand All @@ -66,10 +67,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))
```
25 changes: 2 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,6 @@ 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
#> ── 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
```

``` r

rec_spec <- recipe(mpg ~ ., data = mtcars) %>%
step_normalize(all_numeric_predictors())
Expand Down Expand Up @@ -103,13 +85,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
#> <dbl>
Expand Down
65 changes: 0 additions & 65 deletions man/orbital_predict.Rd

This file was deleted.

41 changes: 41 additions & 0 deletions man/predict.orbital_class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
})

0 comments on commit e3c1466

Please sign in to comment.