print.select_auxiliary_variables_lasso_cv: Print Summary of LASSO Auxiliary Variable Selection Object

View source: R/print.select_auxvar.R

print.select_auxiliary_variables_lasso_cvR Documentation

Print Summary of LASSO Auxiliary Variable Selection Object

Description

S3 print method for objects of class select_auxiliary_variables_lasso_cv. Displays a formatted and colorized summary of the selected auxiliary variables, their grouping by outcome, selected penalty parameters (lambdas), penalty factors, stored models, goodness-of-fit metrics, coefficient estimates, and interaction metadata.

Usage

## S3 method for class 'select_auxiliary_variables_lasso_cv'
print(x, ...)

Arguments

x

An object of class select_auxiliary_variables_lasso_cv containing results from LASSO auxiliary variable selection with cross-validation. Expected to have components:

selected_variables

Character vector of variables selected across outcomes.

by_outcome

Named list, with each element a character vector of selected variables for that outcome.

selected_lambdas

Named numeric vector or list of selected lambda values by outcome.

penalty_factors

Numeric vector of penalty factors (0 = must-keep, 1 = regular penalty).

models

List of fitted models stored for each outcome.

goodness_of_fit

Named list of goodness-of-fit results by outcome, each containing cross_validated (with cv_error, cv_error_sd) and full_data (with deviance_explained, auc, accuracy, brier_score, raw_coefs).

interaction_metadata

List with interaction_terms, main_effects_in_interactions, and full_formula.

...

Additional arguments (currently ignored).

Details

Requires the crayon package for colored output.

The print method outputs information using colored text (via crayon), making it easier to visually parse the summary. It organizes output into sections:

  • Selected variables and their counts

  • Variables selected by each outcome

  • Selected lambda tuning parameters

  • Summary of penalty factors

  • Number of stored models

  • Goodness-of-fit metrics for each outcome, including cross-validation error statistics and metrics on full data fit

  • Coefficients at the lambda minimizing error, ordered by magnitude

  • Interaction terms and main effects metadata

If the crayon package is not installed, the function will stop with an error.

Value

Invisibly returns the input object x.

Examples

## ============================================================
## Example 1: Binary + continuous outcomes, with interactions
##            (prints selected vars, lambdas, GOF, coef table, interactions)
## ============================================================
if (requireNamespace("glmnet", quietly = TRUE) &&
  requireNamespace("pROC", quietly = TRUE) &&
  requireNamespace("crayon", quietly = TRUE)) {
  set.seed(123)

  n <- 180
  x1 <- rnorm(n)
  x2 <- rnorm(n)
  grp <- factor(sample(c("A", "B", "C"), n, replace = TRUE))

  ## Binary outcome with signal in x2, grp, and x1:x2 (make it a 2-level factor)
  eta <- -0.4 + 1.0 * x2 - 0.8 * (grp == "C") + 0.6 * (x1 * x2)
  p <- 1 / (1 + exp(-eta))
  y_bin <- factor(rbinom(n, 1, p), labels = c("No", "Yes"))

  ## Continuous outcome with some interaction
  y_cont <- 1.4 * x1 + 0.9 * x2 - 1.1 * (grp == "B") + 0.5 * (x1 * x2) + rnorm(n, sd = 0.7)

  df <- data.frame(y_bin = y_bin, y_cont = y_cont, x1 = x1, x2 = x2, grp = grp)

  lasso_obj1 <- select_auxiliary_variables_lasso_cv(
    df               = df,
    outcome_vars     = c("y_bin", "y_cont"),
    auxiliary_vars   = c("x1", "x2", "grp"),
    must_have_vars   = c("x1", "grp"), # 'grp' expands to its dummy columns
    check_twoway_int = TRUE, # include all two-way interactions
    nfolds           = 3,
    verbose          = FALSE,
    standardize      = TRUE,
    return_models    = FALSE, # models not stored (printer still shows GOF & coefs)
    parallel         = FALSE
  )

  ## Colorized, formatted summary:
  print(lasso_obj1)
}

## ============================================================
## Example 2: Single continuous outcome, main effects only
##            (stores model so the printer reports it)
## ============================================================
if (requireNamespace("glmnet", quietly = TRUE) &&
  requireNamespace("crayon", quietly = TRUE)) {
  set.seed(456)

  n <- 140
  a <- rnorm(n)
  b <- rnorm(n)
  f <- factor(sample(c("L", "H"), n, replace = TRUE))
  y <- 2 * a + 0.8 * b - 1.2 * (f == "H") + rnorm(n, sd = 0.8)

  toy <- data.frame(y = y, a = a, b = b, f = f)

  lasso_obj2 <- select_auxiliary_variables_lasso_cv(
    df               = toy,
    outcome_vars     = "y",
    auxiliary_vars   = c("a", "b", "f"),
    must_have_vars   = "f", # keep factor (its dummies get zero penalty)
    check_twoway_int = FALSE, # main effects only
    nfolds           = 3,
    verbose          = FALSE,
    standardize      = TRUE,
    return_models    = TRUE, # store cv.glmnet model
    parallel         = FALSE
  )

  print(lasso_obj2)
}


auxvecLASSO documentation built on Aug. 28, 2025, 9:09 a.m.