devtools::load_all() knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" )
The goal of predictr is to allow for model predictions which minimize memory usage and computing time. This package provides a new type of object and adaptors for various common model types.
You can install predictr from github with:
# install.packages("devtools") devtools::install_github("craiggrabowski/predictr")
This is a basic example which illustrates predictor()
, the main method defined in this package:
## basic example code v <- seq(1024) df <- data.frame(x = v, y = v + 1) model <- lm(y ~ x, df) x <- predictor(model) predict(x, data.frame(x = c(2, 3)))
We create a model object using linear regression
and then convert to a new object using predictor()
.
This new object implements the predict()
method,
but its implementation does not produce the same results as
the original regression object.
In particular,
the output is now expressed as a matrix
instead of a numeric vector.
The new object created by predictor()
has a few advantages
over the object produced using a call to lm()
.
First,
the new object is significantly smaller than the existing model
object:
pryr::object_size(model) pryr::object_size(x)
Second,
the predict()
method is now faster for large data:
a <- data.frame(x = seq(100000)) microbenchmark::microbenchmark(predict(model, a), predict(x, a))
Third,
the new object supports a conversion to a function
using as.function()
.
This return value of this converion
is a function which implements predict()
as well,
only now the compute time is much smaller:
f <- as.function(x) microbenchmark::microbenchmark(predict(x, a), f(a))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.