Skip to content

Commit

Permalink
joint.bw = "mean" instead of "owm"
Browse files Browse the repository at this point in the history
  • Loading branch information
zeileis committed Jan 27, 2025
1 parent 1c3d7b4 commit c52ccfc
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
14 changes: 7 additions & 7 deletions R/type_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#' @param joint.bw character string indicating whether (and how) the smoothing
#' bandwidth should be computed from the joint data distribution, in case
#' there are multiple subgroups (from `by` or `facet`). The default of
#' `"owm"` will compute the joint bandwidth as the observation-weighted mean
#' of the individual subgroup bandwidths. Choosing `"full"` will result in a
#' `"mean"` will compute the joint bandwidth as the mean of the individual
#' subgroup bandwidths (weighted by their number of observations). Choosing `"full"` will result in a
#' joint bandwidth computed from the full distribution (merging all subgroups).
#' For `"none"` the individual bandwidth will be computed independently for
#' each subgroup. When no grouped or faceted densities are visualized, the
Expand All @@ -38,7 +38,7 @@
#' bandwidth across all subgroups. The following strategies are available via the
#' `joint.bw` argument.
#'
#' The default `joint.bw = "owm"` first computes the individual bandwidths for
#' The default `joint.bw = "mean"` first computes the individual bandwidths for
#' each group but then computes their mean, weighted by the number of observations
#' in each group. This will work well when all groups have similar amounts of
#' scatter (similar variances), even when they have potentially rather different
Expand Down Expand Up @@ -77,7 +77,7 @@
#' # individual subgroup bandwidths (weighted by group size) as the
#' # joint bandwidth. Alternatively, the bandwidth from the "full"
#' # data or separate individual bandwidths ("none") can be used.
#' tinyplot(~Sepal.Length | Species, data = iris, type = "density") # owm (default)
#' tinyplot(~Sepal.Length | Species, data = iris, type = "density") # mean (default)
#' tinyplot_add(joint.bw = "full", lty = 2) # full data
#' tinyplot_add(joint.bw = "none", lty = 3) # none (individual)
#' legend("topright", c("Mean", "Full", "None"), lty = 1:3, bty = "n", title = "Joint BW")
Expand All @@ -91,11 +91,11 @@ type_density = function(
kernel = c("gaussian", "epanechnikov", "rectangular", "triangular", "biweight", "cosine", "optcosine"),
n = 512,
# more args from density here?
joint.bw = c("owm", "full", "none"),
joint.bw = c("mean", "full", "none"),
alpha = NULL
) {
kernel = match.arg(kernel, c("gaussian", "epanechnikov", "rectangular", "triangular", "biweight", "cosine", "optcosine"))
joint.bw = match.arg(joint.bw, c("owm", "full", "none"))
joint.bw = match.arg(joint.bw, c("mean", "full", "none"))
out = list(
data = data_density(bw = bw, adjust = adjust, kernel = kernel, n = n,
joint.bw = joint.bw, alpha = alpha),
Expand Down Expand Up @@ -136,7 +136,7 @@ data_density = function(bw = "nrd0", adjust = 1, kernel = "gaussian", n = 512,
}
if (joint.bw == "full") {
dens_bw = bw_fun(kernel = bw, unlist(sapply(datapoints, `[[`, "x")))
} else if (joint.bw == "owm") {
} else if (joint.bw == "mean") {
bws = sapply(datapoints, function(dat) bw_fun(kernel = bw, dat$x))
ws = sapply(datapoints, nrow)
dens_bw = weighted.mean(bws, ws)
Expand Down
12 changes: 6 additions & 6 deletions R/type_ridge.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
#' the version used by S.
#' @param joint.bw character string indicating whether (and how) the smoothing
#' bandwidth should be computed from the joint data distribution. The default of
#' `"owm"` will compute the joint bandwidth as the observation-weighted mean
#' of the individual subgroup bandwidths. Choosing `"full"` will result in a
#' `"mean"` will compute the joint bandwidth as the mean of the individual
#' subgroup bandwidths (weighted by their number of observations). Choosing `"full"` will result in a
#' joint bandwidth computed from the full distribution (merging all subgroups).
#' For `"none"` the individual bandwidth will be computed independently for
#' each subgroup. See \code{\link{type_density}} for some discussion of
Expand Down Expand Up @@ -168,7 +168,7 @@ type_ridge = function(
probs = NULL,
ylevels = NULL,
bw = "nrd0",
joint.bw = c("owm", "full", "none"),
joint.bw = c("mean", "full", "none"),
adjust = 1,
kernel = c("gaussian", "epanechnikov", "rectangular", "triangular", "biweight", "cosine", "optcosine"),
n = 512,
Expand All @@ -180,7 +180,7 @@ type_ridge = function(
) {

kernel = match.arg(kernel, c("gaussian", "epanechnikov", "rectangular", "triangular", "biweight", "cosine", "optcosine"))
joint.bw = match.arg(joint.bw, c("owm", "full", "none"))
joint.bw = match.arg(joint.bw, c("mean", "full", "none"))

out = list(
draw = draw_ridge(),
Expand All @@ -206,7 +206,7 @@ type_ridge = function(
#
## Underlying data_ridge function
data_ridge = function(bw = "nrd0", adjust = 1, kernel = "gaussian", n = 512,
joint.bw = "owm",
joint.bw = "mean",
scale = 1.5,
global.max = TRUE,
gradient = FALSE,
Expand Down Expand Up @@ -264,7 +264,7 @@ data_ridge = function(bw = "nrd0", adjust = 1, kernel = "gaussian", n = 512,
}
if (joint.bw == "full") {
dens_bw = bw_fun(kernel = bw, unlist(sapply(datapoints, `[[`, "x")))
} else if (joint.bw == "owm") {
} else if (joint.bw == "mean") {
bws = sapply(datapoints, function(dat) bw_fun(kernel = bw, dat$x))
ws = sapply(datapoints, nrow)
dens_bw = weighted.mean(bws, ws)
Expand Down
2 changes: 1 addition & 1 deletion inst/tinytest/test-density.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ expect_snapshot_plot(f1, label = "density_type_bw_sj")
f1 = function() {
tinyplot(~ Sepal.Width | Species, iris, type = type_density(joint.bw = "none"))
tinyplot_add(type = type_density(joint.bw = "full"), lty = 2)
tinyplot_add(type = type_density(joint.bw = "owm"), lty = 3)
tinyplot_add(type = type_density(joint.bw = "mean"), lty = 3)
legend("topright", c("None", "Full", "OWM"), lty = 1:3, title = "Joint BW")
}
expect_snapshot_plot(f1, label = "density_type_joint_bw")
Expand Down
2 changes: 1 addition & 1 deletion inst/tinytest/test-ridge.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ f = function() {
tinyplot(
Species ~ Sepal.Width, data = iris,
main = 'joint.bw = "owm"',
type = type_ridge(joint.bw = "owm")
type = type_ridge(joint.bw = "mean")
)
}
expect_snapshot_plot(f, label = "ridge_joint_owm")
Expand Down
10 changes: 5 additions & 5 deletions man/type_density.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/type_ridge.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c52ccfc

Please sign in to comment.