Bundling a model prepares it to be saved to a file and later restored for prediction in a new R session. See the 'Value' section for more information on bundles and their usage.
Usage
# S3 method for class 'bart'
bundle(x, ...)
Arguments
- x
A
bart
object returned fromdbarts::bart()
. Notably, this ought not to be the output ofparsnip::bart()
.- ...
Not used in this bundler and included for compatibility with the generic only. Additional arguments passed to this method will return an error.
Value
A bundle object with subclass bundled_bart
.
Bundles are a list subclass with two components:
- object
An R object. Gives the output of native serialization methods from the model-supplying package, sometimes with additional classes or attributes that aid portability. This is often a raw object.
- situate
A function. The
situate()
function is defined whenbundle()
is called, though is a loose analogue of anunbundle()
S3 method for that object. Since the function is defined onbundle()
, it has access to references and dependency information that can be saved alongside theobject
component. Callingunbundle()
on a bundled objectx
callsx$situate(x$object)
, returning the unserialized version ofobject
.situate()
will also restore needed references, such as server instances and environmental variables.
Bundles are R objects that represent a "standalone" version of their
analogous model object. Thus, bundles are ready for saving to a file; saving
with base::saveRDS()
is our recommended serialization strategy for bundles,
unless documented otherwise for a specific method.
To restore the original model object x
in a new environment, load its
bundle with base::readRDS()
and run unbundle()
on it. The output
of unbundle()
is a model object that is ready to predict()
on new data,
and other restored functionality (like plotting or summarizing) is supported
as a side effect only.
The bundle package wraps native serialization methods from model-supplying packages. Between versions, those model-supplying packages may change their native serialization methods, possibly introducing problems with re-loading objects serialized with previous package versions. The bundle package does not provide checks for these sorts of changes, and ought to be used in conjunction with tooling for managing and monitoring model environments like vetiver or renv.
See vignette("bundle")
for more information on bundling and its motivation.
bundle and butcher
The butcher package allows you to remove parts of a fitted model object that are not needed for prediction.
This bundle method is compatible with pre-butchering. That is, for a
fitted model x
, you can safely call:
and predict with the output of unbundle(res)
in a new R session.
See also
Other bundlers:
bundle()
,
bundle.H2OAutoML()
,
bundle.keras.engine.training.Model()
,
bundle.luz_module_fitted()
,
bundle.model_fit()
,
bundle.model_stack()
,
bundle.recipe()
,
bundle.step_umap()
,
bundle.train()
,
bundle.workflow()
,
bundle.xgb.Booster()
Examples
# fit model and bundle ------------------------------------------------
library(dbarts)
mtcars$vs <- as.factor(mtcars$vs)
set.seed(1)
fit <- dbarts::bart(mtcars[c("disp", "hp")], mtcars$vs, keeptrees = TRUE)
#>
#> Running BART with binary y
#>
#> number of trees: 200
#> number of chains: 1, number of threads 1
#> tree thinning rate: 1
#> Prior:
#> k prior fixed to 2.000000
#> power and base for tree prior: 2.000000 0.950000
#> use quantiles for rule cut points: false
#> proposal probabilities: birth/death 0.50, swap 0.10, change 0.40; birth 0.50
#> data:
#> number of training observations: 32
#> number of test observations: 0
#> number of explanatory variables: 2
#>
#> Cutoff rules c in x<=c vs x>c
#> Number of cutoffs: (var: number of possible c):
#> (1: 100) (2: 100)
#> offsets:
#> reg : 0.00 0.00 0.00 0.00 0.00
#> Running mcmc loop:
#> iteration: 100 (of 1000)
#> iteration: 200 (of 1000)
#> iteration: 300 (of 1000)
#> iteration: 400 (of 1000)
#> iteration: 500 (of 1000)
#> iteration: 600 (of 1000)
#> iteration: 700 (of 1000)
#> iteration: 800 (of 1000)
#> iteration: 900 (of 1000)
#> iteration: 1000 (of 1000)
#> total seconds in loop: 0.277682
#>
#> Tree sizes, last iteration:
#> [1] 2 2 2 2 2 2 3 3 2 2 3 2 2 2 3 2 3 2
#> 2 2 3 5 2 3 2 3 2 1 2 3 2 3 2 2 3 2 2 3
#> 3 2 2 2 2 2 2 2 3 1 2 2 2 2 2 2 2 3 3 1
#> 2 3 2 2 1 2 2 3 2 2 2 2 2 3 2 2 2 1 2 2
#> 3 3 2 3 3 2 2 5 2 2 2 2 2 2 2 2 5 2 2 2
#> 2 2 2 2 2 2 3 1 2 2 2 2 2 2 2 2 2 1 2 2
#> 2 2 2 3 2 2 2 2 2 3 3 3 4 3 2 3 1 2 3 2
#> 2 2 3 2 3 2 3 2 2 3 2 2 2 2 2 2 2 2 3 2
#> 2 2 2 2 3 2 3 2 2 2 3 2 1 2 2 2 2 3 2 3
#> 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 4 2 2 2
#> 3 2
#>
#> Variable Usage, last iteration (var:count):
#> (1: 122) (2: 124)
#> DONE BART
#>
fit_bundle <- bundle(fit)
# then, after saveRDS + readRDS or passing to a new session ----------
fit_unbundled <- unbundle(fit_bundle)
fit_unbundled_preds <- predict(fit_unbundled, mtcars)