simulate_mtl: Simulate Multi-Task Data with Time-Varying Effects

View source: R/simulate.R

simulate_mtlR Documentation

Simulate Multi-Task Data with Time-Varying Effects

Description

Generates a realistic simulated dataset with binary and continuous features and a known ground-truth coefficient structure. The mode argument selects the response type: piecewise-exponential survival times (default), multi-task regression targets, or multi-task binary classification labels. Designed for demonstrating and testing orthoMTL.

Usage

simulate_mtl(
  n = 200,
  p = 30,
  n_signals = 5,
  n_continuous = 1,
  thresholds = c(4, 6, 10, 15),
  mode = c("survival", "regression", "classification"),
  noise_sd = 1,
  censoring_max = 25,
  baseline_hazard = 0.05,
  effect_strength = 0.8,
  treatment_effect = -0.3,
  seed = NULL
)

Arguments

n

Number of patients. Default: 200.

p

Number of features excluding the treatment column. Default: 30.

n_signals

Number of features with true non-zero effects. Default: 5. Must be <= p.

n_continuous

Number of continuous features (placed first in the feature matrix). Default: 1. Remaining features are binary.

thresholds

Numeric vector of time thresholds defining the task structure. Default: c(4, 6, 10, 15).

mode

Character; the response type to generate. One of "survival" (default), "regression", or "classification". The feature matrix and ground-truth coefficients are generated identically across modes; only the response differs:

survival

piecewise-exponential SurvTime/Event (the historical behaviour).

regression

Y = X %*% beta + N(0, noise_sd^2), returned as the n x length(thresholds) matrix Y.

classification

binary labels in \{-1, +1\} sampled from P(Y = +1) = 1 / (1 + e^{-X beta}), returned as Y. This matches the label encoding orthoMTL optimises with logistic = TRUE.

noise_sd

Standard deviation of the Gaussian noise added in mode = "regression". Ignored otherwise. Default: 1.

censoring_max

Maximum censoring time. Censoring times are drawn from Unif(0, censoring_max). Default: 25.

baseline_hazard

Baseline hazard rate per interval. Default: 0.05.

effect_strength

Multiplier controlling the magnitude of feature effects. Default: 0.5.

treatment_effect

Effect of treatment on the log-hazard (negative = protective). Default: -0.3.

seed

Optional random seed for reproducibility. Default: NULL (no seed is set; the ambient RNG state is used as-is). Pass an integer to make the simulated data reproducible.

Details

Feature structure:

  • Continuous features are drawn from N(0, 1).

  • Binary features have prevalences drawn from Beta(2, 10), producing a realistic range (~5-30%).

  • Treatment is balanced 1:1 via random assignment.

Effect templates: Each signal feature is assigned one of five temporal patterns:

  • "early": strong effect at early thresholds, fading to zero at late.

  • "late": zero at early thresholds, emerging at late.

  • "constant": equal effect across all thresholds (detectable by standard Cox models).

  • "increasing": effect grows over time.

  • "decreasing": effect shrinks over time.

Templates are assigned cyclically across signal features with random sign (risk-increasing or protective).

Survival time generation: Uses a piecewise-exponential model where the hazard in each interval is h_k(i) = baseline_hazard * exp(X[i, ] %*% beta[, k]). Patients progress through intervals sequentially; an event occurs when the simulated time within an interval is shorter than the interval width.

Censoring: Independent of event times. C ~ Unif(0, censoring_max). Observed time = min(T, C), event indicator = T <= C.

Value

An object of class "simulated_mtl" containing:

mode

The response type generated.

Y

For mode = "regression" / "classification", the n x length(thresholds) response matrix. NULL in survival mode (use SurvTime/Event instead).

SurvTime, Event

Survival mode only (NULL otherwise): observed times and event indicators.

X

Numeric matrix of dimensions n x (p + 1). Columns include n_continuous continuous features (standard normal), p - n_continuous binary features (prevalences drawn from Beta(2, 10)), and a treatment column (balanced 1:1).

SurvTime

Numeric vector of observed survival times.

Event

Binary vector: 1 = event observed, 0 = censored.

treatment

Binary vector (also present as last column of X).

feature_names

Character vector of column names of X.

ground_truth

A list containing:

coefficients

Matrix of true coefficients with dimensions (p + 1) x length(thresholds).

signal_features

Names of features with non-zero effects.

null_features

Names of features with zero effects.

effect_types

Named character vector mapping signal features to their temporal effect template.

thresholds

The thresholds used.

baseline_hazard

The baseline hazard used.

n, p, n_signals, n_continuous, thresholds, seed

Input parameters stored for reference.

call

The matched function call.

See Also

create_longitudinal_labels, orthoMTL

Examples

# Generate simulated data
sim <- simulate_mtl(n = 100, p = 20, n_signals = 4, seed = 42)
sim

# Inspect ground truth
sim$ground_truth$signal_features
sim$ground_truth$effect_types
sim$ground_truth$coefficients[sim$ground_truth$signal_features, ]

# Use in orthoMTL workflow

thresholds <- c(4, 6, 10, 15)
Y <- create_longitudinal_labels(sim$SurvTime, sim$Event, thresholds)
W <- create_indicator_matrix(Y)
K <- create_constraint_matrix(length(thresholds))

fit <- orthoMTL(sim$X, Y, lambda = 1e-3, K = K,
                survival = TRUE, censored.mat = W)
summary(fit)


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