cva.glmnet: Do elastic net cross-validation for alpha and lambda...

View source: R/cvaGlmnetFormula.r

cva.glmnetR Documentation

Do elastic net cross-validation for alpha and lambda simultaneously

Description

Do elastic net cross-validation for alpha and lambda simultaneously

Usage

cva.glmnet(x, ...)

## Default S3 method:
cva.glmnet(
  x,
  y,
  alpha = seq(0, 1, len = 11)^3,
  nfolds = 10,
  foldid = sample(rep(seq_len(nfolds), length = nrow(x))),
  ...,
  outerParallel = NULL,
  checkInnerParallel = TRUE
)

## S3 method for class 'formula'
cva.glmnet(
  formula,
  data,
  ...,
  weights = NULL,
  offset = NULL,
  subset = NULL,
  na.action = getOption("na.action"),
  drop.unused.levels = FALSE,
  xlev = NULL,
  sparse = FALSE,
  use.model.frame = FALSE
)

## S3 method for class 'cva.glmnet'
predict(
  object,
  newx,
  alpha,
  which = match(TRUE, abs(object$alpha - alpha) < 1e-08),
  ...
)

## S3 method for class 'cva.glmnet.formula'
predict(
  object,
  newdata,
  alpha,
  which = match(TRUE, abs(object$alpha - alpha) < 1e-08),
  na.action = na.pass,
  ...
)

## S3 method for class 'cva.glmnet'
coef(
  object,
  alpha,
  which = match(TRUE, abs(object$alpha - alpha) < 1e-08),
  ...
)

## S3 method for class 'cva.glmnet.formula'
print(x, ...)

## S3 method for class 'cva.glmnet'
plot(x, ..., legend.x = xlim[1], legend.y = xlim[2], log.x = TRUE)

minlossplot(x, ...)

## S3 method for class 'cva.glmnet'
minlossplot(x, ..., cv.type = c("1se", "min"))

Arguments

x

A matrix of predictor variables; or for the plotting methods, an object returned by cva.glmnet.

...

Further arguments to be passed to lower-level functions. In the case of cva.glmnet, these arguments are passed to cv.glmnet; for predict and coef, they are passed to predict.cv.glmnet; and for plot and minlossplot, to plot.

y

A response vector or matrix (for a multinomial response).

alpha

A vector of alpha values for which to do cross-validation. The default is a sequence of 11 values more closely spaced around alpha = 0. For the predict and coef methods, the specific value of alpha for which to return predictions/regression coefficients.

nfolds

The number of cross-validation folds to use. Defaults to 10.

foldid

Vector of fold IDs for cross-validation. See glmnet::cv.glmnet.

outerParallel

Method of parallelising the outer loop over alpha. See 'Details' below. If NULL, the loop is run sequentially.

checkInnerParallel

If the outer loop is run in parallel, check that the inner loop over lambda will not be in contention for cores.

formula

A model formula; interaction terms are allowed and will be expanded per the usual rules for linear models.

data

A data frame or matrix containing the variables in the formula.

weights

An optional vector of case weights to be used in the fitting process. If missing, defaults to an unweighted fit.

offset

An optional vector of offsets, an a priori known component to be included in the linear predictor.

subset

An optional vector specifying the subset of observations to be used to fit the model.

na.action

A function which indicates what should happen when the data contains missing values. For the predict method, na.action = na.pass will predict missing values with NA; na.omit or na.exclude will drop them.

drop.unused.levels

Should factors have unused levels dropped? Defaults to FALSE.

xlev

A named list of character vectors giving the full set of levels to be assumed for each factor.

sparse

Should the model matrix be in sparse format? This can save memory when dealing with many factor variables, each with many levels.

use.model.frame

Should the base model.frame function be used when constructing the model matrix? This is the standard method that most R modelling functions use, but has some disadvantages. The default is to avoid model.frame and construct the model matrix term-by-term; see discussion.

object

For the predict and coef methods, an object returned by cva.glmnet.

newx

For the predict method, a matrix of predictor variables.

which

An alternative way of specifying alpha; the index number of the desired value within the alpha vector. If both which and alpha are supplied, the former takes precedence.

