Perform k-fold cross-validation for elnet
.
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,
...
)
data matrix with predictors
response vector
controls the balance between the L1 and the L2 penalty.
alpha = 0
is the ridge (L2) penalty, alpha = 1
is
the lasso.
size of the lambda grid if lambda
is not specified.
a grid of decreasing lambda values.
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'.
should an intercept be estimated?
number of cross validation folds.
function (name) which takes a matrix of residuals and returns a performance measure for each column.
the number of processor cores or an actual parallel
cluster. At most cv_k + 1
nodes will be used.
additional options for the EN algorithm. See
en_options
for details.
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
.
vector of lambda values.
integer specifying the exit status of the EN algorithm.
explanation of the exit status.
matrix of regression coefficients. Each column corresponds to the estimate for the lambda value at the same index.
matrix of residuals. Each column corresponds to the residuals for the lambda value at the same index.
data frame with lambda, average cross-validated performance and the estimated standard deviation.
elnet
to compute only the solution path, without
selecting the optimal penalty parameter using CV.
# 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