Learn R Programming

pense (version 1.2.1)

plot.cv_elnetfit: Plot Method for Cross-Validated Elastic Net Models

Description

Plot the cross-validation error or the coefficient path for a fitted elastic net regression model.

Usage

# S3 method for cv_elnetfit
plot(x, what = c("cv", "coef.path"), ...)

Arguments

x

a fitted, cross-validated EN model from elnet_cv.

what

plot either the cross-validated prediction error ("cv"; default) or the coefficient paths.

...

currently ignored.

Examples

Run this code
# NOT RUN {
# Generate data with highly correlated groups of variables
set.seed(12345)
n <- 100
p <- 20
x <- 1 + matrix(rnorm(n * p), ncol = p)
x[, 2] <- x[, 1] + rnorm(n, sd = 0.01)
x[, 3] <- x[, 1] + rnorm(n, sd = 0.01)
x[, 5] <- x[, 4] + rnorm(n, sd = 0.01)
x[, 6] <- x[, 4] + rnorm(n, sd = 0.01)

y <- x %*% c(rep(c(2, 5), each = 3), numeric(p - 6)) + rnorm(n)

# Compute the classical EN and select the optimal lambda by CV
set.seed(1234)
est_en <- elnet_cv(
    x, y,
    alpha = 0.5
)

# By default, `plot` shows the CV prediction error
plot(est_en)
# We can also plot the coefficient paths
plot(est_en, what = "coef.path")

# Compute the LASSO solution
set.seed(1234)
est_lasso <- elnet_cv(
    x, y,
    alpha = 1
)

plot(est_lasso)
plot(est_lasso, what = "coef.path")
# }

Run the code above in your browser using DataLab