inst/study/study-figures.R

# Static, reproducible study figures. Requires ggplot2 only for figure generation.
study_theme <- function() {
  ggplot2::theme_bw(base_size = 11, base_family = "Times New Roman") +
    ggplot2::theme(text = ggplot2::element_text(colour = "black"),
      axis.text = ggplot2::element_text(colour = "black"),
      legend.position = "bottom", legend.title = ggplot2::element_blank(),
      panel.grid.minor = ggplot2::element_blank(),
      panel.grid.major.y = ggplot2::element_blank(),
      panel.grid.major.x = ggplot2::element_line(colour = "grey90", linewidth = 0.2),
      strip.background = ggplot2::element_rect(fill = "white"),
      strip.text = ggplot2::element_text(colour = "black"))
}

normal_coverage_plot <- function(data_dir) {
  d <- read.csv(file.path(data_dir, "normal-summary.csv"), stringsAsFactors = FALSE)
  design <- read.csv(file.path(data_dir, "normal-design.csv"), stringsAsFactors = FALSE)
  methods <- c("Posterior", "IJ independent", "IJ cluster", "IJ cluster: t reference")
  offsets <- setNames(c(-0.24, -0.08, 0.08, 0.24), methods)
  d$position <- length(design$scenario) + 1 - match(d$scenario, design$scenario) + offsets[d$method]
  d$method <- factor(d$method, levels = methods)
  ggplot2::ggplot(d, ggplot2::aes(x = coverage, y = position, shape = method)) +
    ggplot2::geom_vline(xintercept = 0.95, linetype = 2, colour = "grey45") +
    ggplot2::geom_segment(ggplot2::aes(x = coverage_lower, xend = coverage_upper,
                                       yend = position), linewidth = 0.35) +
    ggplot2::geom_point(size = 2.1, stroke = 0.5) +
    ggplot2::scale_shape_manual(values = c(16, 0, 17, 2), drop = FALSE) +
    ggplot2::scale_y_continuous(breaks = seq_len(nrow(design)), labels = rev(design$label)) +
    ggplot2::scale_x_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2),
                               labels = function(x) paste0(round(100 * x), "%")) +
    ggplot2::labs(x = "Empirical coverage of nominal 95% intervals", y = NULL) +
    ggplot2::guides(shape = ggplot2::guide_legend(nrow = 2, byrow = TRUE)) + study_theme()
}

draw_precision_plot <- function(data_dir) {
  d <- read.csv(file.path(data_dir, "draw-audit-replicates.csv"))
  sizes <- sort(unique(d$draws))
  d$position <- match(d$draws, sizes)
  theory <- data.frame(position = rep(seq_along(sizes), each = 2),
                       tail = rep(c("Lower", "Upper"), length(sizes)),
                       relative_error = unlist(lapply(sizes, function(s) {
                         qchisq(c(0.025, 0.975), df = s - 1) / (s - 1) - 1
                       })))
  ggplot2::ggplot(d, ggplot2::aes(x = position, y = 100 * relative_error)) +
    ggplot2::geom_hline(yintercept = 0, colour = "grey55") +
    ggplot2::geom_line(data = theory, ggplot2::aes(group = tail),
                        linetype = 2, linewidth = 0.4) +
    ggplot2::geom_boxplot(ggplot2::aes(group = position), width = 0.4,
                           fill = "white", outlier.shape = NA, linewidth = 0.4) +
    ggplot2::geom_point(position = ggplot2::position_jitter(width = 0.12, height = 0, seed = 12),
                          size = 0.7, colour = "grey35", alpha = 0.65) +
    ggplot2::scale_x_continuous(breaks = seq_along(sizes), labels = format(sizes, big.mark = ",")) +
    ggplot2::labs(x = "Independent posterior draws", y = "Relative numerical error in IJ SE (%)") + study_theme()
}

quantile_coverage_plot <- function(data_dir) {
  d <- read.csv(file.path(data_dir, "quantile-summary.csv"), stringsAsFactors = FALSE)
  methods <- c("Posterior", "IJ independent", "IJ cluster")
  offsets <- setNames(c(-0.18, 0, 0.18), methods)
  d$position <- match(d$parameter, c("Intercept", "Slope")) + offsets[d$method]
  d$method <- factor(d$method, methods)
  d$scenario <- factor(d$scenario, c("independent", "clustered"),
                         c("Independent, N = 200", "Clustered, J = 30, N = 300"))
  ggplot2::ggplot(d, ggplot2::aes(x = position, y = coverage, shape = method)) +
    ggplot2::geom_hline(yintercept = 0.95, linetype = 2, colour = "grey45") +
    ggplot2::geom_linerange(ggplot2::aes(ymin = coverage_lower, ymax = coverage_upper), linewidth = 0.4) +
    ggplot2::geom_point(size = 2.1, stroke = 0.5) +
    ggplot2::scale_shape_manual(values = c(16, 0, 17)) +
    ggplot2::scale_x_continuous(breaks = 1:2, labels = c("Intercept", "Slope"), limits = c(0.6, 2.4)) +
    ggplot2::scale_y_continuous(limits = c(0, 1), labels = function(x) paste0(round(100 * x), "%")) +
    ggplot2::facet_wrap(~scenario, nrow = 1) +
    ggplot2::labs(x = NULL, y = "Coverage of nominal 95% intervals") + study_theme()
}

write_study_figures <- function(data_dir, output_dir) {
  dir.create(output_dir, recursive = TRUE, showWarnings = FALSE)
  plots <- list(normal_coverage = normal_coverage_plot(data_dir),
                 draw_precision = draw_precision_plot(data_dir),
                 quantile_coverage = quantile_coverage_plot(data_dir))
  heights <- c(normal_coverage = 6.0, draw_precision = 3.8, quantile_coverage = 3.8)
  for (name in names(plots)) {
    ggplot2::ggsave(file.path(output_dir, paste0(name, ".pdf")), plots[[name]],
                     width = 7, height = heights[[name]], device = grDevices::cairo_pdf)
    ggplot2::ggsave(file.path(output_dir, paste0(name, ".png")), plots[[name]],
                     width = 7, height = heights[[name]], dpi = 300,
                     device = grDevices::png, type = "cairo")
  }
  invisible(plots)
}

Try the IJSE package in your browser

Any scripts or data that you put into this service are public.

IJSE documentation built on Oct. 22, 2026, 5:18 p.m.