newdata

For the predict and coef methods, a data frame containing the observations for which to calculate predictions.

legend.x, legend.y

Location for the legend. Defaults to the top-left corner of the plot. Set either of these to NULL to omit the legend.

log.x

Whether to plot the X-axis (lambda) on the log scale. Defaults to TRUE, which for most lambda sequences produces a more reasonable looking plot. If your lambda sequence includes zero, set this to FALSE.

cv.type

For minlossplot, which cross-validated loss value to plot for each value of alpha. This can be either "min" which is the minimum loss, or "1se" which is the highest loss within 1 standard error of the minimum. The default is "1se".

Details

The cva.glmnet function does simultaneous cross-validation for both the alpha and lambda parameters in an elastic net model. The procedure is as outlined in the documentation for glmnet::cv.glmnet: it creates a vector foldid allocating the observations into folds, and then calls cv.glmnet in a loop over different values of alpha, but the same values of foldid each time.

Optionally this loop over alpha can be parallelised; currently, cva.glmnet knows about two methods of doing so:

  • Via parLapply in the parallel package. To use this, set outerParallel to a valid cluster object created by makeCluster.

  • Via rxExec as supplied by Microsoft R Server's RevoScaleR package. To use this, set outerParallel to a valid compute context created by RxComputeContext, or a character string specifying such a context.

If the outer loop is run in parallel, cva.glmnet can check if the inner loop (over lambda) is also set to run in parallel, and disable this if it would lead to contention for cores. This is done if it is likely that the parallelisation is local on a multicore machine, ie if outerParallel is a SOCKcluster object running on "localhost", or if the RevoScaleR compute context is local parallel.

There are two ways in which the matrix of predictors can be generated. The default, with use.model.frame = FALSE, is to process the additive terms in the formula independently. With wide datasets, this is much faster and more memory-efficient than the standard R approach which uses the model.frame and model.matrix functions. However, the resulting model object is not exactly the same as if the standard approach had been used; in particular, it lacks a bona fide terms object. If you require interoperability with other packages that assume the standard model object structure, set use.model.frame = TRUE. See discussion for more information on this topic.

The predict method computes predictions for a specific alpha value given a cva.glmnet object. It looks up the supplied alpha (possibly supplied indirectly via the which argument) in the object's stored alpha vector, and calls glmnet:::predict.cv.glmnet on the corresponding cv.glmnet fit. All the arguments to that function are (or should be) supported.

The coef method is similar, returning the coefficients for the selected alpha value via glmnet:::coef.cv.glmnet.

The plot method for cva.glmnet objects plots the average cross-validated loss by lambda, for each value of alpha. Each line represents one cv.glmnet fit, corresponding to one value of alpha. Note that the specific lambda values can vary substantially by alpha.

The minlossplot function gives the best (lowest) cross-validated loss for each value of alpha.

Value

For cva.glmnet.default, an object of class cva.glmnet. This is a list containing the following:

  • alpha The vector of alpha values

  • nfolds The number of folds

  • modlist A list of cv.glmnet objects, containing the cross-validation results for each value of alpha

The function cva.glmnet.formula adds a few more components to the above, to facilitate working with formulas.

For the predict method, a vector or matrix of predicted values.

For the coef method, a vector of regularised regression coefficients.

See Also

glmnet::cv.glmnet

glmnet::predict.cv.glmnet, glmnet::coef.cv.glmnet

cva.glmnet, glmnet::cv.glmnet, plot

Examples

cva <- cva.glmnet(mpg ~ ., data=mtcars)
predict(cva, mtcars, alpha=1)

## Not run: 

# Leukemia example dataset from Trevor Hastie's website
download.file("https://web.stanford.edu/~hastie/glmnet/glmnetData/Leukemia.RData",
              "Leukemia.RData")
load("Leukemia.Rdata")
leuk <- do.call(data.frame, Leukemia)
leuk.cva <- cva.glmnet(y ~ ., leuk, family="binomial")
leuk.pred <- predict(leuk.cva, leuk, which=6)

## End(Not run)

glmnetUtils documentation built on Sept. 10, 2023, 5:06 p.m.