par_wElnet: Parallel cross-validated Weighted Elastic Net across a...

View source: R/welnet.par.R

par_wElnetR Documentation

Parallel cross-validated Weighted Elastic Net across a sequence of alpha values

Description

Fits wElnet() once per value of alpha, assigning each alpha to its own CPU core via the parallel package, and then selects the overall best (alpha, lambda) combination by minimizing the average weighted error across all fits.

         This function always uses a \code{"PSOCK"} cluster, on every
         platform (Windows, Mac, Linux), because it is designed to run
         safely inside RStudio. RStudio manages a graphics device and
         other GUI state in the main R process; forking that process
         (\code{"FORK"} clusters) is explicitly discouraged by R's own
         \code{parallel} documentation in GUI-based sessions and can
         crash the session. \code{"PSOCK"} workers are fresh, independent
         R processes, so this risk does not apply, at the cost of a
         small amount of serialization overhead when the cluster starts.

         Because this function is part of the \code{MLSurvey} package,
         each worker simply attaches \code{MLSurvey} (along with
         \code{glmnet} and \code{survey}) via \code{library()}, which
         makes [wElnet()], [replicate_weights()], and all of their
         internal helper functions available on every worker
         automatically -- no manual \code{clusterExport()} of function
         definitions is required.

Usage

par_wElnet(
  alpha = c(0, 0.001, 0.008, 0.027, 0.064, 0.125, 0.216, 0.343, 0.512, 0.729, 1),
  n.cores = NULL,
  blas.threads = 1,
  data = NULL,
  col.y = NULL,
  col.x = NULL,
  cluster = NULL,
  strata = NULL,
  weights = NULL,
  design = NULL,
  family = c("gaussian", "binomial"),
  lambda = NULL,
  nlambda = 100,
  lambda.min.ratio = NULL,
  method = c("dCV", "JKn", "bootstrap", "subbootstrap", "BRR", "split", "extrapolation"),
  k = 10,
  R = 1,
  B = 200,
  dCV.sw.test = FALSE,
  train.prob = 0.7,
  method.split = c("dCV", "bootstrap", "subbootstrap"),
  print.rw = FALSE,
  standardize = TRUE,
  offset = NULL,
  ...
)

Arguments

alpha

A numeric vector of elastic net mixing parameters to evaluate in parallel, one per core. Default is the standard 11-point grid c(0, 0.001, 0.008, 0.027, 0.064, 0.125, 0.216, 0.343, 0.512, 0.729, 1).

n.cores

Integer number of CPU cores to use. Defaults to min(parallel::detectCores() - 1, length(alpha)).

blas.threads

Integer number of threads each worker's BLAS library is allowed to use. Defaults to 1, to avoid oversubscribing CPU cores when n.cores worker processes each also try to run multithreaded linear algebra inside glmnet. Requires the RhpcBLASctl package; if it is not installed, this is silently skipped with a message.

data

A data frame with information about the response variable and covariates, as well as sampling weights, strata, and cluster indicators. Passed to wElnet() for every alpha. It could be NULL if the sampling design were supplied in the design argument instead.

col.y

A numeric value indicating the index of the response variable or a string of the response variable name.

col.x

A numeric vector indicating indices of covariates or a vector of strings indicating these column names.

cluster

A string of a cluster identifier. It could be NULL if the sampling design were plugged in the design argument.

strata

A string of a strata identifier. It could be NULL if the sampling design were plugged in the design argument.

weights

A string of a sampling weights' identifier. It could be NULL if the sampling design were included in the design argument.

design

An object of class survey.design generated by svydesign. It could be NULL if information about cluster, strata, weights, and data were given instead.

family

A string indicating a glm family object for fitting weighted elastic net models. Choose between gaussian (linear models) or binomial (logistic models).

lambda

A sequence of lambda supplied by a user. If NULL (default), wElnet() generates its own grid for each alpha.

nlambda

An integer. The number of lambda grid values to generate when lambda is NULL. Default is 100.

lambda.min.ratio

The smallest value for lambda, as a fraction of the maximum lambda. If NULL (default), wElnet()'s own internal default is used.

method

A string indicating a method of replicate weights. Choose one of: JKn, dCV, bootstrap, subbootstrap, BRR, split, extrapolation.

k

An integer. The number of folds for the dCV method. Default is k=10.

R

An integer. The number of times the sample is partitioned for dCV, split, or extrapolation methods. Default is R=1.

B

An integer. The number of bootstrap re-samples for bootstrap and subbootstrap methods. Default is B=200.

dCV.sw.test

A logical value indicating the method for estimating the error for the dCV method. Default is FALSE.

train.prob

A numeric between 0 and 1 setting the proportion of clusters (for split) or strata (for extrapolation) in training sets. Default is train.prob = 0.7.

method.split

A string indicating which replicate weights method to use under the split method: dCV, bootstrap, or subbootstrap.

print.rw

A logical value. If TRUE, each fitted w.elnet object's replicate-weights data set is retained in its output. Default FALSE.

standardize

A logical value passed to glmnet indicating whether predictors should be standardized before fitting. Default TRUE.

offset

An optional offset vector passed to glmnet. Default NULL.

...

Any further arguments accepted by glmnet (beyond standardize and offset), forwarded to every wElnet() call.

Value

An object of class par.w.elnet, a list with:

  • results: a named list of w.elnet objects (the output of wElnet()), one per alpha.

  • summary: a data frame with columns alpha, lambda.min, and min.error, one row per alpha, sorted in the order alpha was supplied.

  • best: the single w.elnet object with the lowest min.error across all alphas.

  • best.alpha: the alpha value corresponding to best.

See Also

wElnet() for the single-alpha model fit that this function parallelizes.

Examples


library(MLSurvey)
data(nhanes2013_sbc)
alpha <- c(0.000, 0.001, 0.008, 0.027, 0.064, 0.125, 0.216, 0.343, 0.512, 0.729, 1.000)

en.par <- par_wElnet(alpha = alpha,
                      data = nhanes2013_sbc,
                      col.y = "HBP", col.x = 2:61,
                      family = "binomial",
                      cluster = "SDMVPSU", strata = "SDMVSTRA", weights = "WTSAF2YR",
                      method = "dCV", k = 10, R = 20)

en.par$summary      # error per alpha
en.par$best.alpha    # winning alpha
en.par$best          # winning w.elnet object (same structure as wElnet() output)



MLSurvey documentation built on Nov. 18, 2026, 5:11 p.m.