Skip to content

Commit 691cfea

Browse files
authored
Merge pull request #182 from stan-dev/remove-arm-dependency
Remove 'arm' dependency
2 parents b27d9b7 + bd6d26d commit 691cfea

File tree

3 files changed

+80
-46
lines changed

3 files changed

+80
-46
lines changed

DESCRIPTION

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ Imports:
3131
ggridges,
3232
hexbin
3333
Suggests:
34-
arm,
3534
gridExtra (>= 2.2.1),
3635
knitr (>= 1.16),
3736
loo (>= 2.0.0),

R/ppc-errors.R

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -311,46 +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) {
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) {
316316
check_ignored_arguments(...)
317317

318318
y <- validate_y(y)
319319
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)
348321

349322
mixed_scheme <- is_mixed_scheme(color_scheme_get())
350323
point_fill <- get_color(ifelse(mixed_scheme, "m", "d"))
351324
point_color <- get_color(ifelse(mixed_scheme, "mh", "dh"))
352325
graph <-
353-
ggplot(binned, aes_(x = ~ xbar)) +
326+
ggplot(binned, aes_(x = ~ ey_bar)) +
354327
geom_hline(
355328
yintercept = 0,
356329
linetype = 2,
@@ -373,7 +346,7 @@ ppc_error_binned <- function(y, yrep, ..., size = 1, alpha = 0.25) {
373346
size = size
374347
) +
375348
geom_point(
376-
mapping = aes_(y = ~ ybar),
349+
mapping = aes_(y = ~ err_bar),
377350
shape = 21,
378351
fill = point_fill,
379352
color = point_color
@@ -384,12 +357,13 @@ ppc_error_binned <- function(y, yrep, ..., size = 1, alpha = 0.25) {
384357
) +
385358
bayesplot_theme_get()
386359

387-
if (n > 1)
360+
if (nrow(yrep) > 1) {
388361
graph <- graph +
389362
facet_wrap(
390363
facets = ~rep_id
391364
# labeller = label_bquote(italic(y)[rep](.(rep_id)))
392365
)
366+
}
393367

394368
graph +
395369
force_axes_in_facets() +
@@ -415,18 +389,77 @@ grouped_error_data <- function(y, yrep, group) {
415389
}
416390
dat <- dplyr::bind_rows(errs)
417391
dat$y_id <- NULL
418-
dat
392+
return(dat)
419393
}
420394

395+
binned_error_data <- function(y, yrep, bins = NULL) {
396+
if (is.null(bins)) {
397+
bins <- n_bins(length(y))
398+
}
421399

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)
432408
}
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+

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)