Nothing
# Regenerate two synthetic, fitted examples. Not run during package checks.
# Usage: Rscript tools/generate-tutorial-data.R output_dir model_cache
args <- commandArgs(trailingOnly = TRUE)
stopifnot(length(args) == 2L)
for (path in args) dir.create(path, recursive = TRUE, showWarnings = FALSE)
output <- normalizePath(args[1L], mustWork = TRUE)
cache <- normalizePath(args[2L], mustWork = TRUE)
options(cmdstanr_write_stan_file_dir = cache)
library(brms)
examples <- list()
records <- list()
base_fit <- NULL
for (index in 1:2) {
scenario <- c("independent", "clustered")[index]
data_seed <- 31300L + index
fit_seed <- 41300L + index
set.seed(data_seed)
n <- if (index == 1L) 200L else 300L
x <- rnorm(n)
cluster <- if (index == 1L) seq_len(n) else rep(seq_len(30), each = 10)
error <- if (index == 1L) exp(0.3 * x) * rnorm(n) else {
sqrt(0.6) * rnorm(30)[cluster] + sqrt(0.4) * rnorm(n)
}
data <- data.frame(y = 0.5 + 0.7 * x + error, x = x, group = cluster)
fit <- if (is.null(base_fit)) {
brm(bf(y ~ x, quantile = 0.5), data = data,
family = asym_laplace(link_quantile = "identity"),
prior = c(prior(normal(0, 2.5), class = "b"),
prior(normal(0, 2.5), class = "Intercept"),
prior(exponential(1), class = "sigma")),
backend = "cmdstanr", chains = 2, cores = 2,
iter = 2000, warmup = 1000, seed = fit_seed,
control = list(adapt_delta = 0.99, max_treedepth = 12), refresh = 0)
} else {
update(base_fit, newdata = data, recompile = FALSE, file = NULL,
chains = 2, cores = 2, iter = 2000, warmup = 1000, seed = fit_seed,
control = list(adapt_delta = 0.99, max_treedepth = 12), refresh = 0)
}
if (is.null(base_fit)) base_fit <- fit
stopifnot(isTRUE(all.equal(fit$data$x, data$x)),
isTRUE(all.equal(fit$data$y, data$y)))
# Keep sampling IDs outside brms too: unused formula columns may be dropped.
examples[[scenario]] <- list(fit = fit, data = data, cluster = cluster,
data_seed = data_seed, fit_seed = fit_seed, truth = c(Intercept = 0.5, x = 0.7))
summary <- posterior::summarise_draws(posterior::as_draws_array(fit))
summary <- summary[is.finite(summary$sd) & summary$sd > 0, ]
nuts <- nuts_params(fit)
record <- data.frame(scenario = scenario, observations = n,
sampling_units = length(unique(cluster)), data_seed = data_seed,
fit_seed = fit_seed, max_rhat = max(summary$rhat),
min_bulk_ess = min(summary$ess_bulk),
divergences = sum(nuts$Value[nuts$Parameter == "divergent__"]))
stopifnot(is.finite(record$max_rhat), record$max_rhat <= 1.05,
record$min_bulk_ess >= 200, record$divergences == 0)
records[[scenario]] <- record
print(record)
}
saveRDS(examples, file.path(output, "median-examples.rds"), compress = "xz")
write.csv(do.call(rbind, records), file.path(output, "fit-diagnostics.csv"), row.names = FALSE)
writeLines(c(capture.output(sessionInfo()), "",
capture.output(cmdstanr::cmdstan_version())),
file.path(output, "session.txt"))
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.