@@ -311,18 +311,19 @@ ppc_error_scatter_avg_vs_x <-
311
311
312
312
# ' @rdname PPC-errors
313
313
# ' @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 ) {
315
316
check_ignored_arguments(... )
316
317
317
318
y <- validate_y(y )
318
319
yrep <- validate_yrep(yrep , y )
319
- binned <- binned_error_data(y , yrep )
320
+ binned <- binned_error_data(y , yrep , bins = bins )
320
321
321
322
mixed_scheme <- is_mixed_scheme(color_scheme_get())
322
323
point_fill <- get_color(ifelse(mixed_scheme , " m" , " d" ))
323
324
point_color <- get_color(ifelse(mixed_scheme , " mh" , " dh" ))
324
325
graph <-
325
- ggplot(binned , aes_(x = ~ xbar )) +
326
+ ggplot(binned , aes_(x = ~ ey_bar )) +
326
327
geom_hline(
327
328
yintercept = 0 ,
328
329
linetype = 2 ,
@@ -345,7 +346,7 @@ ppc_error_binned <- function(y, yrep, ..., size = 1, alpha = 0.25) {
345
346
size = size
346
347
) +
347
348
geom_point(
348
- mapping = aes_(y = ~ ybar ),
349
+ mapping = aes_(y = ~ err_bar ),
349
350
shape = 21 ,
350
351
fill = point_fill ,
351
352
color = point_color
@@ -388,21 +389,48 @@ grouped_error_data <- function(y, yrep, group) {
388
389
}
389
390
dat <- dplyr :: bind_rows(errs )
390
391
dat $ y_id <- NULL
391
- dat
392
+ return ( dat )
392
393
}
393
394
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 ) {
395
423
N <- length(ey )
396
- break_ids <- floor(N * (1 : (nbins - 1 )) / nbins )
424
+ break_ids <- floor(N * (1 : (bins - 1 )) / bins )
397
425
if (any(break_ids == 0 )) {
398
- nbins <- 1
426
+ bins <- 1
399
427
}
400
- if (nbins == 1 ) {
428
+ if (bins == 1 ) {
401
429
breaks <- c(- Inf , sum(range(ey )) / 2 , Inf )
402
430
} else {
403
431
ey_sort <- sort(ey )
404
432
breaks <- - Inf
405
- for (i in 1 : (nbins - 1 )) {
433
+ for (i in 1 : (bins - 1 )) {
406
434
break_i <- break_ids [i ]
407
435
ey_range <- ey_sort [c(break_i , break_i + 1 )]
408
436
if (diff(ey_range ) == 0 ) {
@@ -417,43 +445,21 @@ bin_errors <- function(rep_id, ey, r, nbins) {
417
445
breaks <- unique(c(breaks , Inf ))
418
446
}
419
447
420
- nbins <- length(breaks ) - 1
421
448
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 ) {
425
453
mark <- which(ey_binned == i )
426
454
ey_bar <- mean(ey [mark ])
427
455
r_bar <- mean(r [mark ])
428
456
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 )
430
458
}
431
459
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 )
447
462
}
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 )
458
464
}
459
465
0 commit comments