Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Change density defaultbw = "nrd0" tobw = "sj"#5854

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
teunbrand wants to merge4 commits intotidyverse:main
base:main
Choose a base branch
Loading
fromteunbrand:density_bw_to_SJ
Draft
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
mergecalc_bw() intoprecompute_bw()
  • Loading branch information
@teunbrand
teunbrand committedApr 22, 2024
commit55870d3f6725bebb496b7899907bd5fe2f37c8d2
4 changes: 4 additions & 0 deletionsR/stat-density.R
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -222,6 +222,10 @@ reflect_density <- function(dens, bounds, from, to) {
# using `density(..., warnWbw = FALSE)`.
precompute_bw = function(x, bw = "nrd0") {
bw <- bw[1]
if (length(x) < 2) {
cli::cli_abort("{.arg x} must contain at least 2 elements to select a \\
bandwidth automatically.")
}
if (is.character(bw)) {
bw <- arg_match0(bw, c("nrd0", "nrd", "ucv", "bcv", "sj", "sj-ste", "sj-dpi"))
bw <- switch(
Expand Down
23 changes: 1 addition & 22 deletionsR/stat-ydensity.R
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -96,7 +96,7 @@ StatYdensity <- ggproto("StatYdensity", Stat,
}
range <- range(data$y, na.rm = TRUE)
modifier <- if (trim) 0 else 3
bw <-calc_bw(data$y, bw)
bw <-precompute_bw(data$y, bw)
dens <- compute_density(
data$y, data[["weight"]],
from = range[1] - modifier * bw, to = range[2] + modifier * bw,
Expand DownExpand Up@@ -151,24 +151,3 @@ StatYdensity <- ggproto("StatYdensity", Stat,

dropped_aes = "weight"
)

calc_bw <- function(x, bw) {
if (is.character(bw)) {
if (length(x) < 2) {
cli::cli_abort("{.arg x} must contain at least 2 elements to select a bandwidth automatically.")
}

bw <- switch(
to_lower_ascii(bw),
nrd0 = stats::bw.nrd0(x),
nrd = stats::bw.nrd(x),
ucv = stats::bw.ucv(x),
bcv = stats::bw.bcv(x),
sj = ,
`sj-ste` = stats::bw.SJ(x, method = "ste"),
`sj-dpi` = stats::bw.SJ(x, method = "dpi"),
cli::cli_abort("{.var {bw}} is not a valid bandwidth rule.")
)
}
bw
}
6 changes: 3 additions & 3 deletionstests/testthat/test-stat-ydensity.R
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
test_that("calc_bw() requires at least two values and correct method", {
expect_snapshot_error(calc_bw(1, "nrd0"))
expect_silent(calc_bw(1:5, "nrd0"))
expect_snapshot_error(calc_bw(1:5, "test"))
expect_snapshot_error(precompute_bw(1, "nrd0"))
expect_silent(precompute_bw(1:5, "nrd0"))
expect_snapshot_error(precompute_bw(1:5, "test"))
})

test_that("`drop = FALSE` preserves groups with 1 observations", {
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp