## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(gp3bayes)

## -----------------------------------------------------------------------------
simulation <- simulate_hierarchical_binary_data(
  n_participants = 24,
  trials_per_participant = 12,
  n_items = 8,
  random_slope_sd = 0,
  seed = 202602
)

## -----------------------------------------------------------------------------
contract <- create_model_contract(
  family = "binary",
  outcome_col = "selected",
  participant_col = "participant_id",
  item_col = "item_id",
  trial_col = "trial_id",
  condition_col = "condition",
  predictors = "trial_covariate"
)

readiness <- audit_model_readiness(simulation$data, contract)
readiness

## -----------------------------------------------------------------------------
design <- audit_design_support(
  simulation$data,
  contract,
  separation = FALSE,
  strict_readiness = TRUE
)
design

## -----------------------------------------------------------------------------
prepared <- prepare_hierarchical_binary_data(
  simulation$data,
  contract,
  condition_levels = c("control", "treatment"),
  scale_predictors = "trial_covariate"
)

specification <- specify_binary_model(
  prepared,
  baseline = 0.35
)

prior_check <- check_binary_prior_predictive(
  specification,
  draws = 200,
  seed = 202603
)
prior_check

## -----------------------------------------------------------------------------
sensitivity_plan <- create_sensitivity_suite_plan(
  prior_scale = TRUE,
  psis_loo = TRUE
)

manifest <- create_analysis_manifest(
  specification = specification,
  estimands = "standardized_probability_contrast",
  sensitivity_plan = sensitivity_plan,
  seed = 202604,
  label = "gp3bayes 0.2.0 synthetic release case"
)

frozen_manifest <- freeze_analysis_manifest(manifest)
frozen_manifest

## ----eval=FALSE---------------------------------------------------------------
# fit_rstan <- fit_binary_model_backend(
#   specification,
#   backend = "rstan",
#   chains = 2,
#   iter = 2000,
#   warmup = 1000,
#   cores = 2,
#   seed = 202604
# )
# 
# fit_cmdstanr <- fit_binary_model_backend(
#   specification,
#   backend = "cmdstanr",
#   chains = 2,
#   iter = 2000,
#   warmup = 1000,
#   cores = 2,
#   seed = 202604
# )

## ----eval=FALSE---------------------------------------------------------------
# diagnostics <- diagnose_model_fit(fit_cmdstanr)
# posterior <- summarise_model_posterior(fit_cmdstanr)
# ppc <- check_model_ppc(fit_cmdstanr, draws = 500, seed = 202605)
# estimands <- estimate_model_estimands(fit_cmdstanr)
# loo_result <- compute_psis_loo(fit_cmdstanr)
# suite <- run_sensitivity_suite(fit_cmdstanr, sensitivity_plan)

## ----eval=FALSE---------------------------------------------------------------
# parity <- audit_backend_parity(fit_rstan, fit_cmdstanr)
# parity
# plot(parity)

## ----eval=FALSE---------------------------------------------------------------
# evidence <- collect_model_evidence(
#   fit = fit_cmdstanr,
#   design = design,
#   diagnostics = diagnostics,
#   posterior = posterior,
#   ppc = ppc,
#   estimands = estimands,
#   loo = loo_result,
#   sensitivity = suite,
#   manifest = frozen_manifest
# )
# 
# fit_schema <- freeze_gp3bayes_schema(
#   capture_gp3bayes_schema(fit_cmdstanr)
# )
# 
# evidence
# model_workflow_status(evidence)

