Title: | Model Butcher |
---|---|
Description: | Provides a set of S3 generics to axe components of fitted model objects and help reduce the size of model objects saved to disk. |
Authors: | Joyce Cahoon [aut] , Davis Vaughan [aut], Max Kuhn [aut], Alex Hayes [aut], Julia Silge [cre, aut] , Posit, PBC [cph, fnd] |
Maintainer: | Julia Silge <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.4.9000 |
Built: | 2024-10-25 05:19:31 UTC |
Source: | https://github.com/tidymodels/butcher |
Replace the call object attached to modeling objects with a placeholder.
axe_call(x, verbose = FALSE, ...)
axe_call(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Model object without call attribute.
See the following help topics for more details about individual methods:
butcher
axe-bart
: bart
axe-C5.0
: C5.0
axe-earth
: earth
axe-elnet
: elnet
axe-flexsurvreg
: flexsurvreg
axe-gam
: gam
axe-gausspr
: gausspr
axe-glm
: glm
axe-glmnet
: glmnet
axe-ipred
: classbagg
, regbagg
, survbagg
axe-kknn
: kknn
axe-KMeansCluster
: KMeansCluster
axe-ksvm
: ksvm
axe-lm
: lm
axe-mda
: fda
, mda
axe-model_fit
: model_fit
axe-multnet
: multnet
axe-NaiveBayes
: NaiveBayes
axe-nested_model_fit
: nested_model_fit
axe-nnet
: nnet
axe-pls
: mixo_pls
, mixo_spls
axe-randomForest
: randomForest
axe-ranger
: ranger
axe-rda
: rda
axe-rpart
: rpart
axe-sclass
: sclass
axe-spark
: ml_model
axe-survreg
: survreg
axe-survreg.penal
: survreg.penal
axe-train
: train
axe-train.recipe
: train.recipe
axe-xgb.Booster
: xgb.Booster
axe-xrf
: xrf
Remove the controls from training attached to modeling objects.
axe_ctrl(x, verbose = FALSE, ...)
axe_ctrl(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Model object without control tuning parameters from training.
See the following help topics for more details about individual methods:
butcher
axe-C5.0
: C5.0
axe-gam
: gam
axe-ipred
: classbagg
, regbagg
, survbagg
axe-model_fit
: model_fit
axe-nested_model_fit
: nested_model_fit
axe-randomForest
: randomForest
axe-rpart
: rpart
axe-spark
: ml_model
axe-train
: train
axe-train.recipe
: train.recipe
Remove the training data attached to modeling objects.
axe_data(x, verbose = FALSE, ...)
axe_data(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Model object without the training data
See the following help topics for more details about individual methods:
butcher
axe-coxph
: coxph
axe-earth
: earth
axe-gam
: gam
axe-gausspr
: gausspr
axe-glm
: glm
axe-ipred
: classbagg
, regbagg
, survbagg
axe-kproto
: kproto
axe-ksvm
: ksvm
axe-model_fit
: model_fit
axe-NaiveBayes
: NaiveBayes
axe-nested_model_fit
: nested_model_fit
axe-pls
: mixo_pls
, mixo_spls
axe-rpart
: rpart
axe-spark
: ml_model
axe-survreg
: survreg
axe-survreg.penal
: survreg.penal
axe-train
: train
axe-train.recipe
: train.recipe
Remove the environment(s) attached to modeling objects as they are
not required in the downstream analysis pipeline. If found,
the environment is replaced with rlang::base_env()
.
axe_env(x, verbose = FALSE, ...)
axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Model object with empty environments.
See the following help topics for more details about individual methods:
butcher
axe-coxph
: coxph
axe-flexsurvreg
: flexsurvreg
axe-formula
: formula
axe-function
: function
axe-gam
: gam
axe-gausspr
: gausspr
axe-glm
: glm
axe-ipred
: classbagg
, regbagg
, survbagg
axe-kknn
: kknn
axe-lm
: lm
axe-mass
: lda
, qda
axe-mda
: fda
, mda
axe-model_fit
: model_fit
axe-nested_model_fit
: nested_model_fit
axe-nnet
: nnet
axe-randomForest
: randomForest
axe-rda
: rda
axe-recipe
: quosure
, recipe
, step
, step_arrange
, step_bagimpute
, step_filter
, step_geodist
, step_impute_bag
, step_impute_knn
, step_interact
, step_knnimpute
, step_mutate
, step_ratio
, step_slice
axe-rpart
: rpart
axe-sclass
: sclass
axe-survreg
: survreg
axe-survreg.penal
: survreg.penal
axe-terms
: terms
axe-train
: train
axe-train.recipe
: train.recipe
axe-xgb.Booster
: xgb.Booster
axe-xrf
: xrf
Remove the fitted values attached to modeling objects.
axe_fitted(x, verbose = FALSE, ...)
axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Model object without the fitted values.
See the following help topics for more details about individual methods:
butcher
axe-bart
: bart
axe-C5.0
: C5.0
axe-earth
: earth
axe-gam
: gam
axe-gausspr
: gausspr
axe-glm
: glm
axe-kknn
: kknn
axe-KMeansCluster
: KMeansCluster
axe-kproto
: kproto
axe-ksvm
: ksvm
axe-lm
: lm
axe-mda
: fda
, mda
axe-model_fit
: model_fit
axe-nested_model_fit
: nested_model_fit
axe-nnet
: nnet
axe-pls
: mixo_pls
, mixo_spls
axe-ranger
: ranger
axe-recipe
: recipe
axe-spark
: ml_model
axe-train
: train
axe-train.recipe
: train.recipe
Axing a bart model.
## S3 method for class 'bart' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'bart' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'bart' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'bart' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed bart object.
library(dbarts) x <- dbarts::bart(mtcars[,2:5], mtcars[,1], verbose = FALSE, keeptrees = TRUE) res <- butcher(x, verbose = TRUE)
library(dbarts) x <- dbarts::bart(mtcars[,2:5], mtcars[,1], verbose = FALSE, keeptrees = TRUE) res <- butcher(x, verbose = TRUE)
C5.0 objects are created from the C50
package, which provides an
interface to the C5.0 classification model. The models that can be
generated include basic tree-based models as well as rule-based models.
## S3 method for class 'C5.0' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'C5.0' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'C5.0' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'C5.0' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'C5.0' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'C5.0' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed C5.0 object.
# Load libraries library(parsnip) library(rsample) library(rpart) # Load data set.seed(1234) split <- initial_split(kyphosis, prop = 9/10) spine_train <- training(split) # Create model and fit c5_fit <- decision_tree(mode = "classification") %>% set_engine("C5.0") %>% fit(Kyphosis ~ ., data = spine_train) out <- butcher(c5_fit, verbose = TRUE) # Try another model from parsnip c5_fit2 <- boost_tree(mode = "classification", trees = 100) %>% set_engine("C5.0") %>% fit(Kyphosis ~ ., data = spine_train) out <- butcher(c5_fit2, verbose = TRUE) # Create model object from original library library(C50) library(modeldata) data(mlc_churn) c5_fit3 <- C5.0(x = mlc_churn[, -20], y = mlc_churn$churn) out <- butcher(c5_fit3, verbose = TRUE)
# Load libraries library(parsnip) library(rsample) library(rpart) # Load data set.seed(1234) split <- initial_split(kyphosis, prop = 9/10) spine_train <- training(split) # Create model and fit c5_fit <- decision_tree(mode = "classification") %>% set_engine("C5.0") %>% fit(Kyphosis ~ ., data = spine_train) out <- butcher(c5_fit, verbose = TRUE) # Try another model from parsnip c5_fit2 <- boost_tree(mode = "classification", trees = 100) %>% set_engine("C5.0") %>% fit(Kyphosis ~ ., data = spine_train) out <- butcher(c5_fit2, verbose = TRUE) # Create model object from original library library(C50) library(modeldata) data(mlc_churn) c5_fit3 <- C5.0(x = mlc_churn[, -20], y = mlc_churn$churn) out <- butcher(c5_fit3, verbose = TRUE)
Axing a coxph.
## S3 method for class 'coxph' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'coxph' axe_data(x, verbose = FALSE, ...)
## S3 method for class 'coxph' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'coxph' axe_data(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
The survival::coxph()
model is unique in how it uses environments in
its components, and butchering such an object can behave in surprising ways
in any environment other than the
global environment
(such as when wrapped in a function). We do not recommend that you use
butcher()
with a coxph
object anywhere other than the global environment.
Do this:
my_coxph_func <- function(df) { coxph(Surv(time, status) ~ x + strata(covar), df) } ## in global environment only: butcher(my_coxph_func(df))
Do not do this:
my_coxph_func <- function(df) { res <- coxph(Surv(time, status) ~ x + strata(covar), df) ## no: butcher(res) } ## will not work correctly: my_coxph_func(df)
Axed coxph object.
library(survival) example_data <- tibble::tibble( time = rpois(1000, 2) + 1, status = rbinom(1000, 1, .5), x = rpois(1000, .5), covar = rbinom(1000, 1, .5) ) example_data make_big_model <- function() { boop <- runif(1e6) coxph(Surv(time, status) ~ x + strata(covar), example_data) } res <- make_big_model() weigh(res) weigh(butcher(res))
library(survival) example_data <- tibble::tibble( time = rpois(1000, 2) + 1, status = rbinom(1000, 1, .5), x = rpois(1000, .5), covar = rbinom(1000, 1, .5) ) example_data make_big_model <- function() { boop <- runif(1e6) coxph(Surv(time, status) ~ x + strata(covar), example_data) } res <- make_big_model() weigh(res) weigh(butcher(res))
earth objects are created from the earth package, which is leveraged to do multivariate adaptive regression splines.
## S3 method for class 'earth' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'earth' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'earth' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'earth' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'earth' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'earth' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed earth object.
# Load libraries library(parsnip) # Create model and fit earth_fit <- mars(mode = "regression") %>% set_engine("earth") %>% fit(Volume ~ ., data = trees) out <- butcher(earth_fit, verbose = TRUE) # Another earth model object suppressWarnings(suppressMessages(library(earth))) earth_mod <- earth(Volume ~ ., data = trees) out <- butcher(earth_mod, verbose = TRUE)
# Load libraries library(parsnip) # Create model and fit earth_fit <- mars(mode = "regression") %>% set_engine("earth") %>% fit(Volume ~ ., data = trees) out <- butcher(earth_fit, verbose = TRUE) # Another earth model object suppressWarnings(suppressMessages(library(earth))) earth_mod <- earth(Volume ~ ., data = trees) out <- butcher(earth_mod, verbose = TRUE)
elnet objects are created from the glmnet package, leveraged to fit generalized linear models via penalized maximum likelihood.
## S3 method for class 'elnet' axe_call(x, verbose = FALSE, ...)
## S3 method for class 'elnet' axe_call(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed model object.
# Load libraries library(parsnip) library(rsample) # Load data split <- initial_split(mtcars, prop = 9/10) car_train <- training(split) # Create model and fit elnet_fit <- linear_reg(mixture = 0, penalty = 0.1) %>% set_engine("glmnet") %>% fit_xy(x = car_train[, 2:11], y = car_train[, 1, drop = FALSE]) out <- butcher(elnet_fit, verbose = TRUE)
# Load libraries library(parsnip) library(rsample) # Load data split <- initial_split(mtcars, prop = 9/10) car_train <- training(split) # Create model and fit elnet_fit <- linear_reg(mixture = 0, penalty = 0.1) %>% set_engine("glmnet") %>% fit_xy(x = car_train[, 2:11], y = car_train[, 1, drop = FALSE]) out <- butcher(elnet_fit, verbose = TRUE)
flexsurvreg objects are created from the flexsurv package. They differ from survreg in that the fitted models are not limited to certain parametric distributions. Users can define their own distribution, or leverage distributions like the generalized gamma, generalized F, and the Royston-Parmar spline model.
## S3 method for class 'flexsurvreg' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'flexsurvreg' axe_env(x, verbose = FALSE, ...)
## S3 method for class 'flexsurvreg' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'flexsurvreg' axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed flexsurvreg object.
# Load libraries library(parsnip) library(flexsurv) # Create model and fit flexsurvreg_fit <- surv_reg(mode = "regression", dist = "gengamma") %>% set_engine("flexsurv") %>% fit(Surv(Tstart, Tstop, status) ~ trans, data = bosms3) out <- butcher(flexsurvreg_fit, verbose = TRUE) # Another flexsurvreg model object wrapped_flexsurvreg <- function() { some_junk_in_environment <- runif(1e6) fit <- flexsurvreg(Surv(futime, fustat) ~ 1, data = ovarian, dist = "weibull") return(fit) } out <- butcher(wrapped_flexsurvreg(), verbose = TRUE)
# Load libraries library(parsnip) library(flexsurv) # Create model and fit flexsurvreg_fit <- surv_reg(mode = "regression", dist = "gengamma") %>% set_engine("flexsurv") %>% fit(Surv(Tstart, Tstop, status) ~ trans, data = bosms3) out <- butcher(flexsurvreg_fit, verbose = TRUE) # Another flexsurvreg model object wrapped_flexsurvreg <- function() { some_junk_in_environment <- runif(1e6) fit <- flexsurvreg(Surv(futime, fustat) ~ 1, data = ovarian, dist = "weibull") return(fit) } out <- butcher(wrapped_flexsurvreg(), verbose = TRUE)
formulas might capture an environment from the modeling development process that carries objects that will not be used for any post- estimation activities.
## S3 method for class 'formula' axe_env(x, verbose = FALSE, ...)
## S3 method for class 'formula' axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed formula object.
wrapped_formula <- function() { some_junk_in_environment <- runif(1e6) ex <- as.formula(paste("y ~", paste(LETTERS, collapse = "+"))) return(ex) } lobstr::obj_size(wrapped_formula()) lobstr::obj_size(butcher(wrapped_formula())) wrapped_quosure <- function() { some_junk_in_environment <- runif(1e6) out <- rlang::quo(x) return(out) } lobstr::obj_size(wrapped_quosure()) lobstr::obj_size(butcher(wrapped_quosure))
wrapped_formula <- function() { some_junk_in_environment <- runif(1e6) ex <- as.formula(paste("y ~", paste(LETTERS, collapse = "+"))) return(ex) } lobstr::obj_size(wrapped_formula()) lobstr::obj_size(butcher(wrapped_formula())) wrapped_quosure <- function() { some_junk_in_environment <- runif(1e6) out <- rlang::quo(x) return(out) } lobstr::obj_size(wrapped_quosure()) lobstr::obj_size(butcher(wrapped_quosure))
Functions stored in model objects often have heavy environments
and bytecode attached. To avoid breaking any post-estimation functions
on the model object, the butchered_function
class is not
appended.
## S3 method for class ''function'' axe_env(x, verbose = FALSE, ...)
## S3 method for class ''function'' axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed function.
# Load libraries library(caret) data(iris) train_data <- iris[, 1:4] train_classes <- iris[, 5] train_fit <- train(train_data, train_classes, method = "knn", preProcess = c("center", "scale"), tuneLength = 10, trControl = trainControl(method = "cv")) out <- axe_env(train_fit$modelInfo$prob, verbose = TRUE) out <- axe_env(train_fit$modelInfo$levels, verbose = TRUE) out <- axe_env(train_fit$modelInfo$predict, verbose = TRUE)
# Load libraries library(caret) data(iris) train_data <- iris[, 1:4] train_classes <- iris[, 5] train_fit <- train(train_data, train_classes, method = "knn", preProcess = c("center", "scale"), tuneLength = 10, trControl = trainControl(method = "cv")) out <- axe_env(train_fit$modelInfo$prob, verbose = TRUE) out <- axe_env(train_fit$modelInfo$levels, verbose = TRUE) out <- axe_env(train_fit$modelInfo$predict, verbose = TRUE)
gam objects are created from the mgcv package.
## S3 method for class 'gam' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'gam' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'gam' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'gam' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'gam' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'gam' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'gam' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'gam' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'gam' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'gam' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed gam object.
cars_gam <- mgcv::gam(mpg ~ s(disp, k = 3) + s(wt), data = mtcars) cleaned_gam <- butcher(cars_gam, verbose = TRUE)
cars_gam <- mgcv::gam(mpg ~ s(disp, k = 3) + s(wt), data = mtcars) cleaned_gam <- butcher(cars_gam, verbose = TRUE)
gausspr objects are created from kernlab package, which
provides a means to do classification, regression, clustering,
novelty detection, quantile regression and dimensionality
reduction. Since fitted model objects from kernlab are S4,
the butcher_gausspr
class is not appended.
## S3 method for class 'gausspr' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'gausspr' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'gausspr' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'gausspr' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'gausspr' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'gausspr' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'gausspr' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'gausspr' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed gausspr object.
library(kernlab) test <- gausspr(Species ~ ., data = iris, var = 2) out <- butcher(test, verbose = TRUE) # Example with simulated regression data x <- seq(-20, 20, 0.1) y <- sin(x)/x + rnorm(401, sd = 0.03) test2 <- gausspr(x, y) out <- butcher(test2, verbose = TRUE)
library(kernlab) test <- gausspr(Species ~ ., data = iris, var = 2) out <- butcher(test, verbose = TRUE) # Example with simulated regression data x <- seq(-20, 20, 0.1) y <- sin(x)/x + rnorm(401, sd = 0.03) test2 <- gausspr(x, y) out <- butcher(test2, verbose = TRUE)
glm objects are created from the base stats package.
## S3 method for class 'glm' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'glm' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'glm' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'glm' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'glm' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'glm' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'glm' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'glm' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed glm object.
cars_glm <- glm(mpg ~ ., data = mtcars) cleaned_glm <- butcher(cars_glm, verbose = TRUE)
cars_glm <- glm(mpg ~ ., data = mtcars) cleaned_glm <- butcher(cars_glm, verbose = TRUE)
glmnet objects are created from the glmnet package, leveraged to fit generalized linear models via penalized maximum likelihood.
## S3 method for class 'glmnet' axe_call(x, verbose = FALSE, ...)
## S3 method for class 'glmnet' axe_call(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed glmnet object.
library(parsnip) # Wrap a parsnip glmnet model wrapped_parsnip_glmnet <- function() { some_junk_in_environment <- runif(1e6) model <- logistic_reg(penalty = 10, mixture = 0.1) %>% set_engine("glmnet") %>% fit(as.factor(vs) ~ ., data = mtcars) return(model$fit) } out <- butcher(wrapped_parsnip_glmnet(), verbose = TRUE)
library(parsnip) # Wrap a parsnip glmnet model wrapped_parsnip_glmnet <- function() { some_junk_in_environment <- runif(1e6) model <- logistic_reg(penalty = 10, mixture = 0.1) %>% set_engine("glmnet") %>% fit(as.factor(vs) ~ ., data = mtcars) return(model$fit) } out <- butcher(wrapped_parsnip_glmnet(), verbose = TRUE)
*_bagg
objects are created from the ipred package, which
is used for bagging classification, regression and survival trees.
## S3 method for class 'regbagg' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'classbagg' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'survbagg' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'regbagg' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'classbagg' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'survbagg' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'regbagg' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'classbagg' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'survbagg' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'regbagg' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'classbagg' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'survbagg' axe_env(x, verbose = FALSE, ...)
## S3 method for class 'regbagg' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'classbagg' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'survbagg' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'regbagg' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'classbagg' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'survbagg' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'regbagg' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'classbagg' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'survbagg' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'regbagg' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'classbagg' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'survbagg' axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed *_bagg
object.
library(ipred) fit_mod <- function() { boop <- runif(1e6) bagging(y ~ x, data.frame(y = rnorm(1e4), x = rnorm(1e4))) } mod_fit <- fit_mod() mod_res <- butcher(mod_fit) weigh(mod_fit) weigh(mod_res)
library(ipred) fit_mod <- function() { boop <- runif(1e6) bagging(y ~ x, data.frame(y = rnorm(1e4), x = rnorm(1e4))) } mod_fit <- fit_mod() mod_res <- butcher(mod_fit) weigh(mod_fit) weigh(mod_res)
kknn objects are created from the kknn package, which is utilized to do weighted k-Nearest Neighbors for classification, regression and clustering.
## S3 method for class 'kknn' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'kknn' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'kknn' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'kknn' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'kknn' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'kknn' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed kknn object.
# Load libraries library(parsnip) library(rsample) library(rpart) library(kknn) # Load data set.seed(1234) split <- initial_split(kyphosis, prop = 9/10) spine_train <- training(split) # Create model and fit kknn_fit <- nearest_neighbor(mode = "classification", neighbors = 3, weight_func = "gaussian", dist_power = 2) %>% set_engine("kknn") %>% fit(Kyphosis ~ ., data = spine_train) out <- butcher(kknn_fit, verbose = TRUE) # Another kknn model object m <- dim(iris)[1] val <- sample(1:m, size = round(m/3), replace = FALSE, prob = rep(1/m, m)) iris.learn <- iris[-val,] iris.valid <- iris[val,] kknn_fit <- kknn(Species ~ ., iris.learn, iris.valid, distance = 1, kernel = "triangular") out <- butcher(kknn_fit, verbose = TRUE)
# Load libraries library(parsnip) library(rsample) library(rpart) library(kknn) # Load data set.seed(1234) split <- initial_split(kyphosis, prop = 9/10) spine_train <- training(split) # Create model and fit kknn_fit <- nearest_neighbor(mode = "classification", neighbors = 3, weight_func = "gaussian", dist_power = 2) %>% set_engine("kknn") %>% fit(Kyphosis ~ ., data = spine_train) out <- butcher(kknn_fit, verbose = TRUE) # Another kknn model object m <- dim(iris)[1] val <- sample(1:m, size = round(m/3), replace = FALSE, prob = rep(1/m, m)) iris.learn <- iris[-val,] iris.valid <- iris[val,] kknn_fit <- kknn(Species ~ ., iris.learn, iris.valid, distance = 1, kernel = "triangular") out <- butcher(kknn_fit, verbose = TRUE)
Axing a KMeansCluster.
## S3 method for class 'KMeansCluster' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'KMeansCluster' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'KMeansCluster' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'KMeansCluster' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed KMeansCluster object.
library(ClusterR) data(dietary_survey_IBS) dat <- scale(dietary_survey_IBS[, -ncol(dietary_survey_IBS)]) km <- KMeans_rcpp(dat, clusters = 2, num_init = 5) out <- butcher(km, verbose = TRUE)
library(ClusterR) data(dietary_survey_IBS) dat <- scale(dietary_survey_IBS[, -ncol(dietary_survey_IBS)]) km <- KMeans_rcpp(dat, clusters = 2, num_init = 5) out <- butcher(km, verbose = TRUE)
Axing a kproto.
## S3 method for class 'kproto' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'kproto' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'kproto' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'kproto' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed kproto object.
library(clustMixType) kproto_fit <- kproto( ToothGrowth, k = 2, lambda = lambdaest(ToothGrowth), verbose = FALSE ) out <- butcher(kproto_fit, verbose = TRUE)
library(clustMixType) kproto_fit <- kproto( ToothGrowth, k = 2, lambda = lambdaest(ToothGrowth), verbose = FALSE ) out <- butcher(kproto_fit, verbose = TRUE)
ksvm objects are created from kernlab package, which provides
a means to do classification, regression, clustering, novelty
detection, quantile regression and dimensionality reduction. Since
fitted model objects from kernlab are S4, the butcher_ksvm
class is not appended.
## S3 method for class 'ksvm' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'ksvm' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'ksvm' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'ksvm' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'ksvm' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'ksvm' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed ksvm object.
# Load libraries library(parsnip) library(kernlab) # Load data data(spam) # Create model and fit ksvm_class <- svm_poly(mode = "classification") %>% set_engine("kernlab") %>% fit(type ~ ., data = spam) out <- butcher(ksvm_class, verbose = TRUE)
# Load libraries library(parsnip) library(kernlab) # Load data data(spam) # Create model and fit ksvm_class <- svm_poly(mode = "classification") %>% set_engine("kernlab") %>% fit(type ~ ., data = spam) out <- butcher(ksvm_class, verbose = TRUE)
lm objects are created from the base stats package.
## S3 method for class 'lm' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'lm' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'lm' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'lm' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'lm' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'lm' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed lm object.
# Load libraries library(parsnip) library(rsample) # Load data split <- initial_split(mtcars, prop = 9/10) car_train <- training(split) # Create model and fit lm_fit <- linear_reg() %>% set_engine("lm") %>% fit(mpg ~ ., data = car_train) out <- butcher(lm_fit, verbose = TRUE) # Another lm object wrapped_lm <- function() { some_junk_in_environment <- runif(1e6) fit <- lm(mpg ~ ., data = mtcars) return(fit) } # Remove junk cleaned_lm <- axe_env(wrapped_lm(), verbose = TRUE) # Check size lobstr::obj_size(cleaned_lm) # Compare environment in terms component lobstr::obj_size(attr(wrapped_lm()$terms, ".Environment")) lobstr::obj_size(attr(cleaned_lm$terms, ".Environment"))
# Load libraries library(parsnip) library(rsample) # Load data split <- initial_split(mtcars, prop = 9/10) car_train <- training(split) # Create model and fit lm_fit <- linear_reg() %>% set_engine("lm") %>% fit(mpg ~ ., data = car_train) out <- butcher(lm_fit, verbose = TRUE) # Another lm object wrapped_lm <- function() { some_junk_in_environment <- runif(1e6) fit <- lm(mpg ~ ., data = mtcars) return(fit) } # Remove junk cleaned_lm <- axe_env(wrapped_lm(), verbose = TRUE) # Check size lobstr::obj_size(cleaned_lm) # Compare environment in terms component lobstr::obj_size(attr(wrapped_lm()$terms, ".Environment")) lobstr::obj_size(attr(cleaned_lm$terms, ".Environment"))
lda and qda objects are created from the MASS package, leveraged to carry out linear discriminant analysis and quadratic discriminant analysis, respectively.
## S3 method for class 'lda' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'qda' axe_env(x, verbose = FALSE, ...)
## S3 method for class 'lda' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'qda' axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed lda or qda object.
library(MASS) fit_da <- function(fit_fn) { boop <- runif(1e6) fit_fn(y ~ x, data.frame(y = rep(letters[1:4], 10000), x = rnorm(40000))) } lda_fit <- fit_da(lda) qda_fit <- fit_da(qda) lda_fit_b <- butcher(lda_fit) qda_fit_b <- butcher(qda_fit) weigh(lda_fit) weigh(lda_fit_b) weigh(qda_fit) weigh(qda_fit_b)
library(MASS) fit_da <- function(fit_fn) { boop <- runif(1e6) fit_fn(y ~ x, data.frame(y = rep(letters[1:4], 10000), x = rnorm(40000))) } lda_fit <- fit_da(lda) qda_fit <- fit_da(qda) lda_fit_b <- butcher(lda_fit) qda_fit_b <- butcher(qda_fit) weigh(lda_fit) weigh(lda_fit_b) weigh(qda_fit) weigh(qda_fit_b)
mda and fda objects are created from the mda package, leveraged to carry out mixture discriminant analysis and flexible discriminat analysis, respectively.
## S3 method for class 'mda' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'fda' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'mda' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'fda' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'mda' axe_fitted(x, verbose = FALSE, ...) ## S3 method for class 'fda' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'mda' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'fda' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'mda' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'fda' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'mda' axe_fitted(x, verbose = FALSE, ...) ## S3 method for class 'fda' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed mda object.
library(mda) mtcars$cyl <- as.factor(mtcars$cyl) fit <- mda(cyl ~ ., data = mtcars) out <- butcher(fit, verbose = TRUE) fit2 <- fda(cyl ~ ., data = mtcars) out2 <- butcher(fit2, verbose = TRUE) # Another mda object data(glass) wrapped_mda <- function(fit_fn) { some_junk_in_environment <- runif(1e6) fit <- fit_fn(Type ~ ., data = glass) return(fit) } lobstr::obj_size(wrapped_mda(mda)) lobstr::obj_size(butcher(wrapped_mda(mda))) lobstr::obj_size(wrapped_mda(fda)) lobstr::obj_size(butcher(wrapped_mda(fda)))
library(mda) mtcars$cyl <- as.factor(mtcars$cyl) fit <- mda(cyl ~ ., data = mtcars) out <- butcher(fit, verbose = TRUE) fit2 <- fda(cyl ~ ., data = mtcars) out2 <- butcher(fit2, verbose = TRUE) # Another mda object data(glass) wrapped_mda <- function(fit_fn) { some_junk_in_environment <- runif(1e6) fit <- fit_fn(Type ~ ., data = glass) return(fit) } lobstr::obj_size(wrapped_mda(mda)) lobstr::obj_size(butcher(wrapped_mda(mda))) lobstr::obj_size(wrapped_mda(fda)) lobstr::obj_size(butcher(wrapped_mda(fda)))
model_fit objects are created from the parsnip
package.
## S3 method for class 'model_fit' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'model_fit' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'model_fit' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'model_fit' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'model_fit' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'model_fit' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'model_fit' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'model_fit' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'model_fit' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'model_fit' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed model_fit object.
library(parsnip) library(rpart) # Create model and fit lm_fit <- linear_reg() %>% set_engine("lm") %>% fit(mpg ~ ., data = mtcars) out <- butcher(lm_fit, verbose = TRUE) # Another parsnip model rpart_fit <- decision_tree(mode = "regression") %>% set_engine("rpart") %>% fit(mpg ~ ., data = mtcars, minsplit = 5, cp = 0.1) out <- butcher(rpart_fit, verbose = TRUE)
library(parsnip) library(rpart) # Create model and fit lm_fit <- linear_reg() %>% set_engine("lm") %>% fit(mpg ~ ., data = mtcars) out <- butcher(lm_fit, verbose = TRUE) # Another parsnip model rpart_fit <- decision_tree(mode = "regression") %>% set_engine("rpart") %>% fit(mpg ~ ., data = mtcars, minsplit = 5, cp = 0.1) out <- butcher(rpart_fit, verbose = TRUE)
multnet objects are created from carrying out multinomial regression in the glmnet package.
## S3 method for class 'multnet' axe_call(x, verbose = FALSE, ...)
## S3 method for class 'multnet' axe_call(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed multnet object.
# Load libraries library(parsnip) # Load data set.seed(1234) predictrs <- matrix(rnorm(100*20), ncol = 20) colnames(predictrs) <- paste0("a", seq_len(ncol(predictrs))) response <- as.factor(sample(1:4, 100, replace = TRUE)) # Create model and fit multnet_fit <- multinom_reg(penalty = 0.1) %>% set_engine("glmnet") %>% fit_xy(x = predictrs, y = response) out <- butcher(multnet_fit, verbose = TRUE)
# Load libraries library(parsnip) # Load data set.seed(1234) predictrs <- matrix(rnorm(100*20), ncol = 20) colnames(predictrs) <- paste0("a", seq_len(ncol(predictrs))) response <- as.factor(sample(1:4, 100, replace = TRUE)) # Create model and fit multnet_fit <- multinom_reg(penalty = 0.1) %>% set_engine("glmnet") %>% fit_xy(x = predictrs, y = response) out <- butcher(multnet_fit, verbose = TRUE)
NaiveBayes objects are created from the klaR package, leveraged to fit a Naive Bayes Classifier.
## S3 method for class 'NaiveBayes' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'NaiveBayes' axe_data(x, verbose = FALSE, ...)
## S3 method for class 'NaiveBayes' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'NaiveBayes' axe_data(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed NaiveBayes object.
library(klaR) fit_mod <- function() { boop <- runif(1e6) NaiveBayes( y ~ x, data = data.frame(y = as.factor(rep(letters[1:4], 1e4)), x = rnorm(4e4)) ) } mod_fit <- fit_mod() mod_res <- butcher(mod_fit) weigh(mod_fit) weigh(mod_res)
library(klaR) fit_mod <- function() { boop <- runif(1e6) NaiveBayes( y ~ x, data = data.frame(y = as.factor(rep(letters[1:4], 1e4)), x = rnorm(4e4)) ) } mod_fit <- fit_mod() mod_res <- butcher(mod_fit) weigh(mod_fit) weigh(mod_res)
nested_model_fit objects are created from the nestedmodels package, which allows parsnip models to be fitted on nested data. Axing a nested_model_fit object involves axing all the inner model_fit objects.
## S3 method for class 'nested_model_fit' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'nested_model_fit' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'nested_model_fit' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'nested_model_fit' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'nested_model_fit' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'nested_model_fit' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'nested_model_fit' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'nested_model_fit' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'nested_model_fit' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'nested_model_fit' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed nested_model_fit object.
library(nestedmodels) library(parsnip) model <- linear_reg() %>% set_engine("lm") %>% nested() nested_data <- tidyr::nest(example_nested_data, data = -id) fit <- fit(model, z ~ x + y + a + b, nested_data) # Reduce the model size butcher(fit)
library(nestedmodels) library(parsnip) model <- linear_reg() %>% set_engine("lm") %>% nested() nested_data <- tidyr::nest(example_nested_data, data = -id) fit <- fit(model, z ~ x + y + a + b, nested_data) # Reduce the model size butcher(fit)
nnet objects are created from the nnet package, leveraged to fit multilayer perceptron models.
## S3 method for class 'nnet' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'nnet' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'nnet' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'nnet' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'nnet' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'nnet' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed nnet object.
# Load libraries library(parsnip) library(nnet) # Create and fit model nnet_fit <- mlp("classification", hidden_units = 2) %>% set_engine("nnet") %>% fit(Species ~ ., data = iris) out <- butcher(nnet_fit, verbose = TRUE) # Another nnet object targets <- class.ind(c(rep("setosa", 50), rep("versicolor", 50), rep("virginica", 50))) fit <- nnet(iris[,1:4], targets, size = 2, rang = 0.1, decay = 5e-4, maxit = 20) out <- butcher(fit, verbose = TRUE)
# Load libraries library(parsnip) library(nnet) # Create and fit model nnet_fit <- mlp("classification", hidden_units = 2) %>% set_engine("nnet") %>% fit(Species ~ ., data = iris) out <- butcher(nnet_fit, verbose = TRUE) # Another nnet object targets <- class.ind(c(rep("setosa", 50), rep("versicolor", 50), rep("virginica", 50))) fit <- nnet(iris[,1:4], targets, size = 2, rang = 0.1, decay = 5e-4, maxit = 20) out <- butcher(fit, verbose = TRUE)
mixo_pls
(via pls()
), mixo_spls
(via spls()
), and mixo_plsda
(via plsda()
) objects are created with the mixOmics package,
leveraged to fit partial least squares models.
## S3 method for class 'mixo_pls' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'mixo_spls' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'mixo_pls' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'mixo_spls' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'mixo_pls' axe_fitted(x, verbose = FALSE, ...) ## S3 method for class 'mixo_spls' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'mixo_pls' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'mixo_spls' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'mixo_pls' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'mixo_spls' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'mixo_pls' axe_fitted(x, verbose = FALSE, ...) ## S3 method for class 'mixo_spls' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
The mixOmics package is not available on CRAN, but can be installed
from the Bioconductor repository via remotes::install_bioc("mixOmics")
.
Axed mixo_pls
, mixo_spls
, or mixo_plsda
object.
library(butcher) do.call(library, list(package = "mixOmics")) # pls ------------------------------------------------------------------ fit_mod <- function() { boop <- runif(1e6) pls(matrix(rnorm(2e4), ncol = 2), rnorm(1e4), mode = "classic") } mod_fit <- fit_mod() mod_res <- butcher(mod_fit) weigh(mod_fit) weigh(mod_res) new_data <- matrix(1:2, ncol = 2) colnames(new_data) <- c("X1", "X2") predict(mod_fit, new_data) predict(mod_res, new_data)
library(butcher) do.call(library, list(package = "mixOmics")) # pls ------------------------------------------------------------------ fit_mod <- function() { boop <- runif(1e6) pls(matrix(rnorm(2e4), ncol = 2), rnorm(1e4), mode = "classic") } mod_fit <- fit_mod() mod_res <- butcher(mod_fit) weigh(mod_fit) weigh(mod_res) new_data <- matrix(1:2, ncol = 2) colnames(new_data) <- c("X1", "X2") predict(mod_fit, new_data) predict(mod_res, new_data)
randomForest objects are created from the randomForest
package, which is used to train random forests based on Breiman's
2001 work. The package supports ensembles of classification and
regression trees.
## S3 method for class 'randomForest' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'randomForest' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'randomForest' axe_env(x, verbose = FALSE, ...)
## S3 method for class 'randomForest' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'randomForest' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'randomForest' axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed randomForest object.
# Load libraries library(parsnip) library(rsample) library(randomForest) data(kyphosis, package = "rpart") # Load data set.seed(1234) split <- initial_split(kyphosis, prop = 9/10) spine_train <- training(split) # Create model and fit randomForest_fit <- rand_forest(mode = "classification", mtry = 2, trees = 2, min_n = 3) %>% set_engine("randomForest") %>% fit_xy(x = spine_train[,2:4], y = spine_train$Kyphosis) out <- butcher(randomForest_fit, verbose = TRUE) # Another randomForest object wrapped_rf <- function() { some_junk_in_environment <- runif(1e6) randomForest_fit <- randomForest(mpg ~ ., data = mtcars) return(randomForest_fit) } # Remove junk cleaned_rf <- axe_env(wrapped_rf(), verbose = TRUE) # Check size lobstr::obj_size(cleaned_rf)
# Load libraries library(parsnip) library(rsample) library(randomForest) data(kyphosis, package = "rpart") # Load data set.seed(1234) split <- initial_split(kyphosis, prop = 9/10) spine_train <- training(split) # Create model and fit randomForest_fit <- rand_forest(mode = "classification", mtry = 2, trees = 2, min_n = 3) %>% set_engine("randomForest") %>% fit_xy(x = spine_train[,2:4], y = spine_train$Kyphosis) out <- butcher(randomForest_fit, verbose = TRUE) # Another randomForest object wrapped_rf <- function() { some_junk_in_environment <- runif(1e6) randomForest_fit <- randomForest(mpg ~ ., data = mtcars) return(randomForest_fit) } # Remove junk cleaned_rf <- axe_env(wrapped_rf(), verbose = TRUE) # Check size lobstr::obj_size(cleaned_rf)
ranger objects are created from the ranger package, which is
used as a means to quickly train random forests. The package supports
ensembles of classification, regression, survival and probability
prediction trees. Given the reliance of post processing functions on
the model object, like importance_pvalues
and treeInfo
,
on the first class listed, the butcher_ranger
class is not
appended.
## S3 method for class 'ranger' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'ranger' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'ranger' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'ranger' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed ranger object.
# Load libraries library(parsnip) library(rsample) library(ranger) # Load data set.seed(1234) split <- initial_split(iris, prop = 9/10) iris_train <- training(split) # Create model and fit ranger_fit <- rand_forest(mode = "classification", mtry = 2, trees = 20, min_n = 3) %>% set_engine("ranger") %>% fit(Species ~ ., data = iris_train) out <- butcher(ranger_fit, verbose = TRUE) # Another ranger object wrapped_ranger <- function() { n <- 100 p <- 400 dat <- data.frame(y = factor(rbinom(n, 1, .5)), replicate(p, runif(n))) fit <- ranger(y ~ ., dat, importance = "impurity_corrected") return(fit) } cleaned_ranger <- axe_fitted(wrapped_ranger(), verbose = TRUE)
# Load libraries library(parsnip) library(rsample) library(ranger) # Load data set.seed(1234) split <- initial_split(iris, prop = 9/10) iris_train <- training(split) # Create model and fit ranger_fit <- rand_forest(mode = "classification", mtry = 2, trees = 20, min_n = 3) %>% set_engine("ranger") %>% fit(Species ~ ., data = iris_train) out <- butcher(ranger_fit, verbose = TRUE) # Another ranger object wrapped_ranger <- function() { n <- 100 p <- 400 dat <- data.frame(y = factor(rbinom(n, 1, .5)), replicate(p, runif(n))) fit <- ranger(y ~ ., dat, importance = "impurity_corrected") return(fit) } cleaned_ranger <- axe_fitted(wrapped_ranger(), verbose = TRUE)
rda objects are created from the klaR package, leveraged to carry out regularized discriminant analysis.
## S3 method for class 'rda' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'rda' axe_env(x, verbose = FALSE, ...)
## S3 method for class 'rda' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'rda' axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed rda object.
library(klaR) fit_mod <- function() { boop <- runif(1e6) rda( y ~ x, data = data.frame(y = rep(letters[1:4], 1e4), x = rnorm(4e4)), gamma = 0.05, lambda = 0.2 ) } mod_fit <- fit_mod() mod_res <- butcher(mod_fit) weigh(mod_fit) weigh(mod_res)
library(klaR) fit_mod <- function() { boop <- runif(1e6) rda( y ~ x, data = data.frame(y = rep(letters[1:4], 1e4), x = rnorm(4e4)), gamma = 0.05, lambda = 0.2 ) } mod_fit <- fit_mod() mod_res <- butcher(mod_fit) weigh(mod_fit) weigh(mod_res)
recipe objects are created from the recipes package, which is leveraged for its set of data pre-processing tools. These recipes work by sequentially defining each pre-processing step. The implementation of each step, however, results its own class so we bundle all the axe methods related to recipe objects in general here. Note that the butchered class is only added to the recipe as a whole, and not to each pre-processing step.
## S3 method for class 'recipe' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'step' axe_env(x, ...) ## S3 method for class 'step_arrange' axe_env(x, ...) ## S3 method for class 'step_filter' axe_env(x, ...) ## S3 method for class 'step_mutate' axe_env(x, ...) ## S3 method for class 'step_slice' axe_env(x, ...) ## S3 method for class 'step_impute_bag' axe_env(x, ...) ## S3 method for class 'step_bagimpute' axe_env(x, ...) ## S3 method for class 'step_impute_knn' axe_env(x, ...) ## S3 method for class 'step_knnimpute' axe_env(x, ...) ## S3 method for class 'step_geodist' axe_env(x, ...) ## S3 method for class 'step_interact' axe_env(x, ...) ## S3 method for class 'step_ratio' axe_env(x, ...) ## S3 method for class 'quosure' axe_env(x, ...) ## S3 method for class 'recipe' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'recipe' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'step' axe_env(x, ...) ## S3 method for class 'step_arrange' axe_env(x, ...) ## S3 method for class 'step_filter' axe_env(x, ...) ## S3 method for class 'step_mutate' axe_env(x, ...) ## S3 method for class 'step_slice' axe_env(x, ...) ## S3 method for class 'step_impute_bag' axe_env(x, ...) ## S3 method for class 'step_bagimpute' axe_env(x, ...) ## S3 method for class 'step_impute_knn' axe_env(x, ...) ## S3 method for class 'step_knnimpute' axe_env(x, ...) ## S3 method for class 'step_geodist' axe_env(x, ...) ## S3 method for class 'step_interact' axe_env(x, ...) ## S3 method for class 'step_ratio' axe_env(x, ...) ## S3 method for class 'quosure' axe_env(x, ...) ## S3 method for class 'recipe' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed recipe object.
library(recipes) data(biomass, package = "modeldata") biomass_tr <- biomass[biomass$dataset == "Training",] rec <- recipe(HHV ~ carbon + hydrogen + oxygen + nitrogen + sulfur, data = biomass_tr) %>% step_center(all_predictors()) %>% step_scale(all_predictors()) %>% step_spatialsign(all_predictors()) out <- butcher(rec, verbose = TRUE) # Another recipe object wrapped_recipes <- function() { some_junk_in_environment <- runif(1e6) return( recipe(mpg ~ cyl, data = mtcars) %>% step_center(all_predictors()) %>% step_scale(all_predictors()) %>% prep() ) } # Remove junk in environment cleaned1 <- axe_env(wrapped_recipes(), verbose = TRUE) # Replace prepared training data with zero-row slice cleaned2 <- axe_fitted(wrapped_recipes(), verbose = TRUE) # Check size lobstr::obj_size(cleaned1) lobstr::obj_size(cleaned2)
library(recipes) data(biomass, package = "modeldata") biomass_tr <- biomass[biomass$dataset == "Training",] rec <- recipe(HHV ~ carbon + hydrogen + oxygen + nitrogen + sulfur, data = biomass_tr) %>% step_center(all_predictors()) %>% step_scale(all_predictors()) %>% step_spatialsign(all_predictors()) out <- butcher(rec, verbose = TRUE) # Another recipe object wrapped_recipes <- function() { some_junk_in_environment <- runif(1e6) return( recipe(mpg ~ cyl, data = mtcars) %>% step_center(all_predictors()) %>% step_scale(all_predictors()) %>% prep() ) } # Remove junk in environment cleaned1 <- axe_env(wrapped_recipes(), verbose = TRUE) # Replace prepared training data with zero-row slice cleaned2 <- axe_fitted(wrapped_recipes(), verbose = TRUE) # Check size lobstr::obj_size(cleaned1) lobstr::obj_size(cleaned2)
rpart objects are created from the rpart package, which is used for recursive partitioning for classification, regression and survival trees.
## S3 method for class 'rpart' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'rpart' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'rpart' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'rpart' axe_env(x, verbose = FALSE, ...)
## S3 method for class 'rpart' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'rpart' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'rpart' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'rpart' axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed rpart object.
# Load libraries library(parsnip) library(rsample) library(rpart) # Load data set.seed(1234) split <- initial_split(mtcars, prop = 9/10) car_train <- training(split) # Create model and fit rpart_fit <- decision_tree(mode = "regression") %>% set_engine("rpart") %>% fit(mpg ~ ., data = car_train, minsplit = 5, cp = 0.1) out <- butcher(rpart_fit, verbose = TRUE) # Another rpart object wrapped_rpart <- function() { some_junk_in_environment <- runif(1e6) fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis, x = TRUE, y = TRUE) return(fit) } # Remove junk cleaned_rpart <- axe_env(wrapped_rpart(), verbose = TRUE) # Check size lobstr::obj_size(cleaned_rpart)
# Load libraries library(parsnip) library(rsample) library(rpart) # Load data set.seed(1234) split <- initial_split(mtcars, prop = 9/10) car_train <- training(split) # Create model and fit rpart_fit <- decision_tree(mode = "regression") %>% set_engine("rpart") %>% fit(mpg ~ ., data = car_train, minsplit = 5, cp = 0.1) out <- butcher(rpart_fit, verbose = TRUE) # Another rpart object wrapped_rpart <- function() { some_junk_in_environment <- runif(1e6) fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis, x = TRUE, y = TRUE) return(fit) } # Remove junk cleaned_rpart <- axe_env(wrapped_rpart(), verbose = TRUE) # Check size lobstr::obj_size(cleaned_rpart)
sclass objects are byproducts of classbagg objects.
## S3 method for class 'sclass' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'sclass' axe_env(x, verbose = FALSE, ...)
## S3 method for class 'sclass' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'sclass' axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed sclass object.
# Load libraries library(ipred) library(rpart) library(MASS) # Load data data("GlaucomaM", package = "TH.data") classbagg_fit <- bagging(Class ~ ., data = GlaucomaM, coob = TRUE) out <- butcher(classbagg_fit$mtrees[[1]], verbose = TRUE) # Another classbagg object wrapped_classbagg <- function() { some_junk_in_environment <- runif(1e6) fit <- bagging(Species ~ ., data = iris, nbagg = 10, coob = TRUE) return(fit) } # Remove junk cleaned_classbagg <- butcher(wrapped_classbagg(), verbose = TRUE) # Check size lobstr::obj_size(cleaned_classbagg)
# Load libraries library(ipred) library(rpart) library(MASS) # Load data data("GlaucomaM", package = "TH.data") classbagg_fit <- bagging(Class ~ ., data = GlaucomaM, coob = TRUE) out <- butcher(classbagg_fit$mtrees[[1]], verbose = TRUE) # Another classbagg object wrapped_classbagg <- function() { some_junk_in_environment <- runif(1e6) fit <- bagging(Species ~ ., data = iris, nbagg = 10, coob = TRUE) return(fit) } # Remove junk cleaned_classbagg <- butcher(wrapped_classbagg(), verbose = TRUE) # Check size lobstr::obj_size(cleaned_classbagg)
spark objects are created from the sparklyr package,
a R interface for Apache Spark. The axe methods available
for spark objects are designed such that interoperability
is maintained. In other words, for a multilingual machine
learning team, butchered spark objects instantiated from
sparklyr can still be serialized to disk, work in
Python, be deployed on Scala, etc. It is also worth noting
here that spark objects created from sparklyr have a
lot of metadata attached to it, including but not limited
to the formula, dataset, model, index labels, etc. The
axe functions provided are for parsing down the model
object both prior saving to disk, or loading from disk.
Traditional R save functions are not available for these
objects, so functionality is provided in sparklyr::ml_save
.
This function gives the user the option to keep either the
pipeline_model
or the pipeline
, so both of these
objects are retained from butchering, yet removal of one or the
other might be conducive to freeing up memory on disk.
## S3 method for class 'ml_model' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'ml_model' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'ml_model' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'ml_model' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'ml_model' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'ml_model' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'ml_model' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'ml_model' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed spark object.
library(sparklyr) sc <- spark_connect(master = "local") iris_tbls <- sdf_copy_to(sc, iris, overwrite = TRUE) %>% sdf_random_split(train = 2/3, validation = 2/3, seed = 2018) train <- iris_tbls$train spark_fit <- ml_logistic_regression(train, Species ~ .) out <- butcher(spark_fit, verbose = TRUE) spark_disconnect(sc)
library(sparklyr) sc <- spark_connect(master = "local") iris_tbls <- sdf_copy_to(sc, iris, overwrite = TRUE) %>% sdf_random_split(train = 2/3, validation = 2/3, seed = 2018) train <- iris_tbls$train spark_fit <- ml_logistic_regression(train, Species ~ .) out <- butcher(spark_fit, verbose = TRUE) spark_disconnect(sc)
survreg objects are created from the survival package. They
are returned from the survreg
function, representing fitted
parametric survival models.
## S3 method for class 'survreg' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'survreg' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'survreg' axe_env(x, verbose = FALSE, ...)
## S3 method for class 'survreg' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'survreg' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'survreg' axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed survreg object.
# Load libraries library(parsnip) library(survival) # Create model and fit survreg_fit <- surv_reg(mode = "regression", dist = "weibull") %>% set_engine("survival") %>% fit(Surv(futime, fustat) ~ 1, data = ovarian) out <- butcher(survreg_fit, verbose = TRUE) # Another survreg object wrapped_survreg <- function() { some_junk_in_environment <- runif(1e6) fit <- survreg(Surv(time, status) ~ ph.ecog + age + strata(sex), data = lung) return(fit) } # Remove junk cleaned_survreg <- butcher(wrapped_survreg(), verbose = TRUE) # Check size lobstr::obj_size(cleaned_survreg)
# Load libraries library(parsnip) library(survival) # Create model and fit survreg_fit <- surv_reg(mode = "regression", dist = "weibull") %>% set_engine("survival") %>% fit(Surv(futime, fustat) ~ 1, data = ovarian) out <- butcher(survreg_fit, verbose = TRUE) # Another survreg object wrapped_survreg <- function() { some_junk_in_environment <- runif(1e6) fit <- survreg(Surv(time, status) ~ ph.ecog + age + strata(sex), data = lung) return(fit) } # Remove junk cleaned_survreg <- butcher(wrapped_survreg(), verbose = TRUE) # Check size lobstr::obj_size(cleaned_survreg)
survreg.penal objects are created from the survival package. They
are returned from the survreg
function, representing fitted
parametric survival models.
## S3 method for class 'survreg.penal' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'survreg.penal' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'survreg.penal' axe_env(x, verbose = FALSE, ...)
## S3 method for class 'survreg.penal' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'survreg.penal' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'survreg.penal' axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed survreg object.
# Load libraries library(parsnip) library(survival) # Create model and fit survreg_fit <- surv_reg(mode = "regression", dist = "weibull") %>% set_engine("survival") %>% fit(Surv(time, status) ~ rx, data = rats) out <- butcher(survreg_fit, verbose = TRUE) # Another survreg.penal object wrapped_survreg.penal <- function() { some_junk_in_environment <- runif(1e6) fit <- survreg(Surv(time, status) ~ rx, data = rats, subset = (sex == "f")) return(fit) } # Remove junk cleaned_sp <- axe_env(wrapped_survreg.penal(), verbose = TRUE) # Check size lobstr::obj_size(cleaned_sp)
# Load libraries library(parsnip) library(survival) # Create model and fit survreg_fit <- surv_reg(mode = "regression", dist = "weibull") %>% set_engine("survival") %>% fit(Surv(time, status) ~ rx, data = rats) out <- butcher(survreg_fit, verbose = TRUE) # Another survreg.penal object wrapped_survreg.penal <- function() { some_junk_in_environment <- runif(1e6) fit <- survreg(Surv(time, status) ~ rx, data = rats, subset = (sex == "f")) return(fit) } # Remove junk cleaned_sp <- axe_env(wrapped_survreg.penal(), verbose = TRUE) # Check size lobstr::obj_size(cleaned_sp)
Generics related to axing objects of the term class.
## S3 method for class 'terms' axe_env(x, verbose = FALSE, ...)
## S3 method for class 'terms' axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed terms object.
# Using lm wrapped_lm <- function() { some_junk_in_environment <- runif(1e6) fit <- lm(mpg ~ ., data = mtcars) return(fit) } # Remove junk cleaned_lm <- axe_env(wrapped_lm(), verbose = TRUE) # Check size lobstr::obj_size(cleaned_lm) # Compare environment in terms component lobstr::obj_size(attr(wrapped_lm()$terms, ".Environment")) lobstr::obj_size(attr(cleaned_lm$terms, ".Environment")) # Using rpart library(rpart) wrapped_rpart <- function() { some_junk_in_environment <- runif(1e6) fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis, x = TRUE, y = TRUE) return(fit) } lobstr::obj_size(wrapped_rpart()) lobstr::obj_size(axe_env(wrapped_rpart()))
# Using lm wrapped_lm <- function() { some_junk_in_environment <- runif(1e6) fit <- lm(mpg ~ ., data = mtcars) return(fit) } # Remove junk cleaned_lm <- axe_env(wrapped_lm(), verbose = TRUE) # Check size lobstr::obj_size(cleaned_lm) # Compare environment in terms component lobstr::obj_size(attr(wrapped_lm()$terms, ".Environment")) lobstr::obj_size(attr(cleaned_lm$terms, ".Environment")) # Using rpart library(rpart) wrapped_rpart <- function() { some_junk_in_environment <- runif(1e6) fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis, x = TRUE, y = TRUE) return(fit) } lobstr::obj_size(wrapped_rpart()) lobstr::obj_size(axe_env(wrapped_rpart()))
train objects are created from the caret package.
## S3 method for class 'train' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'train' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'train' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'train' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'train' axe_fitted(x, verbose = FALSE, ...)
## S3 method for class 'train' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'train' axe_ctrl(x, verbose = FALSE, ...) ## S3 method for class 'train' axe_data(x, verbose = FALSE, ...) ## S3 method for class 'train' axe_env(x, verbose = FALSE, ...) ## S3 method for class 'train' axe_fitted(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed train object.
# Load libraries library(caret) data(iris) train_data <- iris[, 1:4] train_classes <- iris[, 5] train_fit <- train(train_data, train_classes, method = "knn", preProcess = c("center", "scale"), tuneLength = 10, trControl = trainControl(method = "cv")) out <- butcher(train_fit, verbose = TRUE)
# Load libraries library(caret) data(iris) train_data <- iris[, 1:4] train_classes <- iris[, 5] train_fit <- train(train_data, train_classes, method = "knn", preProcess = c("center", "scale"), tuneLength = 10, trControl = trainControl(method = "cv")) out <- butcher(train_fit, verbose = TRUE)
train.recipe objects are slightly different from train objects
created from the caret
package in that it also includes
instructions from a recipe
for data pre-processing. Axing
functions specific to train.recipe are thus included as additional
steps are required to remove parts of train.recipe objects.
## S3 method for class 'train.recipe' axe_call(x, ...) ## S3 method for class 'train.recipe' axe_ctrl(x, ...) ## S3 method for class 'train.recipe' axe_data(x, ...) ## S3 method for class 'train.recipe' axe_env(x, ...) ## S3 method for class 'train.recipe' axe_fitted(x, ...)
## S3 method for class 'train.recipe' axe_call(x, ...) ## S3 method for class 'train.recipe' axe_ctrl(x, ...) ## S3 method for class 'train.recipe' axe_data(x, ...) ## S3 method for class 'train.recipe' axe_env(x, ...) ## S3 method for class 'train.recipe' axe_fitted(x, ...)
x |
A model object. |
... |
Any additional arguments related to axing. |
Axed train.recipe object.
library(recipes) library(caret) data(biomass, package = "modeldata") data(biomass) recipe <- biomass %>% recipe(HHV ~ carbon + hydrogen + oxygen + nitrogen + sulfur) %>% step_center(all_predictors()) %>% step_scale(all_predictors()) %>% step_spatialsign(all_predictors()) train.recipe_fit <- train(recipe, biomass, method = "svmRadial", metric = "RMSE") out <- butcher(train.recipe_fit, verbose = TRUE)
library(recipes) library(caret) data(biomass, package = "modeldata") data(biomass) recipe <- biomass %>% recipe(HHV ~ carbon + hydrogen + oxygen + nitrogen + sulfur) %>% step_center(all_predictors()) %>% step_scale(all_predictors()) %>% step_spatialsign(all_predictors()) train.recipe_fit <- train(recipe, biomass, method = "svmRadial", metric = "RMSE") out <- butcher(train.recipe_fit, verbose = TRUE)
xgb.Booster objects are created from the xgboost package,
which provides efficient and scalable implementations of gradient
boosted decision trees. Given the reliance of post processing
functions on the model object, like xgb.Booster.complete
,
on the first class listed, the butcher_xgb.Booster
class is
not appended.
## S3 method for class 'xgb.Booster' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'xgb.Booster' axe_env(x, verbose = FALSE, ...)
## S3 method for class 'xgb.Booster' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'xgb.Booster' axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed xgb.Booster object.
library(xgboost) library(parsnip) data(agaricus.train) bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, eta = 1, nthread = 2, nrounds = 2, eval_metric = "logloss", objective = "binary:logistic", verbose = 0) out <- butcher(bst, verbose = TRUE) # Another xgboost model fit <- boost_tree(mode = "classification", trees = 20) %>% set_engine("xgboost", eval_metric = "mlogloss") %>% fit(Species ~ ., data = iris) out <- butcher(fit, verbose = TRUE)
library(xgboost) library(parsnip) data(agaricus.train) bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label, eta = 1, nthread = 2, nrounds = 2, eval_metric = "logloss", objective = "binary:logistic", verbose = 0) out <- butcher(bst, verbose = TRUE) # Another xgboost model fit <- boost_tree(mode = "classification", trees = 20) %>% set_engine("xgboost", eval_metric = "mlogloss") %>% fit(Species ~ ., data = iris) out <- butcher(fit, verbose = TRUE)
Axing a xrf.
## S3 method for class 'xrf' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'xrf' axe_env(x, verbose = FALSE, ...)
## S3 method for class 'xrf' axe_call(x, verbose = FALSE, ...) ## S3 method for class 'xrf' axe_env(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed xrf object.
library(xrf) xrf_big <- function() { boop <- runif(1e6) xrf( mpg ~ ., mtcars, xgb_control = list(nrounds = 2, max_depth = 2), family = 'gaussian' ) } heavy_m <- xrf_big() m <- butcher(heavy_m, verbose = TRUE) weigh(heavy_m) weigh(m)
library(xrf) xrf_big <- function() { boop <- runif(1e6) xrf( mpg ~ ., mtcars, xgb_control = list(nrounds = 2, max_depth = 2), family = 'gaussian' ) } heavy_m <- xrf_big() m <- butcher(heavy_m, verbose = TRUE) weigh(heavy_m) weigh(m)
Reduce the size of a model object so that it takes up less memory on
disk. Currently, the model object is stripped down to the point that
only the minimal components necessary for the predict
function
to work remain. Future adjustments to this function will be needed to
avoid removal of model fit components to ensure it works with other
downstream functions.
butcher(x, verbose = FALSE, ...)
butcher(x, verbose = FALSE, ...)
x |
A model object. |
verbose |
Print information each time an axe method is executed.
Notes how much memory is released and what functions are
disabled. Default is |
... |
Any additional arguments related to axing. |
Axed model object with new butcher subclass assignment.
Locate where a specific component of a object might exist within the model object itself. This function is restricted in that only items that can be axed can be found.
locate(x, name = NULL)
locate(x, name = NULL)
x |
A model object. |
name |
A name associated with model component of interest.
This defaults to NULL. Possible components include: |
Location of specific component in a model object.
lm_fit <- lm(mpg ~ ., data = mtcars) locate(lm_fit, name = "env") locate(lm_fit, name = "call")
lm_fit <- lm(mpg ~ ., data = mtcars) locate(lm_fit, name = "env") locate(lm_fit, name = "call")
new_model_butcher()
will instantiate the following to help
us develop new axe functions around removing parts of a new
modeling object:
Add modeling package to Suggests
Generate and populate an axe file under R/
Generate and populate an test file under testthat/
new_model_butcher(model_class, package_name, open = interactive())
new_model_butcher(model_class, package_name, open = interactive())
model_class |
A string that captures the class name of the new model object. |
package_name |
A string that captures the package name from which the new model is made. |
open |
Check if user is in interactive mode, and if so, opens the new files for editing. |
Evaluate the size of each element contained in a model object.
weigh(x, threshold = 0, units = "MB", ...)
weigh(x, threshold = 0, units = "MB", ...)
x |
A model object. |
threshold |
The minimum threshold desired for model component size to display. |
units |
The units in which to display the size of each component
within the model object of interest. Defaults to |
... |
Any additional arguments for weighing. |
Tibble with weights of object components in decreasing magnitude.
simulate_x <- matrix(runif(1e+6), ncol = 2) simulate_y <- runif(dim(simulate_x)[1]) lm_out <- lm(simulate_y ~ simulate_x) weigh(lm_out)
simulate_x <- matrix(runif(1e+6), ncol = 2) simulate_y <- runif(dim(simulate_x)[1]) lm_out <- lm(simulate_y ~ simulate_x) weigh(lm_out)