Learn R Programming

MachineShop (version 2.8.0)

MLModel: MLModel Class Constructor

Description

Create a model for use with the MachineShop package.

Usage

MLModel(
  name = "MLModel",
  label = name,
  packages = character(),
  response_types = character(),
  predictor_encoding = c(NA, "model.matrix", "terms"),
  params = list(),
  gridinfo = tibble::tibble(param = character(), values = list(), regular = logical()),
  fit = function(formula, data, weights, ...) stop("no fit function"),
  predict = function(object, newdata, times, ...) stop("no predict function"),
  varimp = function(object, ...) NULL,
  ...
)

Arguments

name

character name of the object to which the model is assigned.

label

optional character descriptor for the model.

packages

character vector of package names upon which the model depends. Each name may be optionally followed by a comment in parentheses specifying a version requirement. The comment should contain a comparison operator, whitespace and a valid version number, e.g. "xgboost (>= 1.3.0)".

response_types

character vector of response variable types to which the model can be fit. Supported types are "binary", = "BinomialVariate", "DiscreteVariate", "factor", "matrix", "NegBinomialVariate", "numeric", "ordered", "PoissonVariate", and "Surv".

predictor_encoding

character string indicating whether the model is fit with predictor variables encoded as a "model.matrix", a data.frame containing the originally specified model "terms", or unspecified (default).

params

list of user-specified model parameters to be passed to the fit function.

gridinfo

tibble of information for construction of tuning grids consisting of a character column param with the names of parameters in the grid, a list column values with functions to generate grid points for the corresponding parameters, and an optional logical column regular indicating which parameters to include by default in regular grids. Values functions may optionally include arguments n and data for the number of grid points to generate and a ModelFrame of the model fit data and formula, respectively; and must include an ellipsis (...).

fit

model fitting function whose arguments are a formula, a ModelFrame named data, case weights, and an ellipsis.

predict

model prediction function whose arguments are the object returned by fit, a ModelFrame named newdata of predictor variables, optional vector of times at which to predict survival, and an ellipsis.

varimp

variable importance function whose arguments are the object returned by fit, optional arguments passed from calls to varimp, and an ellipsis.

...

arguments passed from other methods.

Value

MLModel class object.

Details

If supplied, the grid function should return a list whose elements are named after and contain values of parameters to include in a tuning grid to be constructed automatically by the package.

Argument data in the fit function may be converted to a data frame with the as.data.frame function as needed. The function should return the object resulting from the model fit.

Values returned by the predict functions should be formatted according to the response variable types below.

factor

vector or column matrix of probabilities for the second level of binary factors or a matrix whose columns contain the probabilities for factors with more than two levels.

matrix

matrix of predicted responses.

numeric

vector or column matrix of predicted responses.

Surv

matrix whose columns contain survival probabilities at times if supplied or a vector of predicted survival means otherwise.

The varimp function should return a vector of importance values named after the predictor variables or a matrix or data frame whose rows are named after the predictors.

See Also

models, fit, resample

Examples

Run this code
# NOT RUN {
## Logistic regression model
LogisticModel <- MLModel(
  name = "LogisticModel",
  response_types = "binary",
  fit = function(formula, data, weights, ...) {
    glm(formula, data = data, weights = weights, family = binomial, ...)
  },
  predict = function(object, newdata, ...) {
    predict(object, newdata = newdata, type = "response")
  },
  varimp = function(object, ...) {
    pchisq(coef(object)^2 / diag(vcov(object)), 1)
  }
)

data(Pima.tr, package = "MASS")
res <- resample(type ~ ., data = Pima.tr, model = LogisticModel)
summary(res)

# }

Run the code above in your browser using DataLab