@@ -311,46 +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 ) {
315
- suggested_package( " arm " )
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 ) {
316
316
check_ignored_arguments(... )
317
317
318
318
y <- validate_y(y )
319
319
yrep <- validate_yrep(yrep , y )
320
- errors <- compute_errors(y , yrep )
321
-
322
- ny <- length(y )
323
- if (ny > = 100 ) {
324
- nbins <- floor(sqrt(ny ))
325
- } else if (ny > 10 && ny < 100 ) {
326
- nbins <- 10
327
- } else {
328
- # if (ny <= 10)
329
- nbins <- floor(ny / 2 )
330
- }
331
-
332
- n <- nrow(yrep )
333
- binned <- .binner(
334
- rep_id = 1 ,
335
- ey = yrep [1 , ],
336
- r = errors [1 , ],
337
- nbins = nbins
338
- )
339
- if (n > 1 ) {
340
- for (i in 2 : nrow(errors ))
341
- binned <- rbind(binned , .binner(
342
- rep_id = i ,
343
- ey = yrep [i ,],
344
- r = errors [i ,],
345
- nbins
346
- ))
347
- }
320
+ binned <- binned_error_data(y , yrep , bins = bins )
348
321
349
322
mixed_scheme <- is_mixed_scheme(color_scheme_get())
350
323
point_fill <- get_color(ifelse(mixed_scheme , " m" , " d" ))
351
324
point_color <- get_color(ifelse(mixed_scheme , " mh" , " dh" ))
352
325
graph <-
353
- ggplot(binned , aes_(x = ~ xbar )) +
326
+ ggplot(binned , aes_(x = ~ ey_bar )) +
354
327
geom_hline(
355
328
yintercept = 0 ,
356
329
linetype = 2 ,
@@ -373,7 +346,7 @@ ppc_error_binned <- function(y, yrep, ..., size = 1, alpha = 0.25) {
373
346
size = size
374
347
) +
375
348
geom_point(
376
- mapping = aes_(y = ~ ybar ),
349
+ mapping = aes_(y = ~ err_bar ),
377
350
shape = 21 ,
378
351
fill = point_fill ,
379
352
color = point_color
@@ -384,12 +357,13 @@ ppc_error_binned <- function(y, yrep, ..., size = 1, alpha = 0.25) {
384
357
) +
385
358
bayesplot_theme_get()
386
359
387
- if (n > 1 )
360
+ if (nrow( yrep ) > 1 ) {
388
361
graph <- graph +
389
362
facet_wrap(
390
363
facets = ~ rep_id
391
364
# labeller = label_bquote(italic(y)[rep](.(rep_id)))
392
365
)
366
+ }
393
367
394
368
graph +
395
369
force_axes_in_facets() +
@@ -415,18 +389,77 @@ grouped_error_data <- function(y, yrep, group) {
415
389
}
416
390
dat <- dplyr :: bind_rows(errs )
417
391
dat $ y_id <- NULL
418
- dat
392
+ return ( dat )
419
393
}
420
394
395
+ binned_error_data <- function (y , yrep , bins = NULL ) {
396
+ if (is.null(bins )) {
397
+ bins <- n_bins(length(y ))
398
+ }
421
399
422
- .binner <- function (rep_id , ey , r , nbins ) {
423
- binned_errors <- arm :: binned.resids(ey , r , nbins )$ binned
424
- binned_errors <- binned_errors [, c(" xbar" , " ybar" , " 2se" )]
425
- if (length(dim(binned_errors )) < 2 )
426
- binned_errors <- t(binned_errors )
427
- colnames(binned_errors ) <- c(" xbar" , " ybar" , " se2" )
428
- data.frame (
429
- rep_id = as.integer(rep_id ), # create_yrep_ids(rep_id),
430
- binned_errors
431
- )
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 )
432
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 ) {
423
+ N <- length(ey )
424
+ break_ids <- floor(N * (1 : (bins - 1 )) / bins )
425
+ if (any(break_ids == 0 )) {
426
+ bins <- 1
427
+ }
428
+ if (bins == 1 ) {
429
+ breaks <- c(- Inf , sum(range(ey )) / 2 , Inf )
430
+ } else {
431
+ ey_sort <- sort(ey )
432
+ breaks <- - Inf
433
+ for (i in 1 : (bins - 1 )) {
434
+ break_i <- break_ids [i ]
435
+ ey_range <- ey_sort [c(break_i , break_i + 1 )]
436
+ if (diff(ey_range ) == 0 ) {
437
+ if (ey_range [1 ] == min(ey )) {
438
+ ey_range [1 ] <- - Inf
439
+ } else {
440
+ ey_range [1 ] <- max(ey [ey < ey_range [1 ]])
441
+ }
442
+ }
443
+ breaks <- c(breaks , sum(ey_range ) / 2 )
444
+ }
445
+ breaks <- unique(c(breaks , Inf ))
446
+ }
447
+
448
+ ey_binned <- as.numeric(cut(ey , breaks ))
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 ) {
453
+ mark <- which(ey_binned == i )
454
+ ey_bar <- mean(ey [mark ])
455
+ r_bar <- mean(r [mark ])
456
+ s <- if (length(r [mark ]) > 1 ) sd(r [mark ]) else 0
457
+ out [i , ] <- c(ey_bar , r_bar , 2 * s / sqrt(length(mark )), i )
458
+ }
459
+ out <- as.data.frame(out )
460
+ if (! is.null(rep_id )) {
461
+ out $ rep_id <- as.integer(rep_id )
462
+ }
463
+ return (out )
464
+ }
465
+
0 commit comments