## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
cmdstan_available <- identical(Sys.getenv("PAIRWISELLM_RUN_CMDSTAN_VIGNETTES"), "true") &&
  requireNamespace("cmdstanr", quietly = TRUE) &&
  tryCatch(!is.null(cmdstanr::cmdstan_version()), error = function(e) FALSE)

## ----install, eval=FALSE------------------------------------------------------
# install.packages(
#   "cmdstanr",
#   repos = c("https://stan-dev.r-universe.dev", getOption("repos"))
# )
# cmdstanr::check_cmdstan_toolchain(fix = TRUE)
# cmdstanr::install_cmdstan()

## ----availability-------------------------------------------------------------
cmdstan_available

## ----input--------------------------------------------------------------------
library(pairwiseLLM)

data("example_writing_pairs", package = "pairwiseLLM")
observed <- example_writing_pairs[seq_len(min(40L, nrow(example_writing_pairs))), ]
results_tbl <- build_btl_results_data(
  observed,
  backend = "offline_fixture",
  model = "deterministic_observations"
)
ids <- sort(unique(c(results_tbl$A_id, results_tbl$B_id)))

results_tbl[, c("pair_uid", "A_id", "B_id", "better_id", "winner_pos", "phase")]

## ----fit, eval=FALSE, purl=TRUE-----------------------------------------------
# fit <- fit_bayes_btl_mcmc(
#   results = results_tbl,
#   ids = ids,
#   model_variant = "btl_e_b",
#   cmdstan = list(
#     chains = 2L,
#     parallel_chains = 2L,
#     iter_warmup = 250L,
#     iter_sampling = 250L,
#     seed = 7007L,
#     core_fraction = 1
#   )
# )
# 
# refits <- summarize_refits(fit)
# items <- summarize_items(fit)
# 
# refits[, c(
#   "round_id", "total_pairs", "diagnostics_pass",
#   "divergences", "max_rhat", "min_ess_bulk"
# )]
# items[, c("ID", "theta_mean", "theta_sd", "rank_mean", "deg")]

## ----cumulative, eval=FALSE, purl=TRUE----------------------------------------
# cumulative <- fit_bayes_btl_mcmc(
#   results_tbl,
#   ids = ids,
#   pair_counts = c(20L, 40L),
#   subset_method = "sample",
#   seed = 7007L,
#   cmdstan = list(chains = 2L, parallel_chains = 2L, seed = 7007L)
# )
# 
# summarize_refits(cumulative)
# summarize_items(cumulative, refit_id = 2L)

