tools/summarise-study.R

# Produce tables for the worked example and MCMC attempt accounting.
# Usage: Rscript tools/summarise-study.R /path/to/IJSE /path/to/study-output /path/to/figures
args <- commandArgs(trailingOnly = TRUE)
stopifnot(length(args) == 3L)
source(file.path(args[1L], "inst", "study", "study-figures.R"))
source(file.path(args[1L], "inst", "study", "study-functions.R"))
library(IJSE)
library(brms)
d <- read.csv(file.path(args[2L], "quantile-attempts.csv"), stringsAsFactors = FALSE)
quantile_raw <- read.csv(file.path(args[2L], "quantile-replicates.csv"), stringsAsFactors = FALSE)
accounting <- fit_accounting(d, quantile_raw)
write.csv(accounting, file.path(args[2L], "quantile-accounting.csv"), row.names = FALSE)
normal_raw <- read.csv(file.path(args[2L], "normal-replicates.csv"), stringsAsFactors = FALSE)
write.csv(paired_coverage(normal_long(normal_raw)), file.path(args[2L], "normal-paired-coverage.csv"), row.names = FALSE)
write.csv(paired_coverage(quantile_raw), file.path(args[2L], "quantile-paired-coverage.csv"), row.names = FALSE)
fit <- readRDS(file.path(args[2L], "quantile-clustered-example.rds"))
stopifnot("group" %in% names(fit$data), length(unique(fit$data$group)) == 30L)
result <- ijse(fit, cluster_var = fit$data$group, save_influence = TRUE)
stopifnot(result$clustered, result$n_units == 30L)
first <- quantile_raw[quantile_raw$scenario == "clustered" &
                        quantile_raw$replication == 1L &
                        quantile_raw$method == "IJ cluster", ]
stopifnot(isTRUE(all.equal(unname(result$se[c("b_Intercept", "b_x")]),
                          first$se[match(c("Intercept", "Slope"), first$parameter)])))
worked <- as.data.frame(summary(result)$coefficients)
worked$parameter <- rownames(worked)
rownames(worked) <- NULL
write.csv(worked, file.path(args[2L], "worked-example.csv"), row.names = FALSE)
transformed <- ijse(fit, cluster_var = fit$data$group,
                    transform = function(d) cbind(median_at_x1 = d[, "b_Intercept"] + d[, "b_x"]))
write.csv(data.frame(quantity = names(coef(transformed)), estimate = unname(coef(transformed)),
                      posterior_sd = unname(transformed$posterior_sd), ij_se = unname(transformed$se)),
            file.path(args[2L], "worked-transform.csv"), row.names = FALSE)
stopifnot(isTRUE(all.equal(vcov(result), vcov(ijse(fit, cluster_var = fit$data$group,
                                                 block_size = 50, diagnostics = "none")))))
write_study_figures(args[2L], args[3L])

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.