## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  fig.width  = 7,
  fig.height = 4
)

## ----holdout, message = FALSE-------------------------------------------------
library(PTSDdiag)
library(dplyr)

data("simulated_ptsd")
ptsd <- rename_ptsd_columns(simulated_ptsd[1:120, ],
                            id_col = c("patient_id", "age", "sex"))

ho <- holdout_validation(
  ptsd,
  train_ratio = 0.7,
  n_symptoms  = 6,
  n_required  = 4,
  n_top       = 3,
  score_by    = "balanced_accuracy",
  seed        = 42
)
ho$without_clusters$best_combinations
ho$without_clusters$summary

## ----cv, message = FALSE------------------------------------------------------
cv <- cross_validation(
  ptsd,
  k          = 2,
  n_symptoms = 6,
  n_required = 4,
  n_top      = 3,
  score_by   = "balanced_accuracy",
  seed       = 42
)
cv$without_clusters$summary_by_fold
cv$without_clusters$combinations_summary  # NULL if no combination repeats

## ----external, message = FALSE------------------------------------------------
data("simulated_ptsd_genpop")

# Export the holdout-derived combinations for reuse
tmp <- tempfile(fileext = ".json")
write_combinations(ho$without_clusters$best_combinations, tmp,
                   n_required = 4,
                   score_by = "balanced_accuracy",
                   description = "Six-symptom, four-required definition")

# A second analyst reads the file and applies it to the community sample.
# simulated_ptsd_genpop also carries paired CAPS-5 columns (C1..C20); here we
# use only the PCL-5 items, so we select those before standardising.
spec   <- read_combinations(tmp)
genpop <- rename_ptsd_columns(
  simulated_ptsd_genpop[1:120, c("patient_id", "age", "sex", paste0("S", 1:20))],
  id_col = c("patient_id", "age", "sex")
)

applied <- apply_symptom_combinations(genpop, spec$combinations,
                                      n_required = spec$n_required)
summarize_ptsd_changes(applied) %>%
  create_readable_summary()

## ----all-combinations, fig.alt = "Balanced accuracy of every four-symptom combination by rank"----
curve <- score_all_combinations(ptsd, n_symptoms = 4, n_required = 3,
                                show_progress = FALSE)
nrow(curve)
head(curve, 3)

plot(curve$rank, curve$balanced_accuracy, type = "l", log = "x",
     xlab = "Combination rank (log scale)", ylab = "Balanced accuracy")

