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

## -----------------------------------------------------------------------------
library(WFC)

dims_safe <- wf_dims(
  sex = c("F", "M"),
  age = c("18-34", "35+")
)
design_frame <- data.frame(
  id = sprintf("r%02d", 1:16),
  sex = rep(c("F", "M"), 8),
  age = rep(c("18-34", "18-34", "35+", "35+"), 4),
  base_weight = 1,
  stringsAsFactors = FALSE
)
analysis_frame <- data.frame(
  id = design_frame$id,
  satisfaction = seq(40, 70, length.out = 16),
  approved = rep(c(0, 1), 8),
  stringsAsFactors = FALSE
)

design <- wf_prepare_design(
  design_frame,
  id = "id",
  calibration = c("sex", "age"),
  base_weight = "base_weight"
)
design

## -----------------------------------------------------------------------------
csv_file <- system.file(
  "extdata", "safe-target-example.csv", package = "WFC"
)
csv_source <- paste0(csv_file, ".source.dcf")
cat(paste(readLines(csv_source, warn = FALSE), collapse = "\n"))

target_verified <- wf_import_target(
  csv_file,
  csv_source,
  dims_safe,
  key_map = c(sex = "sex", age = "age"),
  count = "count",
  production = FALSE
)
target_verified$identity

## -----------------------------------------------------------------------------
xlsx_file <- system.file(
  "extdata", "safe-target-example.xlsx", package = "WFC"
)
if (requireNamespace("openxlsx", quietly = TRUE)) {
  target_from_excel <- wf_import_target(
    xlsx_file,
    paste0(xlsx_file, ".source.dcf"),
    dims_safe,
    key_map = c(sex = "sex", age = "age"),
    count = "count",
    production = FALSE
  )
  identical(target_verified$groups, target_from_excel$groups)
}

## ----controlled-plan, eval=FALSE----------------------------------------------
# cell_plan <- wf_plan_cells(
#   design,
#   target_verified,
#   dims_safe,
#   min_cell = 5,
#   max_weight_ratio = 4
# )
# plan <- wf_plan_weights(
#   design,
#   target_verified,
#   dims_safe,
#   method = "raking",
#   bounds = c(0.3, 3),
#   min_cell = 5,
#   cell_plan = cell_plan
# )
# 
# plan$precheck
# is.null(plan$weights)

## ----guided-plan, eval=FALSE--------------------------------------------------
# guided <- wf_guided_plan(
#   design_frame,
#   id = "id",
#   calibration = c("sex", "age"),
#   dims = dims_safe,
#   target_file = csv_file,
#   source_file = csv_source,
#   source_type = "population",
#   key_map = c(sex = "sex", age = "age"),
#   count = "count",
#   base_weight = "base_weight",
#   production = FALSE
# )
# 
# wf_report(guided, audience = "decision")$table
# names(wf_report(guided, audience = "statistician")$sections)

## ----approval-and-execution, eval=FALSE---------------------------------------
# approval <- wf_approve_plan(
#   plan,
#   approver = "Reviewer name",
#   role = "statistician"
# )
# locked <- wf_execute_plan(
#   plan,
#   approval,
#   design,
#   target_verified
# )

## ----agent-refusal, eval=FALSE------------------------------------------------
# agent_refusal <- tryCatch(
#   wf_approve_plan(
#     plan,
#     approver = "Agent",
#     role = "assistant",
#     actor_type = "agent"
#   ),
#   wf_error_safety = function(condition) condition$data
# )
# agent_refusal[c("code", "severity", "next_actions")]

## ----post-lock, eval=FALSE----------------------------------------------------
# analysis_ready <- wf_attach_weights(
#   analysis_frame,
#   locked,
#   id = "id",
#   weight_name = ".weight"
# )
# impact <- wf_assess_impact(
#   locked,
#   analysis_frame,
#   id = "id",
#   outcomes = c("satisfaction", "approved")
# )
# 
# wf_report(locked, audience = "decision")
# wf_report(impact, audience = "statistician")
# wf_audit_export(impact, "impact-audit.json")

