Modeling or machine learning in R can result in fitted model objects
that take up too much memory. There are two main culprits:
Heavy usage of formulas and closures that capture the enclosing
environment in model training
Lack of selectivity in the construction of the model object itself
As a result, fitted model objects contain components that are often
redundant and not required for post-fit estimation activities. The
butcher package provides tooling to “axe” parts of the fitted output
that are no longer needed, without sacrificing prediction functionality
from the original model object.
The problem here is in the terms component of our big_lm. Because of
how lm() is implemented in the stats package, the environment in
which our model was made is carried along in the fitted output. To
remove the (mostly) extraneous component, we can use butcher():
To make the most of your memory available, this package provides five S3
generics for you to remove parts of a model object:
axe_call(): To remove the call object.
axe_ctrl(): To remove controls associated with training.
axe_data(): To remove the original training data.
axe_env(): To remove environments.
axe_fitted(): To remove fitted values.
When you run butcher(), you execute all of these axing functions at
once. Any kind of axing on the object will append a butchered class to
the current model object class(es) as well as a new attribute named
butcher_disabled that lists any post-fit estimation functions that are
disabled as a result.
Model Object Coverage
Check out the vignette("available-axe-methods") to see butcher’s
current coverage. If you are working with a new model object that could
benefit from any kind of axing, we would love for you to make a pull
request! You can visit the vignette("adding-models-to-butcher") for
more guidelines, but in short, to contribute a set of axe methods:
Run
new_model_butcher(model_class = "your_object", package_name = "your_package")
Use butcher helper functions weigh() and locate() to decide what
to axe
Finalize edits to R/your_object.R and
tests/testthat/test-your_object.R
Make a pull request!
Contributing
This project is released with a Contributor Code of
Conduct.
By contributing to this project, you agree to abide by its terms.
For questions and discussions about tidymodels packages, modeling, and
machine learning, please post on RStudio
Community.
If you think you have encountered a bug, please submit an
issue.
Either way, learn how to create and share a
reprex
(a minimal, reproducible example), to clearly communicate about your
code.
butcher
Overview
Modeling or machine learning in R can result in fitted model objects that take up too much memory. There are two main culprits:
As a result, fitted model objects contain components that are often redundant and not required for post-fit estimation activities. The butcher package provides tooling to “axe” parts of the fitted output that are no longer needed, without sacrificing prediction functionality from the original model object.
Installation
Install the released version from CRAN:
Or install the development version from GitHub:
Butchering
As an example, let’s wrap an
lmmodel so it contains a lot of unnecessary stuff:This object is unnecessarily large:
When, in fact, it should only be:
To understand which part of our original model object is taking up the most memory, we leverage the
weigh()function:The problem here is in the
termscomponent of ourbig_lm. Because of howlm()is implemented in thestatspackage, the environment in which our model was made is carried along in the fitted output. To remove the (mostly) extraneous component, we can usebutcher():Comparing it against our
small_lm, we find:And now it will take up about the same memory on disk as
small_lm:To make the most of your memory available, this package provides five S3 generics for you to remove parts of a model object:
axe_call(): To remove the call object.axe_ctrl(): To remove controls associated with training.axe_data(): To remove the original training data.axe_env(): To remove environments.axe_fitted(): To remove fitted values.When you run
butcher(), you execute all of these axing functions at once. Any kind of axing on the object will append a butchered class to the current model object class(es) as well as a new attribute namedbutcher_disabledthat lists any post-fit estimation functions that are disabled as a result.Model Object Coverage
Check out the
vignette("available-axe-methods")to see butcher’s current coverage. If you are working with a new model object that could benefit from any kind of axing, we would love for you to make a pull request! You can visit thevignette("adding-models-to-butcher")for more guidelines, but in short, to contribute a set of axe methods:new_model_butcher(model_class = "your_object", package_name = "your_package")weigh()andlocate()to decide what to axeR/your_object.Randtests/testthat/test-your_object.RContributing
This project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
For questions and discussions about tidymodels packages, modeling, and machine learning, please post on RStudio Community.
If you think you have encountered a bug, please submit an issue.
Either way, learn how to create and share a reprex (a minimal, reproducible example), to clearly communicate about your code.
Check out further details on contributing guidelines for tidymodels packages and how to get help.