train_and_predict: Train a classifier and predict new samples

View source: R/machinelearning.R

train_and_predictR Documentation

Train a classifier and predict new samples

Description

Train a classification model from a dataset and use the fitted model to predict the class labels of new samples.

Usage

train_and_predict(
  dataset,
  new.samples,
  column.class,
  model,
  validation,
  num.folds = 10,
  num.repeats = 10,
  tunelength = 10,
  tunegrid = NULL,
  metric = NULL,
  summary.function = caret::defaultSummary
)

Arguments

dataset

A dataset object containing data and metadata.

new.samples

A data frame or matrix with new samples to classify.

column.class

The metadata column containing the class labels.

model

A model name accepted by caret::train().

validation

Validation method, such as "boot", "boot632", "cv", "repeatedcv", "LOOCV", "LGOCV", or "oob" where supported.

num.folds

Number of folds used in resampling.

num.repeats

Number of repeats used in repeated resampling.

tunelength

Number of tuning levels evaluated by caret.

tunegrid

Optional data frame of tuning parameter combinations.

metric

Optional performance metric used for model selection.

summary.function

Summary function passed to caret::trainControl().

Value

A list with two elements: train.result, the fitted training object, and predictions.result, a data frame with predicted classes for new.samples.

Examples

## Not run: 
datamat <- matrix(
  rnorm(24),
  nrow = 4,
  dimnames = list(paste0("v", 1:4), paste0("s", 1:6))
)
metadata <- data.frame(class = factor(c("A", "A", "A", "B", "B", "B")))
dataset <- list(data = datamat, metadata = metadata)
new.samples <- datamat[, 1:2, drop = FALSE]
train_and_predict(dataset, new.samples, "class", model = "rpart", validation = "cv")

## End(Not run)

specmine documentation built on Aug. 5, 2026, 5:06 p.m.