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

## ----binary-simulation--------------------------------------------------------
library(gp3bayes)

binary_simulation <- simulate_hierarchical_binary_data(
  n_participants = 16,
  trials_per_participant = 10,
  n_items = 8,
  random_slope_sd = 0.20,
  seed = 2026
)

binary_simulation
head(binary_simulation$data)

## ----binary-contract----------------------------------------------------------
binary_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 = c(
    "participant_covariate",
    "trial_covariate"
  ),
  interaction = c(
    "condition",
    "participant_covariate"
  ),
  random_slope = TRUE
)

binary_contract

## ----binary-preparation-------------------------------------------------------
binary_prepared <- prepare_hierarchical_binary_data(
  binary_simulation$data,
  binary_contract,
  condition_levels = c(
    "control",
    "treatment"
  )
)

binary_prepared
binary_prepared$decision_log

## ----binary-specification-----------------------------------------------------
binary_specification <- specify_binary_model(
  binary_prepared,
  baseline = 0.35,
  intercept_scale = 1.5,
  coefficient_scale = 0.75,
  group_sd_scale = 1,
  correlation_eta = 2,
  student_df = 3
)

binary_specification
binary_specification$priors$table

## ----binary-prior-predictive--------------------------------------------------
binary_prior_predictive <- check_binary_prior_predictive(
  binary_specification,
  draws = 100,
  seed = 2027
)

binary_prior_predictive
binary_prior_predictive$checks

## ----binary-fit, eval=FALSE---------------------------------------------------
# binary_translation <- translate_binary_model_to_brms(
#   binary_specification
# )
# 
# binary_fit <- fit_binary_model(
#   binary_specification,
#   chains = 2,
#   iter = 2000,
#   warmup = 1000,
#   cores = 2,
#   seed = 2028,
#   adapt_delta = 0.95,
#   max_treedepth = 12,
#   refresh = 100
# )

## ----binary-validation, eval=FALSE--------------------------------------------
# binary_diagnostics <- diagnose_binary_fit(
#   binary_fit
# )
# 
# binary_posterior <- summarise_binary_posterior(
#   binary_fit,
#   probability = 0.95
# )
# 
# binary_predictive <- check_binary_posterior_predictive(
#   binary_fit,
#   draws = 500,
#   seed = 2029
# )
# 
# binary_diagnostics
# binary_posterior
# binary_predictive

## ----binary-sensitivity-recovery, eval=FALSE----------------------------------
# binary_sensitivity <- assess_binary_prior_sensitivity(
#   binary_fit,
#   scale_multipliers = c(
#     tighter = 0.5,
#     wider = 2
#   )
# )
# 
# binary_recovery <- run_binary_recovery(
#   repetitions = 20,
#   seed = 3001
# )

## ----binary-report, eval=FALSE------------------------------------------------
# binary_report_file <- tempfile(fileext = ".md")
# 
# binary_report <- create_binary_model_report(
#   binary_fit,
#   diagnostics = binary_diagnostics,
#   posterior_summary = binary_posterior,
#   posterior_predictive = binary_predictive,
#   prior_sensitivity = binary_sensitivity,
#   recovery = binary_recovery,
#   file = binary_report_file
# )
# 
# binary_report
# unlink(binary_report_file)

