cv_orthoMTL: Cross-Validation for orthoMTL Hyperparameter Selection

View source: R/cv_orthoMTL.R

cv_orthoMTLR Documentation

Cross-Validation for orthoMTL Hyperparameter Selection

Description

Performs a parallelised grid search over hyperparameters for orthoMTL, evaluating each configuration via cross-validated concordance index. Returns the best configuration without retraining a final model (that is the caller's responsibility).

Usage

cv_orthoMTL(
  X.train,
  Y.train,
  W.train = NULL,
  K = NULL,
  lambdas = c(0.001, 0.01),
  alphas = 0,
  stepsizes = c(0.1, 0.5),
  diag_vals = c(0.5, 1),
  survival = TRUE,
  logistic = FALSE,
  metric = NULL,
  disjoint = FALSE,
  schedule = c("sqrt", "log", "const", "linear"),
  folds = NULL,
  n_cores = 2,
  seed = NULL,
  verbose = TRUE
)

Arguments

X.train

A numeric matrix of training features with dimensions n x p.

Y.train

A numeric matrix of training labels with dimensions n x numTasks. May contain NA for censored observations.

W.train

A numeric indicator matrix of dimensions n x numTasks, where 1 = observed and 0 = censored. Required when survival = TRUE.

K

A square constraint matrix of dimensions numTasks x numTasks. The diagonal will be overridden by values in diag_vals during the search.

lambdas

A numeric vector of regularisation parameters to search.

alphas

A numeric vector of elastic-net mixing parameters in [0, 1] to search. Default: 0 (pure orthogonality penalty, no sparsity).

stepsizes

A numeric vector of gradient descent step sizes to search.

diag_vals

A numeric vector of diagonal values for the constraint matrix K to search.

survival

Logical. Use censored survival loss? Default: TRUE.

logistic

Logical. Fit logistic (classification) models? Passed through to orthoMTL. Default: FALSE.

metric

Character; the scoring metric maximised/minimised over the grid, or NULL (default) to choose automatically by mode: "cindex" when survival = TRUE, "auc" when logistic = TRUE, otherwise "rmse". Supported values: "cindex", "auc", "accuracy" (higher is better), "rmse" (lower is better), "r2" (higher is better).

disjoint

Logical. Enforce disjoint supports? Default: FALSE.

schedule

Character; the gradient-step decay schedule passed to orthoMTL for every fit. One of "sqrt" (default), "log", "const", "linear". "log" or "const" typically reach the same optimum in far fewer iterations than the default "sqrt", which can noticeably speed up the grid search; see the schedule argument of orthoMTL for the trade-offs. Applied uniformly to all configurations (it is not part of the tuning grid).

folds

An integer vector of length n assigning each training observation to a fold. If NULL, a 5-fold assignment is generated with a warning.

n_cores

Integer. Number of cores for parallel execution. Default: 2.

seed

Optional integer random seed for reproducibility. Default: NULL (no seed is set; the ambient RNG state is used as-is). Used both for auto-generated fold assignment and for each orthoMTL fit in the grid.

verbose

Logical. Print progress information? Default: TRUE.

Details

The grid is constructed as the full Cartesian product of lambdas, alphas, stepsizes, and diag_vals. Each configuration is evaluated independently in parallel across cores. Within each configuration, folds are evaluated sequentially and the per-fold C-indices are averaged.

The best configuration is selected by joint maximisation of the mean CV C-index over the entire flattened grid (not greedy sequential search).

This function does not retrain a final model. Use the returned hyperparameters to train via orthoMTL.

Value

An object of class "cv_orthoMTL" containing:

best

A list with the best hyperparameters: lambda, alpha, stepsize, diag_val, and the corresponding cv_score.

results

A data.frame of all configurations with their mean CV C-index, sorted descending by cv_score.

folds

The fold assignment vector used.

n_configs

Total number of configurations tested.

n_folds

Number of unique folds.

call

The matched function call.

See Also

orthoMTL, cindex_mtl

Examples


set.seed(42)
n <- 50; p <- 5; n_tasks <- 3
X <- matrix(rnorm(n * p), n, p)
colnames(X) <- paste0("V", seq_len(p))
SurvTime <- rexp(n, rate = 0.1)
Event <- rbinom(n, 1, 0.7)
thresholds <- c(4, 6, 10)

Y <- create_longitudinal_labels(SurvTime, Event, thresholds)
W <- create_indicator_matrix(Y)
K <- create_constraint_matrix(n_tasks)

folds <- rep(1:2, length.out = n)

cv_res <- cv_orthoMTL(
  X.train = X, Y.train = Y, W.train = W, K = K,
  lambdas = c(1e-3, 1e-2), alphas = 0,
  stepsizes = c(0.1), diag_vals = c(0.5, 1),
  survival = TRUE, disjoint = FALSE,
  folds = folds, n_cores = 1, seed = 42, verbose = FALSE
)

print(cv_res)
cv_res$best


orthoMTL documentation built on Aug. 23, 2026, 5:10 p.m.