R/glmnet_coef_plot.R

Defines functions glmnet_coef_plot

Documented in glmnet_coef_plot

#' Create Lambda chart for glmnet object
#'
#' @param fit parsnip fit object
#'
#' @return ggplot2 object
#' @export
#'
#' @examples
#' library(parsnip)
#' library(glmnet)
#' linear_reg_glmnet_spec <-
#'   linear_reg(penalty = 0, mixture = 1) %>%
#'   set_engine('glmnet')
#'
#' lm_fit <- fit(linear_reg_glmnet_spec, data = mtcars, mpg ~ .)
#'
#' glmnet_coef_plot(lm_fit)
glmnet_coef_plot <- function(fit) {
  as_tibble(t(as.matrix(fit$fit$beta))) %>%
    mutate(lambda = fit$fit$lambda) %>%
    pivot_longer(-lambda) %>%
    ggplot(aes(lambda, value, group = name)) +
    geom_line() +
    scale_x_log10()
}
EmilHvitfeldt/horus documentation built on June 12, 2021, 7 p.m.