wElnet: Weighted Elastic Net prediction models for complex survey...

View source: R/welnet.R

wElnetR Documentation

Weighted Elastic Net prediction models for complex survey data

Description

A model selection function fitting Elastic Net (linear or logistic) models with sampling weights by a replicating weights method to select an optimal lambda for a given alpha by weighted error minimization. This is extended from wlasso::wlasso(), which is now officially available in wlasso. For an extended model selection for a sequence of alpha's by CPU parallel computing, use par_wElnet().

Usage

wElnet(
  data = NULL,
  col.y = NULL,
  col.x = NULL,
  cluster = NULL,
  strata = NULL,
  weights = NULL,
  design = NULL,
  family = c("gaussian", "binomial"),
  lambda = NULL,
  alpha = 1,
  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

data

A data frame with information about the response variable and covariates, as well as sampling weights, strata, and cluster indicators. It could be NULL if the sampling design were replaced in the design argument.

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.

family

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

lambda

A sequence of lambda supplied by a user.

alpha

A numeric value of the elastic net mixing parameter

0 \le \alpha \le 1,

where alpha = 1 for the LASSO penalty, and alpha = 0 for the Ridge penalty.

nlambda

An integer. The number of lambda grids to be generated. Default is nlambda = 100.

lambda.min.ratio

The smallest value for lambda, as a fraction of the maximum lambda. If NULL (default), this is computed internally: 0.01 if the number of observations is smaller than the number of covariates, otherwise 0.0001. A very small value of lambda.min.ratio will lead to a saturated fit in the nobs < nvars case. This is undefined for a "binomial" model.

method

A string indicating a method of replicate weights. Choose one of these: 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 method. 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 dCV method. FALSE, (the default option) estimates the error for each test set and defines the cross-validated error based on the average strategy. Option TRUE estimates the cross-validated error based on the pooling strategy.

train.prob

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

method.split

A string of one the following replicate weights methods to be implemented under the split method in the method argument: dCV, bootstrap or subbootstrap.

print.rw

A logical value. If TRUE, the data set with the replicate weights is saved in the output object. Default print.rw=FALSE.

standardize, offset, ...

Optional parameters to be passed to the low level function glmnet::glmnet().

Value

The output object of the function wElnet() is an object of class w.elnet:

  • lambda: A list containing information of two elements:

    • grid: A vector of all grid values for the tuning parameter.

    • min: A numeric value of an optimal tuning parameter minimizing the average error.

  • error: A list containing information of two elements:

    • average: A numeric vector indicating the average error for each tuning parameter in lambda$grid.

    • all: A numeric matrix indicating the error of each test set for each tuning parameter.

  • alpha: A numeric value of the elastic net mixing parameter used to fit this model (as supplied to the alpha argument).

  • model: A list containing information of two elements in relation to the fitted models.

    • grid: A list containing information on models fitted by each tuning parameter in the lambda$grid.

      • a0: A numeric vector of model intercepts across the whole grid of tuning parameters (hence, of the same length as lambda$grid).

      • beta: A matrix of regression coefficients for all the covariates across the whole grid of tuning parameters (the dimension of the matrix is the size of covariates by the length of lambda$grid).

      • df: A numeric vector of the degrees of freedom (i.e., the number of coefficients different from zero) across the whole grid of tuning parameters (hence, of the same length as lambda$grid).

    • final_model: An object with S3 class glmnet found in r-glmnet. A list containing information on the final model fitted by the optimal tuning parameter, lambda$min. i.e., the optimal tuning parameter selected from lambda$grid.

  • data.rw: A data frame containing the original data set and the replicate weights added to define training and test sets. Only included in the output object if print.rw=TRUE.

  • call: The call that executes this function producing the object of w.elnet.

  • Note that both model and final_model are fitted by the original data, not by one of the generated training sets by replicate weights.

See Also

svyVarSel::wlasso() for detailed arguments.

glmnet::glmnet() for detailed arguments and return values.

Examples


 data(nhanes2013_sbc)
# alpha can be selected based on `glmnet::glmnet()`:
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.dcv <- wElnet(data = nhanes2013_sbc,
              col.y = "HBP", col.x = 2:61,
              family = "binomial", alpha = 0.729,
              cluster = "SDMVPSU", strata = "SDMVSTRA", weights = "WTSAF2YR",
              method = "dCV", k=10, R=20)

# Or equivalently:
des <- survey::svydesign(ids=~SDMVPSU, strata = ~SDMVSTRA, weights = ~WTSAF2YR,
                              nest = TRUE, data = nhanes2013_sbc)
en.dcv <- wElnet(col.y = "HBP", col.x = 2:61, design = des,
              family = "binomial", alpha = alpha[10],
              method = "dCV", k=10, R=20)



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