Learn R Programming

RobustPrediction (version 0.1.7)

tuneandtrainExtLasso: Tune and Train External Lasso

Description

This function tunes and trains a Lasso classifier using the glmnet package. It provides two strategies for tuning hyperparameters based on the estperf argument:

  • When estperf = FALSE (default): Hyperparameters are tuned using the external validation dataset. The lambda value that gives the highest AUC on the external dataset is selected as the best model. However, no AUC value is returned in this case, as per best practices.

  • When estperf = TRUE: Hyperparameters are tuned internally using the training dataset. The model is then validated on the external dataset to provide a conservative (slightly pessimistic) AUC estimate.

Usage

tuneandtrainExtLasso(
  data,
  dataext,
  estperf = FALSE,
  maxit = 120000,
  nlambda = 100
)

Value

A list containing the following components:

  • best_lambda: The optimal lambda value determined during the tuning process.

  • best_model: The trained Lasso model using the selected lambda value.

  • est_auc: The AUC value evaluated on the external dataset. This is only returned when estperf = TRUE, providing a conservative (slightly pessimistic) estimate of the model's performance.

  • active_set_Train: The number of active coefficients (non-zero) in the model trained on the training dataset.

Arguments

data

A data frame containing the training data. The first column should be the response variable (factor), and the remaining columns should be the predictor variables.

dataext

A data frame containing the external validation data. The first column should be the response variable (factor), and the remaining columns should be the predictor variables.

estperf

A logical value indicating whether to use internal tuning with external validation (TRUE) or external tuning (FALSE). Default is FALSE.

maxit

An integer specifying the maximum number of iterations. Default is 120000.

nlambda

An integer specifying the number of lambda values to use in the Lasso model. Default is 100.

Examples

Run this code
# Load sample data
data(sample_data_train)
data(sample_data_extern)

# Example usage with external tuning (default)
result <- tuneandtrainExtLasso(sample_data_train, sample_data_extern, maxit = 120000, nlambda = 100)
print(result$best_lambda)
print(result$best_model)
print(result$active_set_Train)

# Example usage with internal tuning and external validation
result_internal <- tuneandtrainExtLasso(sample_data_train, sample_data_extern, 
  estperf = TRUE, maxit = 120000, nlambda = 100)
print(result_internal$best_lambda)
print(result_internal$best_model)
print(result_internal$est_auc)
print(result_internal$active_set_Train)

Run the code above in your browser using DataLab