| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/glmnet-cv-glmnet.R |
| 3 |
\name{tidy.cv.glmnet} |
| 4 |
\alias{tidy.cv.glmnet} |
| 5 |
\title{Tidy a(n) cv.glmnet object} |
| 6 |
\usage{ |
| 7 |
\method{tidy}{cv.glmnet}(x, ...) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{x}{A \code{cv.glmnet} object returned from \code{\link[glmnet:cv.glmnet]{glmnet::cv.glmnet()}}.} |
| 11 |
|
| 12 |
\item{...}{Additional arguments. Not used. Needed to match generic |
| 13 |
signature only. \strong{Cautionary note:} Misspelled arguments will be |
| 14 |
absorbed in \code{...}, where they will be ignored. If the misspelled |
| 15 |
argument has a default value, the default value will be used. |
| 16 |
For example, if you pass \code{conf.lvel = 0.9}, all computation will |
| 17 |
proceed using \code{conf.level = 0.95}. Two exceptions here are: |
| 18 |
\itemize{ |
| 19 |
\item \code{tidy()} methods will warn when supplied an \code{exponentiate} argument if |
| 20 |
it will be ignored. |
| 21 |
\item \code{augment()} methods will warn when supplied a \code{newdata} argument if it |
| 22 |
will be ignored. |
| 23 |
}} |
| 24 |
} |
| 25 |
\description{ |
| 26 |
Tidy summarizes information about the components of a model. |
| 27 |
A model component might be a single term in a regression, a single |
| 28 |
hypothesis, a cluster, or a class. Exactly what tidy considers to be a |
| 29 |
model component varies across models but is usually self-evident. |
| 30 |
If a model has several distinct types of components, you will need to |
| 31 |
specify which components to return. |
| 32 |
} |
| 33 |
\examples{ |
| 34 |
\dontshow{if (rlang::is_installed(c("glmnet", "ggplot2"))) withAutoprint(\{ # examplesIf} |
| 35 |
|
| 36 |
# load libraries for models and data |
| 37 |
library(glmnet) |
| 38 |
|
| 39 |
set.seed(27) |
| 40 |
|
| 41 |
nobs <- 100 |
| 42 |
nvar <- 50 |
| 43 |
real <- 5 |
| 44 |
|
| 45 |
x <- matrix(rnorm(nobs * nvar), nobs, nvar) |
| 46 |
beta <- c(rnorm(real, 0, 1), rep(0, nvar - real)) |
| 47 |
y <- c(t(beta) \%*\% t(x)) + rnorm(nvar, sd = 3) |
| 48 |
|
| 49 |
cvfit1 <- cv.glmnet(x, y) |
| 50 |
|
| 51 |
tidy(cvfit1) |
| 52 |
glance(cvfit1) |
| 53 |
|
| 54 |
library(ggplot2) |
| 55 |
|
| 56 |
tidied_cv <- tidy(cvfit1) |
| 57 |
glance_cv <- glance(cvfit1) |
| 58 |
|
| 59 |
# plot of MSE as a function of lambda |
| 60 |
g <- ggplot(tidied_cv, aes(lambda, estimate)) + |
| 61 |
geom_line() + |
| 62 |
scale_x_log10() |
| 63 |
g |
| 64 |
|
| 65 |
# plot of MSE as a function of lambda with confidence ribbon |
| 66 |
g <- g + geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = .25) |
| 67 |
g |
| 68 |
|
| 69 |
# plot of MSE as a function of lambda with confidence ribbon and choices |
| 70 |
# of minimum lambda marked |
| 71 |
g <- g + |
| 72 |
geom_vline(xintercept = glance_cv$lambda.min) + |
| 73 |
geom_vline(xintercept = glance_cv$lambda.1se, lty = 2) |
| 74 |
g |
| 75 |
|
| 76 |
# plot of number of zeros for each choice of lambda |
| 77 |
ggplot(tidied_cv, aes(lambda, nzero)) + |
| 78 |
geom_line() + |
| 79 |
scale_x_log10() |
| 80 |
|
| 81 |
# coefficient plot with min lambda shown |
| 82 |
tidied <- tidy(cvfit1$glmnet.fit) |
| 83 |
|
| 84 |
ggplot(tidied, aes(lambda, estimate, group = term)) + |
| 85 |
scale_x_log10() + |
| 86 |
geom_line() + |
| 87 |
geom_vline(xintercept = glance_cv$lambda.min) + |
| 88 |
geom_vline(xintercept = glance_cv$lambda.1se, lty = 2) |
| 89 |
\dontshow{\}) # examplesIf} |
| 90 |
} |
| 91 |
\seealso{ |
| 92 |
\code{\link[=tidy]{tidy()}}, \code{\link[glmnet:cv.glmnet]{glmnet::cv.glmnet()}} |
| 93 |
|
| 94 |
Other glmnet tidiers: |
| 95 |
\code{\link{glance.cv.glmnet}()}, |
| 96 |
\code{\link{glance.glmnet}()}, |
| 97 |
\code{\link{tidy.glmnet}()} |
| 98 |
} |
| 99 |
\concept{glmnet tidiers} |
| 100 |
\value{ |
| 101 |
A \code{\link[tibble:tibble]{tibble::tibble()}} with columns: |
| 102 |
\item{lambda}{Value of penalty parameter lambda.} |
| 103 |
\item{nzero}{Number of non-zero coefficients for the given lambda.} |
| 104 |
\item{std.error}{The standard error of the regression term.} |
| 105 |
\item{conf.low}{lower bound on confidence interval for cross-validation |
| 106 |
estimated loss.} |
| 107 |
\item{conf.high}{upper bound on confidence interval for cross-validation |
| 108 |
estimated loss.} |
| 109 |
\item{estimate}{Median loss across all cross-validation folds for a given |
| 110 |
lamdba} |
| 111 |
|
| 112 |
} |