R/tune_optuna.R
tune_fastsvm_optuna.RdPerforms Bayesian Optimization using Optuna to find the best hyperparameters
for a single kernel configuration. The search is parallelized by launching
multiple R workers (via parallel::makePSOCKcluster) that connect to a
shared SQLite database to synchronize trials.
tune_fastsvm_optuna(
data,
time_col = "t",
delta_col = "delta",
search_space,
n_trials = 50L,
cv = 5L,
cores = parallel::detectCores(),
seed = NULL,
verbose = TRUE,
...
)A data.frame containing the training data.
Character string. Name of the time column.
Character string. Name of the event column (1=event, 0=censored).
A named list defining the parameters to tune.
Use helper functions opt_float, opt_int,
or opt_cat.
Integer. Total number of trials to run (these are distributed
across the cores).
Integer. Number of cross-validation folds to use for evaluating each trial (default 5).
Integer. Number of parallel R workers to launch.
Integer or NULL. Random seed for reproducibility (passed to Optuna's sampler).
Logical. If TRUE, prints start and end messages.
Additional fixed arguments passed to FastKernelSurvivalSVM
(e.g., kernel="rbf", max_iter=1000).
An object of class "fastsvm_optuna" containing:
A list of the optimal hyperparameters found.
The best mean C-index achieved.
The number of trials performed.
The original Python Optuna study object.
if (FALSE) { # \dontrun{
if (reticulate::py_module_available("optuna") && requireNamespace("parallel")) {
library(FastSurvivalSVM)
# 1. Generate Data
df <- data_generation(n = 200, prop_cen = 0.3)
# 2. Define Search Space for an RBF Kernel
# We want to tune 'alpha' and 'gamma'.
space <- list(
alpha = opt_float(0.01, 10, log = TRUE),
gamma = opt_float(0.001, 1, log = TRUE)
)
# 3. Run Optimization using ALL available cores
res <- tune_fastsvm_optuna(
data = df,
time_col = "tempo",
delta_col = "cens",
search_space = space,
n_trials = 20,
cv = 3,
cores = parallel::detectCores(),
kernel = "rbf",
rank_ratio = 0.0 # Fixed parameter
)
print(res$best_params)
print(res$best_score)
# 4. Train Final Model with Best Parameters
# Using the optimized hyperparameters to fit the model on full data.
# Combine fixed arguments (kernel, rank_ratio) with tuned ones (alpha, gamma)
final_args <- c(
list(
data = df,
time_col = "tempo",
delta_col = "cens",
kernel = "rbf",
rank_ratio = 0.0
),
res$best_params
)
# Fit using fastsvm()
final_model <- do.call(fastsvm, final_args)
print(final_model)
}
} # }