Internal LightGBM Hyperparameter Tuning and Model Fitting
fitLGB.RdfitLGB() is an internal helper function invoked during train to perform
hyperparameter grid searches and train final gradient boosting models using the
lightgbm package. It evaluates candidate hyperparameter sets via K-fold
cross-validation or a single validation split, selects the combination minimizing the
loss metric, and returns a final model trained on the complete dataset.
Arguments
- dfull
An
lgb.Datasetobject containing the full dataset (predictor matrix and target outcome) used for the final model fit.- dtrain
An optional
lgb.Datasetobject containing the training subset when validation-set evaluation is performed. Default isNULL.- dvalid
An optional
lgb.Datasetobject containing the validation subset used for early stopping and performance comparison. Default isNULL.- cv.folds
A list of integer vectors specifying predefined fold indices for K-fold cross-validation (via
lgb.cv), or a logical flag (TRUE) when a single validation split is used.- hyper.grid
A list of lists or data frame/grid where each element represents a specific hyperparameter combination (e.g., learning rate, tree depth, feature fraction) to evaluate.
- params.obj
A named list of global LightGBM parameters, including objective setting, metric definitions, and thread counts.
Value
An object of class lgb.Booster containing the final trained model,
augmented with $best_score, $best_iter, and $record_evals metadata summarizing the tuning process.
Details
Internal Fitting Routine for LightGBM Models
Workflow:
Cross-Validation / Validation Evaluation: Loops through each candidate row in
hyper.grid. Depending on whethercv.foldsis a list of fold indices or a logical flag, it executeslgb.cvorlgb.trainwith early stopping enabled (early_stopping_rounds = 2L).Optimal Selection: Compares
best_scoreacross all candidate hyperparameter sets, identifying the set that achieved the lowest evaluation loss and its associated optimal number of boosting iterations (best_iter).Final Model Training: Refits the model on the full dataset (
dfull) using the optimal hyperparameters and fixed iteration count (num_iterations = best_iter).Result Augmentation: Attaches a metadata data.frame containing the grid search results to the returned
lgb.Boosterobject's$record_evalsslot.