-
-
Notifications
You must be signed in to change notification settings - Fork 38
Add LOO Difference Plot #178
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
Draft
ParadaCarleton
wants to merge
23
commits into
stan-dev:master
Choose a base branch
from
ParadaCarleton:master
base: master
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.
+440
−0
Draft
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
dca229b
Add LOO variation plot
8786be3
typo
8e329d0
Typo
90ecb40
Rename file
48b97b6
Made recommended changes
5399cb8
Added continuous example, removed quantile option
4d085d6
Fixed error in example
d4b8641
Typo
a0417dd
Typo/formatting
d78e40c
Merge branch 'master' into master
florence-bockting 30873f2
Merge branch 'master' into master
VisruthSK 33094d7
Formatting/style changes
VisruthSK b565fb8
Merge branch 'master' of https://github.com/ParadaCarleton/loo into P…
VisruthSK e12b456
Few renames and some small R things
VisruthSK 5c23d92
No vertical jitter, removed bayesplot roxygen template refs
VisruthSK 7932852
Simpler example; removed outlier threshold; use checkmate
VisruthSK a22b870
Small language changes to sort_by_group
VisruthSK 0d5456d
Pass data to ggplot instead of capturing
VisruthSK ec754ac
Added back thresholded labelling
VisruthSK a49df8a
Small changes
VisruthSK 74f4aa6
Added non-snapshot tests
VisruthSK 2fb210d
Added func to model comparision section
VisruthSK 0137060
labels require label_threshold
VisruthSK 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
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
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,213 @@ | ||
| #' Compare models across domains | ||
| #' | ||
| #' The LOO difference plot shows how the ELPD of two different models | ||
| #' changes when a predictor is varied. This can be useful for identifying | ||
| #' opportunities for model stacking or expansion. Pointwise differences | ||
| #' are computed as `loo_1 - loo_2`, so positive values indicate better | ||
| #' predictive performance for `loo_1`. | ||
| #' | ||
| #' @param y A vector of observations. | ||
| #' @param loo_1,loo_2 Objects returned by [loo()]. | ||
| #' @param group An optional grouping variable with the same length as `y`. | ||
| #' Points are colored according to group membership. | ||
| #' @param size,alpha Point size and opacity passed to [ggplot2::geom_point()]. | ||
| #' @param jitter Amount of horizontal jitter passed to | ||
| #' [ggplot2::position_jitter()]. | ||
| #' @param sort_by_group If `TRUE`, observations are ordered by `group` | ||
| #' and the x-axis is replaced by a sequential index. The supplied `y` values | ||
| #' are therefore not used as x coordinates. Plotting by index can be useful | ||
| #' when categories have very different sample sizes. To control the group | ||
| #' order, supply `group` as a factor with levels in the desired order. | ||
| #' @param label_threshold Optional nonnegative threshold for labeling | ||
| #' observations. Observations for which the absolute pointwise ELPD | ||
| #' difference exceeds this value are labeled. If `NULL`, no observations | ||
| #' are labeled. | ||
| #' @param labels Optional vector of labels with the same length as `y`, used | ||
| #' for observations selected by `label_threshold`. If `NULL`, observation | ||
| #' indices are used. | ||
| #' | ||
| #' @template bayesvis-reference | ||
| #' | ||
| #' @return A [ggplot2::ggplot()] object. | ||
| #' | ||
| #' @examples | ||
| #' # Artificial example | ||
| #' log_lik <- example_loglik_matrix() | ||
| #' shift <- seq(-0.5, 0.5, length.out = ncol(log_lik)) | ||
| #' log_lik_2 <- sweep(log_lik, 2, shift, FUN = "+") | ||
| #' | ||
| #' loo_1 <- loo(log_lik) | ||
| #' loo_2 <- loo(log_lik_2) | ||
| #' | ||
| #' plot_loo_difference( | ||
| #' seq_len(ncol(log_lik)), | ||
| #' loo_1, | ||
| #' loo_2 | ||
| #' ) | ||
| #' | ||
| #' # Label observations with large pointwise ELPD differences | ||
| #' plot_loo_difference( | ||
| #' seq_len(ncol(log_lik)), | ||
| #' loo_1, | ||
| #' loo_2, | ||
| #' label_threshold = 0.3 | ||
| #' ) | ||
| #' | ||
| #' # Create interspersed groups, then sort them in the plot | ||
| #' group <- rep(c("A", "A", "A", "B"), length.out = ncol(log_lik)) | ||
| #' | ||
| #' plot_loo_difference( | ||
| #' seq_len(ncol(log_lik)), | ||
| #' loo_1, | ||
| #' loo_2, | ||
| #' group = group, | ||
| #' sort_by_group = TRUE | ||
| #' ) | ||
| #' | ||
| #' @export | ||
| plot_loo_difference <- | ||
| function( | ||
| y, | ||
| loo_1, | ||
| loo_2, | ||
| group = NULL, | ||
| size = 1, | ||
| alpha = 1, | ||
| jitter = 0, | ||
| sort_by_group = FALSE, | ||
| label_threshold = NULL, | ||
| labels = NULL | ||
| ) { | ||
| if (!requireNamespace("ggplot2", quietly = TRUE)) { | ||
| stop( | ||
| "Please install 'ggplot2' to use `plot_loo_difference()`.", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
|
|
||
| checkmate::assert_flag(sort_by_group) | ||
| loo_compare_checks(nlist(loo_1, loo_2)) | ||
|
|
||
| # elpd_diffs(a, b) computes b - a | ||
| elpd_diff <- elpd_diffs(loo_2, loo_1) | ||
|
|
||
| checkmate::assert_atomic_vector( | ||
| y, | ||
| len = length(elpd_diff) | ||
| ) | ||
|
|
||
| if (!is.null(group)) { | ||
| checkmate::assert_atomic_vector( | ||
| group, | ||
| len = length(y), | ||
| any.missing = FALSE | ||
| ) | ||
| } | ||
|
|
||
| if (!is.null(labels)) { | ||
| checkmate::assert_atomic_vector( | ||
| labels, | ||
| len = length(y) | ||
| ) | ||
|
|
||
| if (is.null(label_threshold)) { | ||
| stop( | ||
| "`label_threshold` must be supplied when `labels` is supplied.", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| if (!is.null(label_threshold)) { | ||
| checkmate::assert_number( | ||
| label_threshold, | ||
| lower = 0, | ||
| finite = TRUE | ||
| ) | ||
| } | ||
|
|
||
| if (!is.null(label_threshold) && is.null(labels)) { | ||
| labels <- seq_along(y) | ||
| } | ||
|
|
||
| if (sort_by_group) { | ||
| if (is.null(group)) { | ||
| stop( | ||
| "`group` must be supplied when `sort_by_group = TRUE`.", | ||
| call. = FALSE | ||
| ) | ||
| } | ||
|
|
||
| ordering <- order(group) | ||
| elpd_diff <- elpd_diff[ordering] | ||
| group <- group[ordering] | ||
|
|
||
| if (!is.null(labels)) { | ||
| labels <- labels[ordering] | ||
| } | ||
|
|
||
| y <- seq_along(elpd_diff) | ||
| } | ||
|
|
||
| plot_data <- data.frame( | ||
| y = y, | ||
| elpd_diff = elpd_diff | ||
| ) | ||
|
|
||
| if (!is.null(group)) { | ||
| plot_data$group <- factor(group) | ||
| } | ||
|
|
||
| if (!is.null(label_threshold)) { | ||
| plot_data$labels <- ifelse( | ||
| abs(plot_data$elpd_diff) > label_threshold, | ||
| as.character(labels), | ||
| "" | ||
| ) | ||
| } | ||
|
|
||
| jitter_position <- ggplot2::position_jitter( | ||
| width = jitter, | ||
| height = 0, | ||
| seed = 1 | ||
| ) | ||
|
|
||
| plot <- ggplot2::ggplot( | ||
| data = plot_data, | ||
| mapping = ggplot2::aes(x = y, y = elpd_diff) | ||
| ) + | ||
| ggplot2::geom_hline(yintercept = 0) + | ||
| ggplot2::labs( | ||
| x = if (sort_by_group) "Index" else NULL, | ||
| y = "Pointwise ELPD Difference (loo_1 - loo_2)" | ||
| ) | ||
|
|
||
| if (is.null(group)) { | ||
| plot <- plot + | ||
| ggplot2::geom_point( | ||
| position = jitter_position, | ||
| alpha = alpha, | ||
| size = size | ||
| ) | ||
| } else { | ||
| plot <- plot + | ||
| ggplot2::geom_point( | ||
| ggplot2::aes(color = group), | ||
| position = jitter_position, | ||
| alpha = alpha, | ||
| size = size | ||
| ) + | ||
| ggplot2::labs(color = "Group") | ||
| } | ||
|
|
||
| if (!is.null(label_threshold)) { | ||
| plot <- plot + | ||
| ggplot2::geom_text( | ||
| ggplot2::aes(label = labels), | ||
| position = jitter_position, | ||
| vjust = -0.5 | ||
| ) | ||
| } | ||
|
|
||
| plot | ||
| } | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.