| 1 |
#' @templateVar class ridgelm |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x A `ridgelm` object returned from [MASS::lm.ridge()]. |
| 5 |
#' @template param_unused_dots |
| 6 |
#' |
| 7 |
#' @evalRd return_tidy("lambda", "GCV", "term", |
| 8 |
#' estimate = "estimate of scaled coefficient using this lambda", |
| 9 |
#' scale = "Scaling factor of estimated coefficient" |
| 10 |
#' ) |
| 11 |
#' |
| 12 |
#' @examplesIf rlang::is_installed(c("MASS", "ggplot2")) |
| 13 |
#' |
| 14 |
#' # load libraries for models and data |
| 15 |
#' library(MASS) |
| 16 |
#' |
| 17 |
#' names(longley)[1] <- "y" |
| 18 |
#' |
| 19 |
#' # fit model and summarizd results |
| 20 |
#' fit1 <- lm.ridge(y ~ ., longley) |
| 21 |
#' tidy(fit1) |
| 22 |
#' |
| 23 |
#' fit2 <- lm.ridge(y ~ ., longley, lambda = seq(0.001, .05, .001)) |
| 24 |
#' td2 <- tidy(fit2) |
| 25 |
#' g2 <- glance(fit2) |
| 26 |
#' |
| 27 |
#' # coefficient plot |
| 28 |
#' library(ggplot2) |
| 29 |
#' ggplot(td2, aes(lambda, estimate, color = term)) + |
| 30 |
#' geom_line() |
| 31 |
#' |
| 32 |
#' # GCV plot |
| 33 |
#' ggplot(td2, aes(lambda, GCV)) + |
| 34 |
#' geom_line() |
| 35 |
#' |
| 36 |
#' # add line for the GCV minimizing estimate |
| 37 |
#' ggplot(td2, aes(lambda, GCV)) + |
| 38 |
#' geom_line() + |
| 39 |
#' geom_vline(xintercept = g2$lambdaGCV, col = "red", lty = 2) |
| 40 |
#' |
| 41 |
#' @export |
| 42 |
#' @aliases ridgelm_tidiers |
| 43 |
#' @family ridgelm tidiers |
| 44 |
#' @seealso [tidy()], [MASS::lm.ridge()] |
| 45 |
tidy.ridgelm <- function(x, ...) { |
| 46 |
if (length(x$lambda) == 1) { |
| 47 |
# only one choice of lambda |
| 48 |
ret <- tibble( |
| 49 |
lambda = x$lambda, |
| 50 |
GCV = unname(x$GCV), |
| 51 |
term = names(x$coef), |
| 52 |
estimate = x$coef, |
| 53 |
scale = x$scales |
| 54 |
) |
| 55 |
return(ret) |
| 56 |
} |
| 57 |
|
| 58 |
# otherwise, multiple lambdas/coefs/etc, have to tidy |
| 59 |
cotidy <- data.frame( |
| 60 |
unrowname(t(x$coef)), |
| 61 |
lambda = x$lambda, |
| 62 |
GCV = unname(x$GCV) |
| 63 |
) |> |
| 64 |
pivot_longer( |
| 65 |
cols = c(dplyr::everything(), -lambda, -GCV), |
| 66 |
names_to = "term", |
| 67 |
values_to = "estimate" |
| 68 |
) |> |
| 69 |
as.data.frame() |> |
| 70 |
mutate(term = as.character(term)) |> |
| 71 |
mutate(scale = x$scales[term]) |
| 72 |
|
| 73 |
as_tibble(cotidy) |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
#' @templateVar class ridgelm |
| 78 |
#' @template title_desc_glance |
| 79 |
#' |
| 80 |
#' @inherit tidy.ridgelm params examples |
| 81 |
#' |
| 82 |
#' @evalRd return_glance( |
| 83 |
#' kHKB = "modified HKB estimate of the ridge constant", |
| 84 |
#' kLW = "modified L-W estimate of the ridge constant", |
| 85 |
#' lambdaGCV = "choice of lambda that minimizes GCV" |
| 86 |
#' ) |
| 87 |
#' |
| 88 |
#' @details This is similar to the output of `select.ridgelm`, but it is |
| 89 |
#' returned rather than printed. |
| 90 |
#' |
| 91 |
#' @export |
| 92 |
#' @family ridgelm tidiers |
| 93 |
#' @seealso [glance()], [MASS::select.ridgelm()], [MASS::lm.ridge()] |
| 94 |
glance.ridgelm <- function(x, ...) { |
| 95 |
as_glance_tibble( |
| 96 |
kHKB = x$kHKB, |
| 97 |
kLW = x$kLW, |
| 98 |
lambdaGCV = x$lambda[which.min(x$GCV)], |
| 99 |
na_types = "rrr" |
| 100 |
) |
| 101 |
} |