Learn R Programming

fastml (version 0.6.1)

predict.fastml: Predict method for fastml objects

Description

Generates predictions from a trained `fastml` object on new data. Supports both single-model and multi-model workflows, and handles classification and regression tasks with optional post-processing and verbosity.

Usage

# S3 method for fastml
predict(
  object,
  newdata,
  type = "auto",
  model_name = NULL,
  verbose = FALSE,
  postprocess_fn = NULL,
  ...
)

Value

A vector of predictions, or a named list of predictions (if multiple models are used). If `postprocess_fn` is supplied, its output will be returned instead.

Arguments

object

A fitted `fastml` object created by the `fastml()` function.

newdata

A data frame or tibble containing new predictor data for which to generate predictions.

type

Type of prediction to return. One of `"auto"` (default), `"class"`, `"prob"`, or `"numeric"`. - `"auto"`: chooses `"class"` for classification and `"numeric"` for regression. - `"prob"`: returns class probabilities (only for classification). - `"class"`: returns predicted class labels. - `"numeric"`: returns predicted numeric values (for regression).

model_name

(Optional) Name of a specific model to use when `object$best_model` contains multiple models.

verbose

Logical; if `TRUE`, prints progress messages showing which models are used during prediction.

postprocess_fn

(Optional) A function to apply to the final predictions (e.g., inverse transforms, thresholding).

...

Additional arguments (currently unused).

Examples

Run this code
if (FALSE) {
  set.seed(123)
  model <- fastml(iris, label = "Species")
  test_data <- iris[sample(1:150, 20),-5]

  ## Best model(s) predictions
  preds <- predict(model, newdata = test_data)

  ## Predicted class probabilities using best model(s)
  probs <- predict(model, newdata = test_data, type = "prob")

  ## Prediction from a specific model by name
  single_model_preds <- predict(model, newdata = test_data, model_name = "rand_forest (ranger)")

}

Run the code above in your browser using DataLab