Skip to content

Commit 707eed8

Browse files
committed
add bins argument
[ci skip]
1 parent a1b0233 commit 707eed8

File tree

2 files changed

+49
-41
lines changed

2 files changed

+49
-41
lines changed

R/ppc-errors.R

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -311,18 +311,19 @@ ppc_error_scatter_avg_vs_x <-
311311

312312
#' @rdname PPC-errors
313313
#' @export
314-
ppc_error_binned <- function(y, yrep, ..., size = 1, alpha = 0.25) {
314+
#' @param bins For \code{ppc_error_binned}, the number of bins to use (approximately).
315+
ppc_error_binned <- function(y, yrep, ..., bins = NULL, size = 1, alpha = 0.25) {
315316
check_ignored_arguments(...)
316317

317318
y <- validate_y(y)
318319
yrep <- validate_yrep(yrep, y)
319-
binned <- binned_error_data(y, yrep)
320+
binned <- binned_error_data(y, yrep, bins = bins)
320321

321322
mixed_scheme <- is_mixed_scheme(color_scheme_get())
322323
point_fill <- get_color(ifelse(mixed_scheme, "m", "d"))
323324
point_color <- get_color(ifelse(mixed_scheme, "mh", "dh"))
324325
graph <-
325-
ggplot(binned, aes_(x = ~ xbar)) +
326+
ggplot(binned, aes_(x = ~ ey_bar)) +
326327
geom_hline(
327328
yintercept = 0,
328329
linetype = 2,
@@ -345,7 +346,7 @@ ppc_error_binned <- function(y, yrep, ..., size = 1, alpha = 0.25) {
345346
size = size
346347
) +
347348
geom_point(
348-
mapping = aes_(y = ~ ybar),
349+
mapping = aes_(y = ~ err_bar),
349350
shape = 21,
350351
fill = point_fill,
351352
color = point_color
@@ -388,21 +389,48 @@ grouped_error_data <- function(y, yrep, group) {
388389
}
389390
dat <- dplyr::bind_rows(errs)
390391
dat$y_id <- NULL
391-
dat
392+
return(dat)
392393
}
393394

394-
bin_errors <- function(rep_id, ey, r, nbins) {
395+
binned_error_data <- function(y, yrep, bins = NULL) {
396+
if (is.null(bins)) {
397+
bins <- n_bins(length(y))
398+
}
399+
400+
errors <- compute_errors(y, yrep)
401+
binned_errs <- list()
402+
for (s in 1:nrow(errors)) {
403+
binned_errs[[s]] <- bin_errors(ey = yrep[s,], r = errors[s,], bins = bins,
404+
rep_id = s)
405+
}
406+
dat <- dplyr::bind_rows(binned_errs)
407+
return(dat)
408+
}
409+
410+
# calculate number of bins binned_error_data()
411+
# @parmam N Number of data points, length(y)
412+
n_bins <- function(N) {
413+
if (N <= 10) {
414+
return(floor(N / 2))
415+
} else if (N > 10 && N < 100) {
416+
return(10)
417+
} else { # N >= 100
418+
return(floor(sqrt(N)))
419+
}
420+
}
421+
422+
bin_errors <- function(ey, r, bins, rep_id = NULL) {
395423
N <- length(ey)
396-
break_ids <- floor(N * (1:(nbins - 1)) / nbins)
424+
break_ids <- floor(N * (1:(bins - 1)) / bins)
397425
if (any(break_ids == 0)) {
398-
nbins <- 1
426+
bins <- 1
399427
}
400-
if (nbins == 1) {
428+
if (bins == 1) {
401429
breaks <- c(-Inf, sum(range(ey)) / 2, Inf)
402430
} else {
403431
ey_sort <- sort(ey)
404432
breaks <- -Inf
405-
for (i in 1:(nbins - 1)) {
433+
for (i in 1:(bins - 1)) {
406434
break_i <- break_ids[i]
407435
ey_range <- ey_sort[c(break_i, break_i + 1)]
408436
if (diff(ey_range) == 0) {
@@ -417,43 +445,21 @@ bin_errors <- function(rep_id, ey, r, nbins) {
417445
breaks <- unique(c(breaks, Inf))
418446
}
419447

420-
nbins <- length(breaks) - 1
421448
ey_binned <- as.numeric(cut(ey, breaks))
422-
423-
out <- matrix(NA, nrow = nbins, ncol = 3)
424-
for (i in 1:nbins) {
449+
bins <- length(breaks) - 1
450+
out <- matrix(NA, nrow = bins, ncol = 4)
451+
colnames(out) <- c("ey_bar", "err_bar", "se2", "bin")
452+
for (i in 1:bins) {
425453
mark <- which(ey_binned == i)
426454
ey_bar <- mean(ey[mark])
427455
r_bar <- mean(r[mark])
428456
s <- if (length(r[mark]) > 1) sd(r[mark]) else 0
429-
out[i, ] <- c(ey_bar, r_bar, 2 * s / sqrt(length(mark)))
457+
out[i, ] <- c(ey_bar, r_bar, 2 * s / sqrt(length(mark)), i)
430458
}
431459
out <- as.data.frame(out)
432-
colnames(out) <- c("xbar", "ybar", "se2")
433-
out$rep_id <- as.integer(rep_id)
434-
return(out)
435-
}
436-
437-
438-
binned_error_data <- function(y, yrep) {
439-
N <- length(y)
440-
if (N >= 100) {
441-
nbins <- floor(sqrt(N))
442-
} else if (N > 10 && N < 100) {
443-
nbins <- 10
444-
} else {
445-
# if (N <= 10)
446-
nbins <- floor(N / 2)
460+
if (!is.null(rep_id)) {
461+
out$rep_id <- as.integer(rep_id)
447462
}
448-
449-
errors <- compute_errors(y, yrep)
450-
binned_df <- bin_errors(rep_id = 1, ey = yrep[1, ], r = errors[1, ], nbins = nbins)
451-
if (nrow(errors) > 1) {
452-
for (s in 2:nrow(errors)) {
453-
binned_s <- bin_errors(rep_id = s, ey = yrep[s,], r = errors[s,], nbins = nbins)
454-
binned_df <- rbind(binned_df, binned_s)
455-
}
456-
}
457-
return(binned_df)
463+
return(out)
458464
}
459465

man/PPC-errors.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)