-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Weighted density #5254
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
Weighted density #5254
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ | |
#' @eval rd_computed_vars( | ||
#' density = "density estimate.", | ||
#' count = "density * number of points - useful for stacked density plots.", | ||
#' wdensity = "density * sum of weights. In absence of weights, the same as | ||
#' `count`", | ||
#' scaled = "density estimate, scaled to maximum of 1.", | ||
#' n = "number of points.", | ||
#' ndensity = "alias for `scaled`, to mirror the syntax of [`stat_bin()`]." | ||
|
@@ -112,17 +114,19 @@ StatDensity <- ggproto("StatDensity", Stat, | |
compute_density <- function(x, w, from, to, bw = "nrd0", adjust = 1, | ||
kernel = "gaussian", n = 512, | ||
bounds = c(-Inf, Inf)) { | ||
nx <- length(x) | ||
nx <- w_sum <- length(x) | ||
if (is.null(w)) { | ||
w <- rep(1 / nx, nx) | ||
} else { | ||
w <- w / sum(w) | ||
w_sum <- sum(w) | ||
w <- w / w_sum | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've been trying to come up with examples that should be valid, but aren't, but I'm not having an easy time of it. If you censor a non-zero weight and are left with all zero weights, the weighted density should reflect that and show a flat line. Currently, it does this, but also passes on a warning from devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2
df <- data.frame(x = 1:3)
ggplot(df, aes(x)) +
geom_density(aes(weight = c(1, 0, 0), y = after_stat(wdensity)),
bounds = c(1.1, Inf))
#> Warning: Some data points are outside of `bounds`. Removing them.
#> Warning in density.default(x, weights = w, bw = bw, adjust = adjust, kernel =
#> kernel, : sum(weights) != 1 -- will not get true density Created on 2023-04-11 with reprex v2.0.2 I'm wondering if there is something to be improved here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. As far as I'm concerned you can handle it however you see fit, I just wanted to call out the inconsistency. |
||
} | ||
|
||
# Adjust data points and weights to all fit inside bounds | ||
sample_data <- fit_data_to_bounds(bounds, x, w) | ||
x <- sample_data$x | ||
w <- sample_data$w | ||
w_sum <- sample_data$w_sum * w_sum | ||
nx <- length(x) | ||
|
||
# if less than 2 points return data frame of NAs and a warning | ||
|
@@ -134,6 +138,7 @@ compute_density <- function(x, w, from, to, bw = "nrd0", adjust = 1, | |
scaled = NA_real_, | ||
ndensity = NA_real_, | ||
count = NA_real_, | ||
wdensity = NA_real_, | ||
n = NA_integer_, | ||
.size = 1 | ||
)) | ||
|
@@ -156,6 +161,7 @@ compute_density <- function(x, w, from, to, bw = "nrd0", adjust = 1, | |
scaled = dens$y / max(dens$y, na.rm = TRUE), | ||
ndensity = dens$y / max(dens$y, na.rm = TRUE), | ||
count = dens$y * nx, | ||
wdensity = dens$y * w_sum, | ||
n = nx, | ||
.size = length(dens$x) | ||
) | ||
|
@@ -164,7 +170,7 @@ compute_density <- function(x, w, from, to, bw = "nrd0", adjust = 1, | |
# Check if all data points are inside bounds. If not, warn and remove them. | ||
fit_data_to_bounds <- function(bounds, x, w) { | ||
is_inside_bounds <- (bounds[1] <= x) & (x <= bounds[2]) | ||
|
||
w_sum <- 1 | ||
if (any(!is_inside_bounds)) { | ||
cli::cli_warn("Some data points are outside of `bounds`. Removing them.") | ||
x <- x[is_inside_bounds] | ||
|
@@ -175,7 +181,7 @@ fit_data_to_bounds <- function(bounds, x, w) { | |
} | ||
} | ||
|
||
return(list(x = x, w = w)) | ||
return(list(x = x, w = w, w_sum = w_sum)) | ||
} | ||
|
||
# Update density estimation to mitigate boundary effect at known `bounds`: | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.