Title: | Predict with 'tidymodels' Workflows in Databases |
---|---|
Description: | Turn 'tidymodels' workflows into objects containing the sufficient sequential equations to perform predictions. These smaller objects allow for low dependency prediction locally or directly in databases. |
Authors: | Emil Hvitfeldt [aut, cre], Posit Software, PBC [cph, fnd] |
Maintainer: | Emil Hvitfeldt <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.0.9000 |
Built: | 2024-10-29 17:15:52 UTC |
Source: | https://github.com/tidymodels/orbital |
augment()
will add column(s) for predictions to the given data.
## S3 method for class 'orbital_class' augment(x, new_data, ...)
## S3 method for class 'orbital_class' augment(x, new_data, ...)
x |
An orbital object. |
new_data |
A data frame or remote database table. |
... |
Not currently used. |
This function is a shorthand for the following code
dplyr::bind_cols( predict(orbital_obj, new_data), new_data )
Note that augment()
works better and safer than above as it also works on
data set in data bases.
This function is confirmed to not work work in spark data bases or arrow tables.
A modified data frame or remote database table.
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) augment(orbital_obj, mtcars)
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) augment(orbital_obj, mtcars)
Fitted workflows, parsnip objects, and recipes objects can be turned into an orbital object that contain all the information needed to perform predictions.
orbital(x, ..., prefix = ".pred")
orbital(x, ..., prefix = ".pred")
x |
A fitted workflow, parsnip, or recipes object. |
... |
Not currently used. |
prefix |
A single string, specifies the prediction naming scheme.
If |
An orbital object contains all the information that is needed to perform predictions. This makes the objects substantially smaller than the original objects. The main downside with this object is that all the input checking has been removed, and it is thus up to the user to make sure the data is correct.
The printing of orbital objects reduce the number of significant digits for
easy viewing, the can be changes by using the digits
argument of print()
like so print(orbital_object, digits = 10)
. The printing likewise truncates
each equation to fit on one line. This can be turned off using the truncate
argument like so print(orbital_object, truncate = FALSE)
.
Full list of supported models and recipes steps can be found here:
vignette("supported-models")
.
These objects will not be useful by themselves. They can be used to
predict() with, or to generate code using functions
such as orbital_sql()
or orbital_dt()
.
An orbital object.
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(wf_fit) # Also works on parsnip object by itself fit(lm_spec, mpg ~ disp, data = mtcars) %>% orbital() # And prepped recipes prep(rec_spec) %>% orbital()
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(wf_fit) # Also works on parsnip object by itself fit(lm_spec, mpg ~ disp, data = mtcars) %>% orbital() # And prepped recipes prep(rec_spec) %>% orbital()
Returns data.table code that is equivilant to prediction code.
orbital_dt(x)
orbital_dt(x)
x |
An orbital object. This function requires dtplyr to be
installed to run. The resulting code will likely need to be adopted to your
use-case. Most likely by removing the initial |
data.table code.
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) orbital_dt(orbital_obj)
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) orbital_dt(orbital_obj)
Use orbital object splicing function to apply orbital prediction in a quosure
aware function such as dplyr::mutate()
.
orbital_inline(x)
orbital_inline(x)
x |
An orbital object. |
This function is mostly going to be used for
Dots Injection.
This function is used internally in predict(), but
is also exported for user flexibility. Should be used with !!!
as seen in
the examples.
Note should be taken that using this function modifies existing variables and creates new variables, unlike predict() which only returns predictions.
a list of quosures.
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) orbital_inline(orbital_obj) library(dplyr) mtcars %>% mutate(!!!orbital_inline(orbital_obj))
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) orbital_inline(orbital_obj) library(dplyr) mtcars %>% mutate(!!!orbital_inline(orbital_obj))
Reading an orbital object from disk
orbital_json_read(path)
orbital_json_read(path)
path |
file on disk. |
This function is aware of the version
field of the orbital object, and will
read it in correctly, according to its specification.
An orbital object.
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) tmp_file <- tempfile() orbital_json_write(orbital_obj, tmp_file) orbital_json_read(tmp_file)
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) tmp_file <- tempfile() orbital_json_write(orbital_obj, tmp_file) orbital_json_read(tmp_file)
Saving an orbital object to disk in a human and machine readable way.
orbital_json_write(x, path)
orbital_json_write(x, path)
x |
An orbital object. |
path |
file on disk. |
The structure of the resulting JSON file allows for easy reading, both by
orbital itself with orbital_json_read()
, but potentially by other packages
and langauges. The file is versioned by the version
field to allow for
changes why being backwards combatible with older file versions.
Nothing.
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) tmp_file <- tempfile() orbital_json_write(orbital_obj, tmp_file) readLines(tmp_file)
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) tmp_file <- tempfile() orbital_json_write(orbital_obj, tmp_file) readLines(tmp_file)
Returns a R file that contains a function that output predictions when applied to data frames.
orbital_r_fun(x, name = "orbital_predict", file)
orbital_r_fun(x, name = "orbital_predict", file)
x |
An orbital object. |
name |
Name of created function. Defaults to '"orbital_predict"“. |
file |
A file name. |
The generated function is only expected to work on data frame objects. The generated function doesn't require the orbital package to be loaded. Depending on what models and steps are used, other packages such as dplyr will need to be loaded as well.
Nothing.
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) file_name <- tempfile() orbital_r_fun(orbital_obj, file = file_name) readLines(file_name)
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) file_name <- tempfile() orbital_r_fun(orbital_obj, file = file_name) readLines(file_name)
Returns SQL code that is equivilant to prediction code.
orbital_sql(x, con)
orbital_sql(x, con)
x |
An orbital object. |
con |
A connection object. |
This function requires a database connection object, as the resulting code SQL code can differ depending on the type of database.
SQL code.
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) library(dbplyr) con <- simulate_dbi() orbital_sql(orbital_obj, con)
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) library(dbplyr) con <- simulate_dbi() orbital_sql(orbital_obj, con)
Running prediction on data frame of remote database table, without needing to load original packages used to fit model.
## S3 method for class 'orbital_class' predict(object, new_data, ...)
## S3 method for class 'orbital_class' predict(object, new_data, ...)
object |
An orbital object. |
new_data |
A data frame or remote database table. |
... |
Not currently used. |
Using this function should give identical results to running predict()
or
bake()
on the orginal object.
The prediction done will only return prediction colunms, a opposed to
returning all modified functions as done with orbital_inline()
.
A modified data frame or remote database table.
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)
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)