ml_model: R6 class for prediction models

ml_modelR Documentation

R6 class for prediction models

Description

R6 class for prediction models

R6 class for prediction models

Details

Provides standardized estimation and prediction methods

Public fields

info

Optional information/name of the model

formals

List with formal arguments of estimation and prediction functions

formula

Formula specifying response and design matrix

Active bindings

fit

Active binding returning estimated model object

Methods

Public methods


Method new()

Create a new prediction model object

Usage
ml_model$new(
  formula = NULL,
  fit,
  pred = predict,
  pred.args = NULL,
  info = NULL,
  specials,
  response.arg = "y",
  x.arg = "x",
  ...
)
Arguments
formula

formula specifying outcome and design matrix

fit

function for fitting the model (must be a function response, 'y', and design matrix, 'x'. Alternatively, a function with a single 'formula' argument)

pred

prediction function (must be a function of model object, 'fit', and new design matrix, 'newdata')

pred.args

optional arguments to prediction function

info

optional description of the model

specials

optional additional terms (weights, offset, id, subset, ...) passed to 'fit'

response.arg

name of response argument

x.arg

name of design matrix argument

...

optional arguments to fitting function


Method estimate()

Estimation method

Usage
ml_model$estimate(data, ..., store = TRUE)
Arguments
data

data.frame

...

Additional arguments to estimation method

store

Logical determining if estimated model should be stored inside the class.


Method predict()

Prediction method

Usage
ml_model$predict(newdata, ..., fit = NULL)
Arguments
newdata

data.frame

...

Additional arguments to prediction method

fit

Optional model fit object


Method update()

Update formula

Usage
ml_model$update(formula, ...)
Arguments
formula

formula

...

Additional arguments to lower level functions


Method print()

Print method

Usage
ml_model$print(...)
Arguments
...

Additional arguments to lower level functions


Method response()

Extract response from data

Usage
ml_model$response(data)
Arguments
data

data.frame


Method design()

Extract design matrix (features) from data

Usage
ml_model$design(data)
Arguments
data

data.frame


Method clone()

The objects of this class are cloneable with this method.

Usage
ml_model$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Author(s)

Klaus Kähler Holst

Examples

data(iris)
rf <- function(formula, ...)
ml_model$new(formula, info="grf::probability_forest",
             fit=function(x,y, ...) grf::probability_forest(X=x, Y=y, ...),
             pred=function(fit, newdata) predict(fit, newdata)$predictions, ...)

args <- expand.list(num.trees=c(100,200), mtry=1:3,
                   formula=c(Species ~ ., Species ~ Sepal.Length + Sepal.Width))
models <- lapply(args, function(par) do.call(rf, par))

x <- models[[1]]$clone()
x$estimate(iris)
predict(x, newdata=head(iris))

 # Reduce Ex. timing
a <- targeted::cv(models, data=iris)
cbind(coef(a), attr(args, "table"))


targeted documentation built on Oct. 26, 2022, 1:09 a.m.