Learn R Programming

pense (version 1.0.8)

elnet_cv: Cross-validate Elastic Net

Description

Perform k-fold cross-validation for elnet.

Usage

elnet_cv(x, y, alpha, nlambda = 100, lambda, weights, intercept = TRUE,
  cv_k = 10, cv_measure, ncores = getOption("mc.cores", 1L), cl = NULL,
  options = en_options_aug_lars(), lambda_min_ratio, ...)

Arguments

x

data matrix with predictors

y

response vector

alpha

controls the balance between the L1 and the L2 penalty. alpha = 0 is the ridge (L2) penalty, alpha = 1 is the lasso.

nlambda

size of the lambda grid if lambda is not specified.

lambda

a grid of decreasing lambda values.

weights

an optional vector of weights to be used in the fitting process. Should be NULL or a numeric vector. If non-NULL, weighted EN is used with weights weights. See also 'Details'.

intercept

should an intercept be estimated?

cv_k

number of cross validation folds.

cv_measure

function (name) which takes a matrix of residuals and returns a performance measure for each column.

ncores, cl

the number of processor cores or an actual parallel cluster. At most cv_k + 1 nodes will be used.

options

additional options for the EN algorithm. See en_options for details.

lambda_min_ratio

if the lambda grid should be automatically defined, the ratio of the smallest to the largest lambda value in the grid. The default is 1e-6 if $n < p$, and 1e-5 * 10^floor(log10(p / n)) otherwise.

...

additional arguments passed on to elnet.

Value

lambda

vector of lambda values.

status

integer specifying the exit status of the EN algorithm.

message

explanation of the exit status.

coefficients

matrix of regression coefficients. Each column corresponds to the estimate for the lambda value at the same index.

residuals

matrix of residuals. Each column corresponds to the residuals for the lambda value at the same index.

cvres

data frame with lambda, average cross-validated performance and the estimated standard deviation.

See Also

elnet to compute only the solution path, without selecting the optimal penalty parameter using CV.

Examples

Run this code
# NOT RUN {
# Generate some dummy data
set.seed(12345)
n <- 30
p <- 15
x <- 1 + matrix(rnorm(n * p), ncol = p)
y <- x %*% c(2:5, numeric(p - 4)) + rnorm(n)

x_test <- matrix(rnorm(10 * n * p), ncol = p)
y_test <- drop(x_test %*% c(2:5, numeric(p - 4)) + rnorm(n))

# Compute the classical EN and select lambda based on CV
set.seed(1234)
est <- elnet_cv(
    x, y,
    alpha = 0.6,
    nlambda = 100
)

# The optimal lambda according to CV is
est$lambda_opt
plot(est)

# and the RMSPE at this lambda is
sqrt(mean((y_test - predict(est, x_test))^2))
# }

Run the code above in your browser using DataLab