Skip to contents

This function builds an Elastic Net model using glmnet, with automatic alpha selection via cross-validation. It supports generalized linear models for survival analysis (Cox), binary classification, and regression tasks.

Usage

BuildPhenoModelAutoAlpha(
  expr,
  pheno,
  family = c("cox", "binomial", "gaussian"),
  nfolds = 10,
  seed = 2025
)

Arguments

expr

A numeric matrix or data.frame of gene expression or cell-type proportion data (samples × features).

pheno

A response vector of phenotypes: survival object, binary labels, or continuous outcomes.

family

Character string indicating the type of outcome. Must be one of "cox", "binomial", or "gaussian".

nfolds

Number of folds to use in cross-validation. Default is 10.

seed

Random seed for reproducibility. Default is 2025.

Value

A list with the following components:

model

The fitted cv.glmnet object with best alpha.

lambda

The lambda.min value corresponding to the best alpha.

alpha

The optimal alpha value selected.

Details

The function performs K-fold cross-validation across a range of alpha values to identify the optimal alpha that minimizes the cross-validation error. Once the best alpha is selected, the model is re-trained using that alpha.

Applicable families:

  • "cox": Cox proportional hazards model (survival)

  • "binomial": Logistic regression (binary classification)

  • "gaussian": Linear regression (continuous outcomes)

Author

Bin Duan

Examples

if (FALSE) { # \dontrun{
# Binary phenotype example
data("osmFISH_bulk_decon")
data("osmFISH_bulk_pheno")
model <- BuildPhenoModelAutoAlpha(
  expr = osmFISH_bulk_decon,
  pheno = osmFISH_bulk_pheno,
  family = "binomial")

# Survival example
library(survival)
surv_obj <- Surv(time = c(5, 10, 15), event = c(1, 0, 1))
model <- BuildPhenoModelAutoAlpha(
  expr = matrix(rnorm(30), nrow = 3),
  pheno = surv_obj,
  family = "cox")
} # }