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

## ----data---------------------------------------------------------------------
library(oda)

# Cross-classification: rows = vote (class), cols = party (attribute).
#          Rep (0)   Dem (1)
#  Con (0)   118       78     n(Con) = 196
#  Pro (1)    34      177     n(Pro) = 211
vote  <- c(rep(0L, 118), rep(0L,  78), rep(1L,  34), rep(1L, 177))
party <- c(rep(0L, 118), rep(1L,  78), rep(0L,  34), rep(1L, 177))

table(vote, party,
      dnn = c("Vote (0=Con, 1=Pro)", "Party (0=Rep, 1=Dem)"))

## ----fit-canonical, eval=FALSE------------------------------------------------
# # Canonical reference run (mc_iter = 25000L; not evaluated in CRAN vignette)
# fit <- oda_fit(
#   x         = party,
#   y         = vote,
#   attr_type = "ordered",
#   direction = "greater",
#   mc_iter   = 25000L,
#   loo       = "on"
# )

## ----fit----------------------------------------------------------------------
# CRAN-safe run: mc_iter = 500L for vignette rendering speed.
# Training rule, ESS, and confusion matrix are identical to the canonical run.
# The MC p-value reflects fewer permutations; use the canonical run for publication.
fit <- oda_fit(
  x         = party,
  y         = vote,
  attr_type = "ordered",
  direction = "greater",
  mc_iter   = 500L,
  mc_seed   = 42L,
  loo       = "on"
)

## ----print-fit----------------------------------------------------------------
print(fit)

## ----confusion----------------------------------------------------------------
# Confusion matrix: actual vote (rows) x predicted vote (cols)
conf_mat <- matrix(
  c(fit$confusion$TN, fit$confusion$FP,
    fit$confusion$FN, fit$confusion$TP),
  nrow = 2L, byrow = TRUE,
  dimnames = list(Actual    = c("Con(0)", "Pro(1)"),
                  Predicted = c("Con(0)", "Pro(1)"))
)
print(conf_mat)

## ----metrics------------------------------------------------------------------
summary(fit)

## ----pv-----------------------------------------------------------------------
# Predictive value: accuracy when the model makes a prediction into each class
pv_con <- fit$confusion$TN / (fit$confusion$TN + fit$confusion$FN)
pv_pro <- fit$confusion$TP / (fit$confusion$TP + fit$confusion$FP)
cat("PV Con (0):", round(pv_con * 100, 1), "%\n")
cat("PV Pro (1):", round(pv_pro * 100, 1), "%\n")

