-
Notifications
You must be signed in to change notification settings - Fork 31
fix #118 + unsupervised varPro wrappers (gg_beta_uvarpro / gg_sdependent) [dev] #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ehrlinger
wants to merge
15
commits into
dev
Choose a base branch
from
fix/118-gg-varpro-empty-importance
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8873932
docs: v3.1.0 documentation sweep + gg_vimp fix (CRAN release) (#109)
ehrlinger db71424
fix(vignettes): static PD surfaces + 96-dpi figures to cut install si…
ehrlinger 18156a8
docs(examples): \donttest the slow plot.gg_variable example sections …
ehrlinger a7d8052
Cut CRAN overall check time below 10 min (#114)
ehrlinger 0802143
chore: open 3.1.0.9000 dev cycle after the CRAN release (#115)
ehrlinger 168bd7c
fix(cran): v3.1.1 — clear gcc-UBSAN additional issue (varPro test ski…
ehrlinger 561ed57
fix(#118): clear error when varPro importance() is empty (v3.1.2)
ehrlinger 4d23f4f
feat(varpro): add gg_beta_uvarpro() + gg_sdependent() to 3.1.2
ehrlinger 83d8021
fix(cran): v3.1.2 — skip only isopro(method="unsupv"), the sole gcc-U…
ehrlinger fdec398
chore(cran): record 3.1.2 submission (#123)
ehrlinger 73960c8
Re-cut as 3.1.3: merge main (3.1.2) + fix pkgdown index + vignette title
ehrlinger 7084f62
review: validate inputs + drop committed dev/render artifacts
ehrlinger 5c2481d
chore: de-release — this is dev work at 3.1.2.9000, not a 3.1.3 release
ehrlinger 696abc5
Merge dev into #121: reconcile with the 3.1.2.9000 dev line
ehrlinger fef4bd5
refactor: extract validation helpers to clear cyclocomp lint
ehrlinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,3 +52,6 @@ vignettes/varpro.html | |
|
|
||
| .claude | ||
| .positai | ||
|
|
||
| # Local dev tool state (brainstorm/superpowers) | ||
| .superpowers/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| ##============================================================================= | ||
| #' Per-variable lasso-beta importance from an unsupervised varPro fit | ||
| #' | ||
| #' Tidy wrapper around [varPro::get.beta.entropy()] for a `uvarpro` object. | ||
| #' Where [gg_beta_varpro()] refines the *supervised* release-rule contrast, | ||
| #' `gg_beta_uvarpro()` does the unsupervised analogue: `uvarpro()` builds | ||
| #' entropy regions with no response, and `get.beta.entropy()` fits a | ||
| #' cross-validated lasso within each region to ask how strongly every other | ||
| #' variable explains the released variable. Averaging the absolute lasso | ||
| #' coefficients per variable gives one number per variable: an unsupervised, | ||
| #' lasso-flavoured importance. | ||
| #' | ||
| #' @details | ||
| #' `get.beta.entropy(o)` returns a (released-variable x variable) numeric | ||
| #' matrix of absolute lasso coefficients. The column mean (`na.rm = TRUE`) is | ||
| #' the per-variable importance reported here, matching the canonical | ||
| #' `sort(colMeans(beta, na.rm = TRUE), decreasing = TRUE)` idiom in the | ||
| #' `varPro::uvarpro()` help ("iowa housing - illustrates lasso importance"). | ||
| #' | ||
| #' Because `get.beta.entropy()` is expensive (a cross-validated `glmnet` per | ||
| #' region), the `beta_fit` argument accepts a pre-computed matrix so you can | ||
| #' iterate on the cutoff without re-fitting. The pairing mirrors the | ||
| #' `beta_fit` argument of [gg_beta_varpro()]. | ||
| #' | ||
| #' @param object A `uvarpro` object from [varPro::uvarpro()]. | ||
| #' @param ... Forwarded to [varPro::get.beta.entropy()] when | ||
| #' `beta_fit = NULL` (e.g. `pre.filter`, `second.stage`, `use.cv`). | ||
| #' Ignored, with a warning, when `beta_fit` is supplied. | ||
| #' @param cutoff Selection threshold on `beta_mean`. `NULL` (default) uses | ||
| #' `mean(beta_mean)`; a scalar sets it explicitly. Variables at or above the | ||
| #' cutoff are flagged `selected`. | ||
| #' @param beta_fit Optional pre-computed [varPro::get.beta.entropy()] matrix | ||
| #' for `object`. When supplied, must be a numeric matrix with column names | ||
| #' (the variables); `...` is then ignored. | ||
| #' | ||
| #' @return A `gg_beta_uvarpro` object (a `data.frame`), one row per variable, | ||
| #' most-important first, with columns: | ||
| #' \describe{ | ||
| #' \item{`variable`}{factor; levels reversed so the most-important | ||
| #' variable lands at the top after `coord_flip()` (the `gg_vimp` | ||
| #' convention).} | ||
| #' \item{`beta_mean`}{`mean(|lasso beta|)` over the released regions | ||
| #' (`colMeans(beta, na.rm = TRUE)`).} | ||
| #' \item{`n_released`}{number of regions contributing a non-`NA` | ||
| #' coefficient for the variable.} | ||
| #' \item{`selected`}{logical; `beta_mean >= cutoff`.} | ||
| #' } | ||
| #' The `provenance` attribute records `source`, `family` (`"unsupv"`), | ||
| #' `cutoff`, `n_var`, `n_released_regions`, and `precomputed`. | ||
| #' | ||
| #' @seealso [gg_beta_varpro()] (supervised analogue), [gg_udependent()], | ||
| #' [varPro::get.beta.entropy()], [varPro::uvarpro()]. | ||
| #' | ||
| #' @examples | ||
| #' \donttest{ | ||
| #' if (requireNamespace("varPro", quietly = TRUE)) { | ||
| #' set.seed(1) | ||
| #' o <- varPro::uvarpro(mtcars, ntree = 50) | ||
| #' gg <- gg_beta_uvarpro(o) | ||
| #' plot(gg) | ||
| #' } | ||
| #' } | ||
| #' | ||
| #' @export | ||
| gg_beta_uvarpro <- function(object, ..., cutoff = NULL, beta_fit = NULL) { | ||
| UseMethod("gg_beta_uvarpro", object) | ||
| } | ||
|
|
||
| #' @export | ||
| gg_beta_uvarpro.default <- function(object, ..., cutoff = NULL, | ||
| beta_fit = NULL) { | ||
| stop("gg_beta_uvarpro: expected a 'uvarpro' object from varPro::uvarpro(); ", | ||
| "got an object of class ", paste(class(object), collapse = "/"), ".", | ||
| call. = FALSE) | ||
| } | ||
|
|
||
| #' @export | ||
| gg_beta_uvarpro.uvarpro <- function(object, ..., cutoff = NULL, | ||
| beta_fit = NULL) { | ||
| if (!inherits(object, "uvarpro")) { | ||
| stop("gg_beta_uvarpro: expected a 'uvarpro' object from varPro::uvarpro().", | ||
| call. = FALSE) | ||
| } | ||
| .assert_scalar_numeric_or_null(cutoff, "cutoff", "gg_beta_uvarpro") | ||
|
|
||
| # Resolve the beta matrix (cache path) | ||
| if (is.null(beta_fit)) { | ||
| b <- varPro::get.beta.entropy(object, ...) | ||
| } else { | ||
| .validate_beta_uvarpro(beta_fit) | ||
| if (length(list(...)) > 0L) { | ||
| warning("gg_beta_uvarpro: arguments in '...' ignored because beta_fit is supplied.", | ||
| call. = FALSE) | ||
| } | ||
| b <- beta_fit | ||
| } | ||
|
|
||
| # Empty fast-path: no regions / no variables survived | ||
| if (.is_empty_beta_matrix(b)) { | ||
| return(.gg_beta_uvarpro_empty(object, beta_fit, cutoff)) | ||
| } | ||
|
|
||
| beta_mean_v <- colMeans(b, na.rm = TRUE) | ||
| n_released_v <- colSums(!is.na(b)) | ||
|
|
||
| # Most-important first; reverse the factor levels so coord_flip() puts the | ||
| # top variable at the top (matches gg_vimp / gg_beta_varpro). | ||
| ord_names <- names(sort(beta_mean_v, decreasing = TRUE)) | ||
|
|
||
| resolved_cutoff <- if (is.null(cutoff)) { | ||
| mean(beta_mean_v, na.rm = TRUE) | ||
| } else { | ||
| as.numeric(cutoff) | ||
| } | ||
|
ehrlinger marked this conversation as resolved.
ehrlinger marked this conversation as resolved.
|
||
|
|
||
| out <- data.frame( | ||
| variable = factor(ord_names, levels = rev(ord_names)), | ||
| beta_mean = unname(beta_mean_v[ord_names]), | ||
| n_released = as.integer(unname(n_released_v[ord_names])), | ||
| stringsAsFactors = FALSE | ||
| ) | ||
| out$selected <- out$beta_mean >= resolved_cutoff | ||
| rownames(out) <- NULL | ||
|
|
||
| class(out) <- c("gg_beta_uvarpro", "data.frame") | ||
| attr(out, "provenance") <- list( | ||
| source = "varPro::get.beta.entropy", | ||
| family = "unsupv", | ||
| ntree = if (!is.null(object$ntree)) as.integer(object$ntree) else NA_integer_, | ||
| cutoff = stats::setNames(resolved_cutoff, "unsupv"), | ||
| cutoff_default = is.null(cutoff), | ||
| n_var = ncol(b), | ||
| n_released_regions = nrow(b), | ||
| precomputed = !is.null(beta_fit), | ||
| xvar.names = colnames(b) | ||
| ) | ||
| out | ||
| } | ||
|
|
||
| #' @noRd | ||
| .validate_beta_uvarpro <- function(beta_fit, caller = "gg_beta_uvarpro") { | ||
| if (!is.matrix(beta_fit) || !is.numeric(beta_fit)) { | ||
| stop(caller, ": beta_fit does not look like a ", | ||
| "varPro::get.beta.entropy() result. Expected a numeric matrix.", | ||
| call. = FALSE) | ||
| } | ||
| if (ncol(beta_fit) > 0L && is.null(colnames(beta_fit))) { | ||
| stop(caller, ": beta_fit must have column names (the variables). ", | ||
| "varPro::get.beta.entropy() returns a named matrix.", | ||
| call. = FALSE) | ||
| } | ||
| invisible(NULL) | ||
| } | ||
|
|
||
| #' @noRd | ||
| .is_empty_beta_matrix <- function(m) { | ||
| is.null(m) || !is.matrix(m) || nrow(m) == 0L || ncol(m) == 0L | ||
| } | ||
|
|
||
| #' @noRd | ||
| .assert_scalar_numeric_or_null <- function(x, arg, caller) { | ||
| if (!is.null(x) && | ||
| (!is.numeric(x) || length(x) != 1L || is.na(x))) { | ||
| stop(caller, ": `", arg, "` must be a single non-NA numeric value (or NULL).", | ||
| call. = FALSE) | ||
| } | ||
| invisible(NULL) | ||
| } | ||
|
|
||
| #' @rdname print.gg | ||
| #' @export | ||
| print.gg_beta_uvarpro <- function(x, ...) { | ||
| prov <- attr(x, "provenance") | ||
| precomputed <- isTRUE(if (!is.null(prov)) prov$precomputed else FALSE) | ||
| n_regions <- if (!is.null(prov)) prov$n_released_regions %||% NA_integer_ else NA_integer_ | ||
| n_sel <- sum(x$selected, na.rm = TRUE) | ||
| cutoff <- if (!is.null(prov)) prov$cutoff %||% NA_real_ else NA_real_ | ||
| cutoff_val <- if (length(cutoff) >= 1L) cutoff[[1]] else NA_real_ | ||
| cutoff_default <- isTRUE(if (!is.null(prov)) prov$cutoff_default else FALSE) | ||
| cat(.gg_header(x, "gg_beta_uvarpro"), | ||
| sprintf(" | cutoff: %.4g%s", cutoff_val, | ||
| if (cutoff_default) " (default)" else ""), | ||
| sprintf(" | precomputed: %s", precomputed), | ||
| "\n", | ||
| sprintf(" %d of %d variables selected over %s released region(s)\n", | ||
| n_sel, nrow(x), | ||
| if (is.na(n_regions)) "NA" else format(n_regions)), | ||
| sep = "") | ||
| invisible(x) | ||
| } | ||
|
|
||
| #' @rdname summary.gg | ||
| #' @export | ||
| summary.gg_beta_uvarpro <- function(object, ...) { | ||
| v <- sort(stats::setNames(object$beta_mean, as.character(object$variable)), | ||
| decreasing = TRUE) | ||
| top <- utils::head(v, 5L) | ||
| body <- c( | ||
| sprintf("variables: %d (selected: %d)", | ||
| nrow(object), sum(object$selected, na.rm = TRUE)), | ||
| "top variables by mean |lasso beta|:", | ||
| sprintf(" %-14s %.4g", names(top), unname(top)) | ||
| ) | ||
| .summary_skel(object, "gg_beta_uvarpro", body) | ||
| } | ||
|
|
||
| #' @importFrom ggplot2 autoplot | ||
| #' @export | ||
| autoplot.gg_beta_uvarpro <- function(object, ...) { | ||
| plot.gg_beta_uvarpro(object, ...) | ||
| } | ||
|
|
||
| #' @noRd | ||
| .gg_beta_uvarpro_empty <- function(object, beta_fit, cutoff) { | ||
| out <- data.frame( | ||
| variable = factor(character(0)), | ||
| beta_mean = numeric(0), | ||
| n_released = integer(0), | ||
| selected = logical(0), | ||
| stringsAsFactors = FALSE | ||
| ) | ||
| class(out) <- c("gg_beta_uvarpro", "data.frame") | ||
| attr(out, "provenance") <- list( | ||
| source = "varPro::get.beta.entropy", | ||
| family = "unsupv", | ||
| ntree = if (!is.null(object$ntree)) as.integer(object$ntree) else NA_integer_, | ||
| cutoff = stats::setNames(if (is.null(cutoff)) NA_real_ else as.numeric(cutoff), "unsupv"), | ||
| cutoff_default = is.null(cutoff), | ||
| n_var = 0L, | ||
| n_released_regions = 0L, | ||
| precomputed = !is.null(beta_fit), | ||
| xvar.names = character(0) | ||
| ) | ||
| out | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.